Spring Http Invoker
10901 단어 spring
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.