【JAVA】Spring HTTP Invoker 远程服务调用
2016-01-26 09:20
706 查看
远程服务调用在实际的项目中很常用,在多重方式中,HTTP应该算是比较常用的,对客户端来说也很方便
但是spring http invoker只支持JAVA语言,结构简单,只依赖与spring框架本身。
首先我们来看服务端(依赖于WEB容器来启动,tomcat/jetty)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201601/f90567bc8e52790f215768898d53503e)
定义接口和接口实现类,这里的接口和下面的客户端的接口是同一个
remote-servlet.xml
<bean name="/my" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="myServiceImpl" />
<property name="serviceInterface" value="com.chiwei.MyService" />
</bean>
<bean id="myServiceImpl" class="com.chiwei.MyServiceImpl" />
web.xml
<servlet>
<servlet-name>service</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>注意这里的HttpInvokerServiceExporter的bean配置,name就是客户端访问时URL的最后部分
web.xml中的url-pattern就是URL中的前一部分
实现类
public class MyServiceImpl implements MyService {
public String fun(String param) {
return "Hello " + param;
}
}
再来看客户端
![](https://oscdn.geek-share.com/Uploads/Images/Content/201601/834786c4195a530d2e1744cd149d4653)
配置文件
<bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
<property name="serviceUrl" value="http://localhost:8888/service/my"/>
<property name="serviceInterface" value="com.chiwei.MyService" />
</bean>这里的ip port依赖于部署的环境,port就是web容器里的配置端口
测试类
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:config/application-context.xml");
MyService service = (MyService)ac.getBean("userService");
System.out.println(service.fun(" chiwei !"));
}
}
Hello chiwei !
服务端通过tomcat容器启动即可
但是spring http invoker只支持JAVA语言,结构简单,只依赖与spring框架本身。
首先我们来看服务端(依赖于WEB容器来启动,tomcat/jetty)
定义接口和接口实现类,这里的接口和下面的客户端的接口是同一个
remote-servlet.xml
<bean name="/my" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="myServiceImpl" />
<property name="serviceInterface" value="com.chiwei.MyService" />
</bean>
<bean id="myServiceImpl" class="com.chiwei.MyServiceImpl" />
web.xml
<servlet>
<servlet-name>service</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>注意这里的HttpInvokerServiceExporter的bean配置,name就是客户端访问时URL的最后部分
web.xml中的url-pattern就是URL中的前一部分
实现类
public class MyServiceImpl implements MyService {
public String fun(String param) {
return "Hello " + param;
}
}
再来看客户端
配置文件
<bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
<property name="serviceUrl" value="http://localhost:8888/service/my"/>
<property name="serviceInterface" value="com.chiwei.MyService" />
</bean>这里的ip port依赖于部署的环境,port就是web容器里的配置端口
测试类
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:config/application-context.xml");
MyService service = (MyService)ac.getBean("userService");
System.out.println(service.fun(" chiwei !"));
}
}
Hello chiwei !
服务端通过tomcat容器启动即可
相关文章推荐
- java模拟从http上下载文件
- 网络丢包严重的解决办法
- 网络流(dinic)
- 升级 TCP 协议将使网速提升30%,中国受益尤为明显
- 运维小知识之nginx---..nginx-sticky-module-1.1ngx_http_sticky_misc.cIn function ‘ngx_http_sticky_misc_text_
- AsyncTask异步任务学习笔记(一、异步任务加载网络图片(多图))
- AngualrJS中每次$http请求时的一个遮罩层Directive
- Android 发送http请求实例
- LAMP环境由http转到https
- 解决android 6.0(api 23) SDK,不再提供org.apache.http.*
- TCP UDP HTTP
- 网络字节序和本地字节序的理解和实现
- SIM808 执行HTTP POST 例程【全网首发】
- TCP连接——爱的传声筒
- TCP/IP协议详解——邮差与邮局
- 图解TCP-IP协议
- HTTP协议是什么
- Win7+VMware Workstation环境下的CentOS-Linux网络连接设置
- Xcode7使用NSURLSession发送HTTP请求报错
- 网络加载糗事百科json数据实践