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;
    }
    
}
View Code

 

 

客户端

客户端也采用一个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.

 

posted on 2017-11-13 12:55  罗烨鑫  阅读(411)  评论(0编辑  收藏  举报

导航