Spring Http Invoker

10901 단어 spring
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:서버 웹.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):데모 버 전 을 보 여 줍 니 다.
<?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

좋은 웹페이지 즐겨찾기