1:服务端接口:
package com.sqtoon.appcenter.client.httpinvoker; public interface IHelloService { public String sayHi(String name); }
2:服务端实现类:
package com.sqtoon.appcenter.platform.httpinvoker; import com.sqtoon.appcenter.client.httpinvoker.IHelloService; public class HelloService implements IHelloService { @Override public String sayHi(String name) { return name + ", 你好!"; } }
3:服务端web.xml:
<!-- spring remote begin --> <servlet> <servlet-name>remote</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/remote-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping> <!-- spring remote end -->
3:服务端spring配置文件:
(1):applicationContext.xml:
<bean id="helloService" class="com.sqtoon.appcenter.platform.httpinvoker.HelloService" />
(2):remote-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <bean id="helloServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" > <property name="service" ref="helloService" /> <property name="serviceInterface" value="com.sqtoon.appcenter.client.httpinvoker.IHelloService" /> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/helloservice" value-ref="helloServiceExporter" /> </map> </property> </bean> </beans>
----------------------------------------------------------------
4:客户端Spring配置文件:applicationContext-remote.xml:
(1):演示demo版:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd" default-lazy-init="true"> <bean id="sayHelloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://rpc.appcenter.sqtoon.com/remote/helloservice" /> <property name="serviceInterface" value="com.sqtoon.appcenter.client.httpinvoker.IHelloService"/> </bean> </beans>
(2):线上生成环境版本:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd" default-lazy-init="true"> <bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor"> <property name="httpClient"> <util:constant static-field="com.sqtoon.smvc.utils.http.HttpClient4Utils.httpClient"/> </property> </bean> <bean id="sayHelloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://rpc.appcenter.sqtoon.com/remote/helloservice" /> <property name="serviceInterface" value="com.sqtoon.appcenter.client.httpinvoker.IHelloService" /> <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" /> </bean> </beans>
5:客户端单元测试:
package com.sqtoon.appcenter.web.test.httpinvoker;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.sqtoon.appcenter.client.httpinvoker.IHelloService;
import com.sqtoon.smvc.SpringContextHolder;
@ContextConfiguration(locations = { "/applicationContext.xml", "/applicationContext-remote.xml" })
public class SayHelloServiceTest extends AbstractJUnit4SpringContextTests {
@Test
public void testHttpInvoker() {
try {
IHelloService helloService = (IHelloService) SpringContextHolder.getBean("sayHelloService");
System.out.println(helloService.sayHi("张三"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
6:HttpClient 参考实现:
public static final CloseableHttpClient httpClient = buildHttpClient(SOCKET_TIMEOUT_DEFAULT); public static final CloseableHttpClient httpClientNoTimeout = buildHttpClient(INFINITE_TIMEOUT); private static CloseableHttpClient buildHttpClient(int socketTimeout) { // 设置最大连接数和每个host的最大连接数 HttpClientBuilder httpClientBuilder = HttpClients.custom().setMaxConnTotal(500).setMaxConnPerRoute(100); // 内部默认使用 PoolingHttpClientConnectionManager 作为其连接管理器, 再次设置会覆盖下面其它参数的设置 // httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager()); // 设置服务器连接超时时间 及 服务器响应超时时间 httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT_DEFAULT).setSocketTimeout(socketTimeout).build()); // 设置在关闭TCP连接时最大停留时间,是否禁用优化算法延迟发送数据 及 在非阻塞I/O情况下的服务器响应时间 httpClientBuilder.setDefaultSocketConfig(SocketConfig.custom().setSoLinger(1000).setTcpNoDelay(true).setSoTimeout(socketTimeout).build()); // 设置接收/传输数据时的buffer大小,及默认字符集 httpClientBuilder.setDefaultConnectionConfig(ConnectionConfig.custom().setBufferSize(SOCKET_BUFFER_SIZE_DEFAULT).setCharset(Charset.forName(DEFAULT_CHARSET)).build()); // 设置失败后重新尝试访问的处理器,不使用已经请求的连接 httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_COUNT_DEFAULT, false)); // 代理名称 httpClientBuilder.setUserAgent(DEFAULT_AGENT); // List<Header> defaultHeaders = new ArrayList<Header>(); // defaultHeaders.add(new BasicHeader("", "")); // httpClientBuilder.setDefaultHeaders(defaultHeaders); // httpClientBuilder.addInterceptorFirst(itcp) // 构建HttpClient return httpClientBuilder.build(); }
7:以上使用maven版本:
Spring:4.1.1
HttpComponent:4.3.6