spring发布RMI服务

最近交流了一个项目,需要从RMI、WebService、接口文件中采集数据到大数据平台,下面自己测试了通过Spring发布RMI服务。

说明:RMI服务要求服务端和客户端都是Java代码的,两端不能采用异构的语言开发。

服务端

1、编写服务接口和实现

package com.yaa.web.interfaces.rmi.springRMI;

public interface HelloRMIService {
  public int getAdd(int a, int b);
}
package com.yaa.web.interfaces.rmi.springRMI;

public class HelloRMIServiceImpl implements HelloRMIService {

  @Override
  public int getAdd(int a, int b) {     return a+b;   }

2、RMI通过spring发布:

服务端采用java注册方式将RMI服务接口交由spring容器管理。在MVCconfig的配置如下: 

//发布一个RMI服务
@Bean
public HelloRMIServiceImpl helloRMIServiceImpl() throws IOException{
  HelloRMIServiceImpl hello = new HelloRMIServiceImpl();
  return hello;
}
@Bean
public RmiServiceExporter rmiServiceExporter() throws IOException{
  RmiServiceExporter rmi = new RmiServiceExporter();
  rmi.setService(helloRMIServiceImpl());//实现类
  rmi.setServiceName("helloRMI");//服务名
  rmi.setServiceInterface(HelloRMIService.class);//接口类
  rmi.setRegistryPort(8889);//发布的端口,这里默认使用本机的localhost 或 127.0.0.1.
  return rmi;
}

完成后即运行服务端Web服务。

MVCconfig是spring容器的初始化类(替换了spring-servlet.xml,零配置xml),实现如下:

@Configuration
@EnableWebMvc //启动spring MVC容器,相当于 xml中的 <mvc:annotation-driven/>
@ComponentScan(basePackages = "com.yaa")
@ImportResource(locations={"classpath:META-INF/cxf/cxf.xml","classpath:META-INF/cxf/cxf-servlet.xml"})
public class MVCConfig extends WebMvcConfigurerAdapter{
@Autowired
RoleToUserProfileConverter roleToUserProfileConverter; @Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(roleToUserProfileConverter);
} @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp"); return viewResolver;
} /*
* 处理静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/"); } @Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
} //发布一个RMI服务
@Bean
public HelloRMIServiceImpl helloRMIServiceImpl() throws IOException{
HelloRMIServiceImpl hello = new HelloRMIServiceImpl();
return hello;
}
@Bean
public RmiServiceExporter rmiServiceExporter() throws IOException{
RmiServiceExporter rmi = new RmiServiceExporter();
rmi.setService(helloRMIServiceImpl());
rmi.setServiceName("helloRMI");
rmi.setServiceInterface(HelloRMIService.class);
rmi.setRegistryPort(8889);
return rmi;
} }

客户端

客户端也采用一个spring的Web服务,

1、配置Bean

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 客户端 -->
<bean id="myRMIClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface" value="com.yaa.web.interfaces.rmi.springRMI.HelloRMIService"></property>
<property name="serviceUrl" value="rmi://127.0.0.1:8889/helloRMI"></property>
</bean> </beans>

2、编写一个带Main的类访问远程服务

public class HelloClientSpring {

    public static void main(String[] args){

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/Test/rmiClient.xml");
HelloRMIService helloRMIService = applicationContext.getBean("myRMIClient",HelloRMIService.class);
System.out.println(helloRMIService.getAdd(8, 4)); if (applicationContext != null)
applicationContext = null;
}
}

注意:

ClassPathXmlApplicationContext("com/Test/rmiClient.xml");中的com/Test/rmiClient.xml文件位置根据自己rmiClient.xml文件的具体路径填写。否则报找不到。

完成后以Application方式运行   HelloClientSpring类,即可看到 8+4 的结果是12.

最新文章

  1. MySQL中CURRENT_TIMESTAMP(转)
  2. vert.x学习(三),Web开发之Thymeleaf模板的使用
  3. 微信小程序-登陆、支付、模板消息
  4. javascript网址收集
  5. 【转载】解决Windows 10 局域网内共享的问题
  6. [c#]exchange回复,全部回复,转发所遇到的问题
  7. 又来了,SDE非直连
  8. Android 编译Settings、Mms等模块,并Push到手机中安装失败
  9. Redis - sort set类型操作
  10. Spring和SpringMVC的区别
  11. HDU 3342 Legal or Not (图是否有环)
  12. hadoop mapreduce 优化
  13. Codeforces Round #258 (Div. 2)[ABCD]
  14. Html辅助方法 之 Form表单标签
  15. uva 10304 - Optimal Binary Search Tree 区间dp
  16. MongoDB系列之三(副本集配置)
  17. 【c语言】统计一个数二进制中的1的个数
  18. springboot情操陶冶-web配置(二)
  19. ubuntu root 密码是随机的! root权限下设置共享文件夹
  20. ON_UPDATE_COMMAND_UI和ON_COMMAND有什么区别?

热门文章

  1. Linux中命令查找顺序
  2. 在一台服务器上搭建多个网站的方法(Apache版)
  3. IIS 6.0上部署.NET 4.0网站
  4. 剑指offer 面试45题
  5. PAT 天梯赛 L1-031. 到底是不是太胖了 【水】
  6. Swing 添加超链接 打开页面
  7. 【Head First Servlets and JSP】笔记9:属性的作用域、线程安全
  8. 个人对于css sprite的一点点见解
  9. Python编程-多态、封装、特性
  10. 自己的第一个MapReduce程序