spring发布RMI服务(-)
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.
spring发布RMI服务(-)的更多相关文章
- 【Java Web开发学习】Spring发布RMI服务
[Java 远程服务]Spring发布RMI服务 转载:https://www.cnblogs.com/yangchongxing/p/9084066.html RmiServiceExporter可 ...
- cxf整合spring发布rest服务 httpclient访问服务
1.创建maven web项目并添加依赖 pom.xml <properties> <webVersion>3.0</webVersion> <cxf.ver ...
- 应用Spring MVC发布restful服务是怎样的一种体验
摘要:“约定优于配置”这是一个相当棒的经验,SOAP服务性能差.基于配置.紧耦合,restful服务性能好.基于约定.松耦合,现在我就把使用Spring MVC发布restful服务的 ...
- Spring整合CXF之发布WebService服务
今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...
- 在spring boot微服务中使用JWS发布webService
发布时间:2018-11-22 技术:Java+spring+maven 概述 在springboot微服务中使用JWS发布webService,在服务启动时自动发布webservice接口. ...
- (七)CXF之与spring整合发布web服务
一.需求分析 用spring发布服务 二.案例 2.1 引入maven依赖 <dependencies> <!-- 添加Spring支持 --> <dependency& ...
- [译]Spring构建微服务
此文为译文,原文地址 介绍 本文通过一个使用Spring.Spring Boot和Spring Cloud的小例子来说明如何构建微服务系统. 我们可以通过数个微服务组合成一个大型系统. 我们可以想象下 ...
- Dubbo和Spring Cloud微服务架构'
微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...
- Dubbo和Spring Cloud微服务架构比较
Dubbo 出生于阿里系,是阿里巴巴服务化治理的核心框架,并被广泛应用于中国各互联网公司:只需要通过 Spring 配置的方式即可完成服务化,对于应用无入侵,设计的目的还是服务于自身的业务为主. 微服 ...
随机推荐
- app开发制作会难吗?app开发好学吗?
前面我们讲到了app是什么,APP是运行在智能手机的第三方应用程序,可以满足用户的不同需求.那么app开发制作会难吗?这个与产品的复杂度有很大的关系,复杂度包括业务逻辑多不多,业务模块多不多等,对于玩 ...
- Java基础—类和对象
基本概念 对象:对象是类的一个实例,有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴.叫.吃等. 类:类是具有相同属性和方法的一组对象的集合,它为属于该类的所有对象提 ...
- Eclipse中tomcat设置
首先在Eclipse中新建一个tomcat的server,这里就不多说了. 新建完成后,双击新建的server,如下: 设置1:在Server Locations里,选择Use Tomcat inst ...
- 2.5 使用ARDUINO做主控,手机发送短信控制开关LED
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- Eclipse Find/Replace
1.Eclipse内容助手 选中Regular expressions,使用正则表达式进行匹配.图中出现了小黄灯,Ctrl+Space显示出帮助信息. 2.Wrap search(循环检索)选中后,检 ...
- Python操作——Redi
redis是一个key-value存储系统. 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(列表).hash(哈希).set(集合).zset(有 ...
- vimium的使用介绍和基本用法
vimium是chrome浏览器的一个插件,fq去chrome应用商店搜索vimium,下载安装 纯键盘操作,脱离了鼠标,提高效率 核心是f,安装好vimium后只需要按f,输入对应的编号就能进入相应 ...
- SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...
- 【TopCoder】SRM152 DIV2总结
为什么平常刷的时候感觉还不错,比赛的时候只能做出来一道题=.= 250分题:大水题,根据题目规则把一个字符串翻译成数字,直接代码:GitHub 我是通过遍历一个个数出来的,看到大神的解法是把字符用‘- ...
- 20145231 《Java程序设计》第一周学习总结
20145231 <Java程序设计>第一周学习总结 教材学习内容总结 Java三大平台Java SE,Java EE,Java ME.其中,Java SE是我们学习的基础. Java S ...