搜索词>>webservice 耗时0.0040
  • Java编程纯jdk java编写webservice服务和客服端

    Java编程中纯jdk java方式编写webservice服务(server)和客服端(client)Java编程中纯jdk java方式编写webservice服务(server)和客服端(client)<br /> <br /> 1.server<br /> <br /> 编写一个简单的server接口 <pre> <code class="language-java">package com.leftso.ws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface CommService { // 使用@WebMethod注解标注WebServiceI接口中的方法 @WebMethod String sayHello(String name); } </code></pre> <br /> 编写接口实现: <pre> <code class="language-java">package com.leftso.ws; import javax.jws.WebService; /** * 使用@WebService注解标注WebServiceI接口的实现类WebServiceImpl * * @author leftso * */ @WebService public class CommServiceImp implements CommService { @Override public String sayHello(String name) { return "Hello ," + name; } } </code></pre> <br /> 将该接口以webservice方式发布出去: <pre> <code class="language-java">package com.leftso.ws; import javax.xml.ws.Endpoint; public class WebServicePublish { public static void main(String[] args) { // 定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx // String address = "http://192.168.1.100:8989/";这个WebService发布地址的写法是合法的 // String address = // "http://192.168.1.100:8989/Webservice";这个WebService发布地址的是合法的 String address = "http://127.0.0.1:9000/WS_Server/Webservice"; // 使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用 Endpoint.publish(address, new CommServiceImp()); System.out.println("发布webservice成功!"); System.out.println("请使用:"+address+"?wsdl 访问"); } } </code></pre> <br /> 运行main方法,访问地址http://127.0.0.1:9000/WS_Server/Webservice?wsdl<br /> 可以看见wsdl文档如下<br /> <img alt="wsdl" class="img-thumbnail" src="/resources/assist/images/blog/f26343d873c54ffd93e74e4b14952f0f.png" /><br /> 2.client<br /> 调用,可以用最简单的jdk自带的工具wsimport生成本地Java代码,以类的方式直接调用方法就行 <pre> <code>wsimport -keep url_wsdl(自己的wsdl地址)</code></pre> <br />  
  • spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务

    spring boot整合cxf发布webservice服务和cxf客户端调用,说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行、航空公司的机票查询接口等。<h2>一.前言</h2>     说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行、航空公司的机票查询接口等。本博客主要讲解得是spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务<br /> 本案例使用maven方式 <h2>二.编码</h2> 核心文件清单<br /> <strong>1.pom.xml</strong> <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.leftso</groupId> <artifactId>demo-webservice-cxf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo-webservice-cxf</name> <description>Demo project for Spring Boot security</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- CXF webservice --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.7</version> </dependency> <!-- CXF webservice --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project></code></pre> <br /> <strong>2.CommonService.java 服务接口</strong> <pre> <code class="language-java">package com.leftso.webservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * 接口 * * @author leftso * */ @WebService(name = "CommonService", // 暴露服务名称 targetNamespace = "http://webservice.leftso.com/"// 命名空间,一般是接口的包名倒序 ) public interface CommonService { @WebMethod @WebResult(name = "String", targetNamespace = "") public String sayHello(@WebParam(name = "userName") String name); }</code></pre> <br /> <strong>3.接口实现</strong> <pre> <code class="language-java">package com.leftso.webservice; import javax.jws.WebService; import org.springframework.stereotype.Component; /** * 接口实现 * * @author leftso * */ @WebService(serviceName = "CommonService", // 与接口中指定的name一致 targetNamespace = "http://webservice.leftso.com/", // 与接口中的命名空间一致,一般是接口的包名倒 endpointInterface = "com.leftso.webservice.CommonService"// 接口地址 ) @Component public class CommonServiceImp implements CommonService { @Override public String sayHello(String name) { return "Hello ," + name; } }</code></pre> <br /> <strong>4.配置cxf服务发布,默认服务在Host:port/services/***路径下</strong> <pre> <code class="language-java">package com.leftso.config; import javax.xml.ws.Endpoint; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.leftso.webservice.CommonService; @Configuration public class CxfConfig { @Autowired private Bus bus; @Autowired CommonService commonService; /** JAX-WS **/ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, commonService); endpoint.publish("/CommonService"); return endpoint; } }</code></pre> 这里相当于把Commonservice接口发布在了路径<span style="color:#ff0000">/services/CommonService</span>下,wsdl文档路径为<span style="color:#ff0000">http://localhost:8080/services/CommonService?wsdl</span><br /> <br /> <strong>创建基于cxf的客服端调用webservice接口(非使用wsdl文档生成java类)</strong> <pre> <code class="language-java">package com.leftso.client; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.leftso.webservice.CommonService; public class CxfClient { public static void main(String[] args) { cl1(); } /** * 方式1.代理类工厂的方式,需要拿到对方的接口 */ public static void cl1() { try { // 接口地址 String address = "http://localhost:8080/services/CommonService?wsdl"; // 代理工厂 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 设置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 设置接口类型 jaxWsProxyFactoryBean.setServiceClass(CommonService.class); // 创建一个代理接口实现 CommonService cs = (CommonService) jaxWsProxyFactoryBean.create(); // 数据准备 String userName = "Leftso"; // 调用代理接口的方法调用并返回结果 String result = cs.sayHello(userName); System.out.println("返回结果:" + result); } catch (Exception e) { e.printStackTrace(); } } /** * 动态调用方式 */ public static void cl2() { // 创建动态客户端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl"); // 需要密码的情况需要加上用户名和密码 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, // PASS_WORD)); Object[] objects = new Object[0]; try { // invoke("方法名",参数1,参数2,参数3....); objects = client.invoke("sayHello", "Leftso"); System.out.println("返回数据:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }</code></pre> <br /> <br /> 调用后返回结果输出为 <pre> <code class="language-html">Hello,Leftso</code></pre> <br /> <br /> 项目源码GITHUB下载:<a href="https://github.com/leftso/demo-spring-boot-cxf" rel="external nofollow" target="_blank" >点击下载</a> <h2>三.问题Q&A</h2> <h3>3.1已知问题1</h3> 将上面例子的Spring Boot版本升级至1.5.x。启动项目报错: <pre> <code class="language-html">java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/ServletRegistrationBean at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_40] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_40] at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_40] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:570) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:697) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:640) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at com.leftso.Application.main(Application.java:10) [classes/:na] Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.ServletRegistrationBean at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_40] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_40] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_40] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_40] ... 23 common frames omitted</code></pre> <strong>解决办法:</strong><br />     出现这个问题的第一时间,我以为是常规的maven管理jar包依赖问题。后面群里有小伙伴多次尝试证明了不是maven的依赖包问题。而是spring boot的版本和cxf的版本不兼容问题。<br /> 已知兼容版本: <ul> <li><span style="color:#ff0000"><strong>Spring boot 1.4.x ------>cxf-spring-boot-starter-jaxws 3.1.x</strong></span></li> <li><span style="color:#ff0000"><strong>Spring boot 1.5.x------->cxf-spring-boot-starter-jaxws 3.2.x</strong></span></li> </ul> 所以出现上面问题,且用了spring boot 1.5.x的小伙伴请将cxf-spring-boot-starter-jaxws 版本升级到3.2.1即可解决。<br /> <br /> <strong>3.2修改spring boot cxf整合后默认的/services路径</strong><br /> 在spring boto的application配置文件中配置<br /> cxf.path=/yourpath
  • webservice面试题_WebService面试回答_WebService基础知识普及

    WebService 常见面试问题解答,在这篇文章中,我们几乎涵盖了您需要熟悉的Webservice概念的每个知识点部分,以回答任何层级问题。您还可以在分类中找到最佳的WebService服务面试问题和答案,这样您只有在必要时才能获得特定解决。Web Services 常见面试问题解答
  • spring boot整合Jersey2.x实现JAX-RS webservice

    spring boot整合Jersey2.x实现JAX-RS webservicespring boot整合Jersey2.x实现JAX-RS webservice<br /> <br /> jersey常用注解解释: <table class="table table-bordered table-hover"> <tbody> <tr> <td>Annotation</td> <td>作用</td> <td>说明</td> </tr> <tr> <td>@GET</td> <td>查询请求</td> <td>相当于数据库的查询数据操作</td> </tr> <tr> <td>@POST</td> <td>插入请求</td> <td>相当于数据库的插入数据操作</td> </tr> <tr> <td>@PUT</td> <td>更新请求</td> <td>相当于数据库的更新数据操作</td> </tr> <tr> <td>@DELETE</td> <td>删除请求</td> <td>相当于数据的删除数据操作</td> </tr> <tr> <td>@Path</td> <td>uri路径</td> <td>定义资源的访问路径,client通过这个路径访问资源。比如:@Path("user")</td> </tr> <tr> <td>@Produces</td> <td>指定返回MIME格式</td> <td>资源按照那种数据格式返回,可取的值有:MediaType.APPLICATION_XXX。比如:@Produces(MediaType.APPLICATION_XML)</td> </tr> <tr> <td>@Consumes</td> <td>接受指定的MIME格式</td> <td>只有符合这个参数设置的请求再能访问到这个资源。比如@Consumes("application/x-www-form-urlencoded")</td> </tr> <tr> <td>@PathParam</td> <td>uri路径参数</td> <td>写在方法的参数中,获得请求路径参数。比如:@PathParam("username") String userName</td> </tr> <tr> <td>@QueryParam</td> <td>uri路径请求参数</td> <td>写在方法的参数中,获得请求路径附带的参数。比如:@QueryParam("desc") String desc</td> </tr> <tr> <td>@DefaultValue</td> <td>设置@QueryParam参数的默认值</td> <td>如果@QueryParam没有接收到值,就使用默认值。比如:@DefaultValue("description") @QueryParam("desc") String desc</td> </tr> <tr> <td>@FormParam</td> <td>form传递的参数</td> <td>接受form传递过来的参数。比如:@FormParam("name") String userName</td> </tr> <tr> <td>@BeanParam</td> <td>通过Bena的形式传递参数</td> <td>接受client传递的bean类型的参数,同时这个bean可以在属性上配置@FormParam用以解决client的属性名称和bean的属性名称不一致的问题。比如:@BeanParam User user</td> </tr> <tr> <td>@Context</td> <td>获得一些系统环境信息</td> <td>通过@Context可以获得以下信息:UriInfo、ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse和HttpHeaders等</td> </tr> <tr> <td>@XmlRootElement</td> <td>将bean转换为xml</td> <td>如果要讲bean以xml或json的格式返回,必须要这个注解。比如:<br /> @XmlRootElement<br /> public class User{...}</td> </tr> </tbody> </table> <br /> 项目文件清单<br /> pom.xml <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.leftso</groupId> <artifactId>demo-webservice-jersey</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo-webservice-jersey</name> <description>demo-webservice-jersey</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> </code></pre> <br /> JerseyResourceConfig.java <pre> <code class="language-java">package com.leftso.config; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spring.scope.RequestContextFilter; /** * 继承ResourceConfig,并添加一些配置信息 * * @author leftso * */ public class JerseyResourceConfig extends ResourceConfig { public JerseyResourceConfig() { register(RequestContextFilter.class); // 配置那个包下面的会被Jersey扫描 packages("com.leftso.rest"); } } </code></pre> <br /> JerseyConfig.java <pre> <code class="language-java">package com.leftso.config; import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.servlet.ServletProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 配置Jersey * * @author leftso * */ @Configuration public class JerseyConfig { @Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*"); // our rest resources will be available in the path /rest/* registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyResourceConfig.class.getName()); return registration; } } </code></pre> <br /> RestResource.java(测试资源类) <pre> <code class="language-java">package com.leftso.rest; import java.util.HashMap; import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/") public class RestResource { @Path("/hello") // 具体路径 @GET // 请求方式 @Produces(MediaType.APPLICATION_JSON) // 返回的格式 // @Consumes()//接受指定的MIME格式 public Map<String, Object> hello() { Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "1"); map.put("codeMsg", "success"); return map; } } </code></pre> <br /> Application.java <pre> <code class="language-java">package com.leftso; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } </code></pre> <br /> 启动项目,访问地址:http://127.0.0.1:8080/rest/hello<br /> <br /> 显示内容: <pre> <code>{"code":"1","codeMsg":"success"}</code></pre> <br /> 项目下载:<br /> GITHUB:<a href="https://github.com/leftso/demo-webservice-jersey" rel="external nofollow" target="_blank">https://github.com/leftso/demo-webservice-jersey</a><br />  
  • spring boot RPC 框架 Hessian

    spring boot RPC 框架 Hessian,本文主要讲解spring boot整合hessian实现Spring boot RPC调用和Spring boot rpc框架hessian的使用。在微服务中经常遇到一个服务需要调用另外的服务这种内部调用的方式有RPC的方式。<h2>一.背景说明</h2>  该博客主要讲述的是spring boot整合hession实现Java编程中的RPC(远程调用)。实现RPC的方式还有很多例如Java的RMI,webservice等。这里主要讲解hessian的方式。 <h2>二.hessian服务端</h2> <h3>2.1创建一个spring boot项目添加web依赖和hessian依赖</h3> pom.xml如下: <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.xqlee.project</groupId> <artifactId>demo-springboot-rpc-hessian-server</artifactId> <version>2.0</version> <packaging>jar</packaging> <name>demo-springboot-rpc-hessian-server</name> <description>lee-leftso-v2.0</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.caucho/hessian --> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.51</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> </code></pre> <h3>2.2创建一个简单的service接口并实现</h3> HelloWorldServcie.java接口: <pre> <code class="language-java">package net.xqlee.project.service; public interface HelloWorldService { String sayHello(String name); } </code></pre> HelloWorldServiceServiceImp.java <pre> <code class="language-java">package net.xqlee.project.service; import org.springframework.stereotype.Service; @Service("HelloWorldService") public class HelloWorldServiceServiceImp implements HelloWorldService { @Override public String sayHello(String name) { return "Hello, " + name; } } </code></pre> <h3>2.3注册service接口到hessian工厂</h3> <pre> <code class="language-java">package net.xqlee.project; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.remoting.caucho.HessianServiceExporter; import net.xqlee.project.service.HelloWorldService; @SpringBootApplication public class DemoSpringbootRpcHessianApplication { @Autowired private HelloWorldService helloWorldService; public static void main(String[] args) { SpringApplication.run(DemoSpringbootRpcHessianApplication.class, args); } // 发布服务 @Bean(name = "/HelloWorldService") public HessianServiceExporter accountService() { HessianServiceExporter exporter = new HessianServiceExporter(); exporter.setService(helloWorldService); exporter.setServiceInterface(HelloWorldService.class); return exporter; } } </code></pre> <br /> 至此服务端编写完毕。<br /> <span style="color:#ff0000"><strong>提示:</strong></span><br /> <span style="color:#ff0000">这里需要一个操作.eclipse中进行右键导出,将接口导出到一个jar包文件,这样也是方便调用方使用。我这里导出并命令为rpc-hessian-demo-interface.jar</span> <p> </p> <h2>三.hessian客服端</h2> <h3>3.1创建一个spring boot项目添加web依赖和hessian依赖</h3> 注意,还需要引入刚才服务端导出的<strong><span style="color:#ff0000">rpc-hessian-demo-interface.jar</span></strong><br /> pom.xml文件: <pre> <code class="language-xml"><?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.xqlee.project</groupId> <artifactId>demo-springboot-rpc-hessian-client</artifactId> <version>2.0</version> <packaging>jar</packaging> <name>demo-springboot-rpc-hessian-client</name> <description>lee-leftso-v2.0</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.caucho/hessian --> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.51</version> </dependency> <!-- 本地引入hessian服务端的接口 --> <dependency> <groupId>net.xqlee.project</groupId> <artifactId>rpc-hessian-demo</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/rpc-hessian-demo-interface.jar</systemPath> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> </code></pre> <h3>3.2将引入的服务端接口通过hessian实例化</h3> <pre> <code class="language-java">package net.xqlee.project; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.remoting.caucho.HessianProxyFactoryBean; import net.xqlee.project.service.HelloWorldService; @SpringBootApplication public class DemoSpringbootRpcHessianClientApplication { public static void main(String[] args) { SpringApplication.run(DemoSpringbootRpcHessianClientApplication.class, args); } @Bean public HessianProxyFactoryBean helloClient() { HessianProxyFactoryBean factory = new HessianProxyFactoryBean(); factory.setServiceUrl("http://localhost:8083/HelloWorldService"); factory.setServiceInterface(HelloWorldService.class); return factory; } } </code></pre> <h3><br /> 3.3创建一个测试的controller进行调用测试</h3> <pre> <code class="language-java">package net.xqlee.project.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import net.xqlee.project.service.HelloWorldService; @RestController public class DemoHessianController { @Autowired HelloWorldService helloWorldService; @GetMapping("/rpchello.do") public Object rpcSayHello(String name) { return helloWorldService.sayHello(name); } } </code></pre> <br /> 浏览器访问地址:<br /> <img alt="rpc" class="img-thumbnail" src="/resources/assist/images/blog/539bdd2e07d847feb851a8d7513363dc.png" /><br /> <br />  
  • Linux yum nginx安装和nginx入门配置方法

    Linux(centos) nginx安装和nginx入门配置方法,centos yum 安装nginxLinux(centos) nginx安装和nginx入门配置方法<br /> <br /> 1.切换至root用户 <pre> <code>$su -</code></pre> 2.执行在线安装命令 <pre> <code class="language-html">#yum install nginx -y</code></pre> 如果上面提示以下内容 <pre> <code class="language-html">[root@template ~]# yum install nginx -y Loaded plugins: fastestmirror, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirror.bit.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn No package nginx available. Error: Nothing to do [root@template ~]# yum install nginx -y Loaded plugins: fastestmirror, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirror.bit.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn No package nginx available. </code></pre> 则需要配置nginx的源,如果直接安装则忽略这里的配置源步骤。<br /> <br /> <strong>配置yum的nginx源</strong><br /> <code>/etc/yum.repos.d/</code>目录下创建一个源配置文件<code>nginx.repo</code><br /> 命令: <pre> <code>cd /etc/yum.repos.d/</code></pre>   <pre> <code>vim nginx.repo</code></pre> <br /> nginx.repo内容输入: <pre> <code class="language-html">[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/6/$basearch/ gpgcheck=0 enabled=1</code></pre> 一般情况centos7会自带源的,只有部分centos6没有。所以配置源。配置完成后再执行安装命令 <pre> <code>yum install nginx -y</code></pre> <br /> <br /> 3.安装完成,启动nginx命令 <pre> <code class="language-html">#nginx</code></pre> 4.启动后在浏览器输入刚才安装好nginx的服务器IP地址<br /> <img alt="nginx" class="img-thumbnail" src="/resources/assist/images/blog/d26591d7-3032-44ee-8352-3d55fdcbe4de.png" style="height:375px; width:900px" /><br /> 出现上面的页面标识安装和启动成功<br /> 5.nginx停止 <pre> <code class="language-html">#nginx -s stop</code></pre> <br /> 6.nginx通用配置文件nginx.conf配置说明<br />   <pre> <code class="language-html">#指定nginx进程运行用户及用户组(默认nginx)(动静分离时候需要。注意权限否则静态文件可能访问不到。) user nginx; #nginx要开启的进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数 #推荐最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了 worker_processes 8; #为每个进程分配cpu,上例中将8个进程分配到8个cpu worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #worker_processes 4;#4个进程配置 #worker_cpu_affinity 0001 0010 0100 1000 error_log /var/log/nginx/error.log warn;#错误日志存放路径,以及日志级别默认warn pid /var/run/nginx.pid;#默认存放nginx进程id文件 #当一个nginx进程打开的最多文件描述符数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致 #[注]文件资源限制的配置可以在/etc/security/limits.conf设置,针对root/user等各个用户或者*代表所有用户来设置。 worker_rlimit_nofile 65535; events { #使用epoll的I/O模型,用这个模型来高效处理异步事件,epoll是linux平台下的高效模式 use epoll; #每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections #定义nginx每个进程的最大连接数为51200,一般网上都配置65535,根据张宴大神的建议51200即可 worker_connections 51200;#默认1024 } http { include /etc/nginx/mime.types;#实现对配置文件所包含的文件的设定 default_type application/octet-stream;#设置默认类型为二进制流 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃! keepalive_timeout 65; #客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。 client_header_buffer_size 4K; #############下面部分为动静分离需要nginx代理文件时候使用,仅做Tomcat分发则不需要启用############ #下面这个参数将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。 #open_file_cache max=102400 inactive=20s; #下面这个是指多长时间检查一次缓存的有效信息。 #open_file_cache_valid 30s; #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。 #open_file_cache_min_uses 1; ############上面面部分为动静分离需要nginx代理文件时候使用,仅做Tomcat分发则不需要启用############ #隐藏响应头中的有关操作系统和web server(Nginx)版本号的信息,这样对于安全性是有好处的。 server_tokens off; #gzip on; include /etc/nginx/conf.d/*.conf; }</code></pre> <br /> 7.新增一个nginx解析<br /> 切换目录至配置目录 <pre> <code>#cd /etc/nginx/conf.d/</code></pre> 新建一个解析配置文件test.conf <pre> <code class="language-html">#server {###配置虚拟机 # listen 80;#配置监听端口 # server_name zhljc.com www.zhljc.com srv.gift www.srv.gift 127.0.0.1; #配置访问域名,可以有多个[127.0.0.1/localhost表示任何] # location / {#对所有地址进行负载均衡 # root html;##定义服务器的默认网站根目录位置 # index index.html index.htm;##定义首页索引文件的名称 # # #以下三行,目的是将代理服务器收到的用户的信息传到真实服务器上 # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # # #websocket # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection "upgrade"; # # proxy_pass http://localhost:8080/bamboo; # }#end location # if ($host = 'www.xqlee.net'){ # rewrite ^/(.*)$ http://xqlee.net/$1 permanent; # } # #}#end server #upstream tomcats-webservice {#设定负载均衡的服务器列表,注意多个负载时候这里的名字必须唯一 #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。 #负载方式,默认 #1.轮询[每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除] #2.weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 #3.ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 #server localhost:8000 weight=1 max_fails=3 fail_timeout=60s; #第一台机器 #server localhost:8080 weight=1 max_fails=3 fail_timeout=60s; #第二台机器 #ip_hash; #server localhost:8000; #}#end upstream </code></pre> <br /> <br />