一 前言 (转载自http://www.iteye.com/topic/908016)
工作环境中,大部分功能,都是基于服务(SOA模式)的,因此用到了rmi.使用他的目的就是让分布式的开发变的简单,同时提高了项目的扩展性,可维护性,可读性.
rmi有很多种,例如:
1)远程方法调用(RMI)
2)Spring自己的HTTP invoker
3)EJB
4)Web Services
......
本文选择了spring的rmi接口进行分布式开发应用.具体用哪种rmi,我们还需要深入的挖掘.本文不作讨论.
至于具体哪里简单呢?需要朋友们深入了解java的远程方法调用(RMI).服务端的接口要继承java.rmi.Remote接口.启动类需要继承java.rmi.server.UnicastRemoteObject类.而且诸如服务器地址等配置信息,也不应该放在代码中,因此spring的rmi模式就应运而生了.
二 环境(javaSE项目)
jdk1.6.0.23
eclipse3.6.1_javaee
三 包
1)日志管理
commons-logging-1.1.1.jar
log4j-1.2.16.jar
2)spring核心包
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
3)aop支持
aopalliance.jar
org.springframework.aop-3.0.5.RELEASE.jar
四 spring基础配置
1)log4j的配置(src/log4j.properties),推荐配置;
log4j.rootLogger=DEBUG,appender1
# org.springframework包下面所有的日志输出的级别设为DEBUG
log4j.logger.org.springframework=INFO
# 控制台输出
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n
# 立即输出
log4j.appender.appender1.immediateFlush=true
2)commons-logging的配置(src/commons-logging.properties):这里是选配的,不配也不会报错,只是为了方便以后日志系统的切换;
3)spring的配置(src/applicationContext.xml),主要是头文件,spring3对头文件的要求很严格;
五 服务端配置与开发
1)开发服务的接口
package com.baidu.phl.rmi.service;
public interface HelloRmiService {
public int getAdd(int a, int b);
}
2)开发服务的实现类
package com.baidu.phl.rmi.service.impl;
import com.baidu.phl.rmi.service.HelloRmiService;
public class HelloRmiServiceImpl implements HelloRmiService {
public int getAdd(int a, int b) {
return a + b;
}
}
3)配置spring,配置实现类
4)配置spring,注册rmi服务,其中需要说明
a.远程调用服务名
b.提供服务的实现类
c.提供服务的接口
d.提供服务的端口
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--服务端-->
<bean id="helloRmiServiceImpl" class="com.baidu.phl.rmi.service.impl.HelloRmiServiceImpl" scope="prototype"/>
<!-- 将类为一个RMI服务 -->
<bean id="myRmi" class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 服务类 -->
<property name="service" ref="helloRmiServiceImpl" />
<!-- 服务名 -->
<property name="serviceName" value="helloRmi" />
<!-- 服务接口 -->
<property name="serviceInterface" value="com.baidu.phl.rmi.service.HelloRmiService" />
<!-- 服务端口 -->
<property name="registryPort" value="9999" />
<!-- 其他属性自己查看org.springframework.remoting.rmi.RmiServiceExporter的类,就知道支持的属性了-->
</bean>
</beans>
六 客户端配置与开发
1)将服务端的接口进行打包(注:不需要实现类,只是客户端访问服务端的一个存根)
2)配置spring
a.指定访问服务的地址
b.指定服务的接口
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--客户端-->
<bean id="myClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:9999/helloRmi"/>
<property name="serviceInterface" value="com.baidu.phl.rmi.service.HelloRmiService"/>
</bean>
</beans>
七 测试
1(服务端测试代码
package com.baidu.phl.rmi.service.test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServerTest {
private static Log log = LogFactory.getLog(ServerTest.class);
public static void main(String[] args) {
//初始化工作只能运行一次;运行多次的话,会启动多个服务
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
log.info("hello commons-logging!");
}
}
2(客户端测试代码
package com.baidu.phl.rmi.service.test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baidu.phl.rmi.service.HelloRmiService;
public class ClientTest {
private static Log log = LogFactory.getLog(ClientTest.class);
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
HelloRmiService hms = context.getBean("myClient", HelloRmiService.class);
System.out.println("远程调用返回结果为:"+hms.getAdd(1, 2));
log.info("远程调用返回结果为:"+hms.getAdd(1, 2));
}
}
八 总结
1)接口与类就是java最简单的结构.与RMI无关;
2)客户端必须要有实现类的接口(存根),这样才能访问后实现类的转型,引用与方法调用;
3)本文将服务端和客户端写到了一个工程里,朋友们可以自己将起分离开来使用,但需要注意修改IP地址和开放防火墙相应端口;
4)服务端和客户端都需要使用spring才可以进行rmi的相关开发;