spring应用RMI暴露服务

spring使用RMI暴露服务

(1)定义接口:

Java代码 spring应用RMI暴露服务
  1. package com.logcd.spring.rmi;   
  2.   
  3. public interface HelloService {   
  4.     public String doHello(String name);   
  5. }  
package com.logcd.spring.rmi;

public interface HelloService {
	public String doHello(String name);
}


(2)接口实现:

Java代码 spring应用RMI暴露服务
  1. package com.logcd.spring.rmi;   
  2.   
  3. public  class HelloServiceImpl implements HelloService{   
  4.   
  5.     public String doHello(String name) {   
  6.         return "Hello , " + name;   
  7.     }   
  8.   
  9. }  
package com.logcd.spring.rmi;

public  class HelloServiceImpl implements HelloService{

	public String doHello(String name) {
		return "Hello , " + name;
	}

}


(3)rmi-server.xml

Java代码 spring应用RMI暴露服务
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"    
  3.   "http://www.springframework.org/dtd/spring-beans.dtd">    
  4.   
  5. <beans>    
  6.     <bean id="helloService" class="com.logcd.spring.rmi.HelloServiceImpl"/>   
  7.   
  8.     <!--RmiServiceExporter显示地支持使用RMI调用器暴露任何非RMI服务-->   
  9.     <bean id="serviceExporter"    
  10. class="org.springframework.remoting.rmi.RmiServiceExporter">   
  11.   
  12.         <property name="service" ref="helloService"/>   
  13.         <property name="serviceInterface"  
  14.            value="com.logcd.spring.rmi.HelloService"/>   
  15.         <!--定义要暴露的服务名可以与输出的bean不同名,客户端通过这个名字来调用服务-->   
  16.         <property name="serviceName" value ="HelloService"/>   
  17.         <!--覆盖RMI注册端口号(1099),通常应用服务器也会维护RMI注册,最好不要冲突-->   
  18.         <property name="registryPort" value="1199"/>   
  19.     </bean>   
  20.        
  21. </beans>  
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 
    <bean id="helloService" class="com.logcd.spring.rmi.HelloServiceImpl"/>

	<!--RmiServiceExporter显示地支持使用RMI调用器暴露任何非RMI服务-->
    <bean id="serviceExporter" 
class="org.springframework.remoting.rmi.RmiServiceExporter">

        <property name="service" ref="helloService"/>
        <property name="serviceInterface"
           value="com.logcd.spring.rmi.HelloService"/>
        <!--定义要暴露的服务名可以与输出的bean不同名,客户端通过这个名字来调用服务-->
        <property name="serviceName" value ="HelloService"/>
        <!--覆盖RMI注册端口号(1099),通常应用服务器也会维护RMI注册,最好不要冲突-->
        <property name="registryPort" value="1199"/>
    </bean>
    
</beans>


(4)rmi-client.xml

Java代码 spring应用RMI暴露服务
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"  
  3.    "http://www.springframework.org/dtd/spring-beans.dtd">   
  4. <beans>   
  5.     <!--使用RmiProxyFactoryBean连接服务端-->   
  6.      <bean id="serviceProxy"    
  7.                class="org.springframework.remoting.rmi.RmiProxyFactoryBean">   
  8.   
  9.         <property name="serviceUrl"  
  10.                     value="rmi://localhost:1199/HelloService"/>    
  11.         <property name="serviceInterface"  
  12.                   value="com.logcd.spring.rmi.HelloService"/>   
  13.      </bean>   
  14. </beans>  
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!--使用RmiProxyFactoryBean连接服务端-->
     <bean id="serviceProxy" 
               class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

        <property name="serviceUrl"
	                value="rmi://localhost:1199/HelloService"/> 
        <property name="serviceInterface"
                  value="com.logcd.spring.rmi.HelloService"/>
     </bean>
</beans>


(5)测试

Java代码 spring应用RMI暴露服务
  1. package com.logcd.spring.rmi;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  5.   
  6. public class TestSpringRMI {   
  7.        
  8.     public static void main(String args[]){   
  9.        ApplicationContext context= new ClassPathXmlApplicationContext(   
  10.                 new String[]{"rmi-server.xml","rmi-client.xml"});    
  11.           
  12.        HelloService service = (HelloService)context.getBean("serviceProxy");   
  13.           
  14.        System.out.println(service.doHello("logcd"));   
  15.     }   
  16.        
  17. }  
1 楼 yin_bp 2010-08-24  
楼主可以看看bbossgroups中使用rmi的方式哦:使用rmi协议,基于cglib实现组件管理和远程方法调用

bbossgroups项目中提供的一套非常简洁但是功能却很丰富的aop框架,本文介绍如何使用使用rmi协议、基于cglib实现组件管理和远程方法调用。
组件配置org/frameworkset/spi/cglib/service-bean-assemble.xml
<properties>
	<property id="cglibbean" singlable="true"  class="org.frameworkset.spi.cglib.CGLibService" />
</properties>


组件实现类:
package org.frameworkset.spi.cglib;


/**
 * <p>Title: CGLibService.java</p> 
 * <p>Description: </p>
 * <p>bboss workgroup</p>
 * <p>Copyright (c) 2007</p>
 * @Date 2010-6-21 上午10:31:54
 * @author biaoping.yin
 * @version 1.0
 */
public class CGLibService {
	
	public String sayhello(String name)
	{
		System.out.println("remote from "+ name);
		return "Hello," + name;
	}

}


测试客服端

package org.frameworkset.spi.cglib;

import org.frameworkset.spi.ApplicationContext;
import org.junit.Test;

/**
 * <p>Title: CGLibTest.java</p> 
 * <p>Description: </p>
 * <p>bboss workgroup</p>
 * <p>Copyright (c) 2007</p>
 * @Date 2010-6-21 上午10:30:57
 * @author biaoping.yin
 * @version 1.0
 */
public class CGLibTest {
	static ApplicationContext context_provider = ApplicationContext.getApplicationContext("org/frameworkset/spi/cglib/service-bean-assemble.xml");
	@Test
	public void test()
	{
		//远程调用
		CGLibService service = (CGLibService)context_provider.getBeanObject("(rmi::172.16.17.216:1099)/cglibbean");
		System.out.println(service.sayhello("多多"));
	}
	
	
	@Test
	public void localtest()
	{
		//本地调用
		CGLibService service = (CGLibService)context_provider.getBeanObject("cglibbean");
		System.out.println(service.sayhello("多多"));
	}

}



可到sourceforge下载最新版本bbossgroups-2.0-RC1,下载地址:
http://sourceforge.net/projects/bboss/files/
2 楼 yin_bp 2011-05-19  
bbossgroups 3.2添加了直接发布rmi服务的方法,详情可查看iteye上bbossgroups专栏文章《bbossgroups 3.2 中rmi组件服务发布和rmi客服端获取方法》:
http://www.iteye.com/wiki/bbossgroups/3089-bbossgroups-aop-rmi