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