上篇博客,介绍了RMI远程方法调用示例,通过示例,可以总结到,其实,也就是被调用则发布服务,称谓服务端,主动调用则是调用服务,称谓客户端。
既然是远程调用,因此,服务器端要提供发布的标签,让别人能够找到你这个服务对象,比如webservice,服务端要发布成wsdl地址,客户端通过wsdl可以找到此服务对象。服务器一旦发布服务标签,客户端只要根据此即可。
所以无论何种方式实现分布式调用,或使用何种协议,基本步骤或基本思想是想通的。
上篇博客使用普通的java项目,来发布或调用服务。我们也知道,硬编码这种方式,一旦改动则需重新编译部署,却不及xml热部署来的快。
这篇博客,则使用spring来管理rmi发布或调用服务配置信息,比如发布的地址或发布的类。
服务端:
接口部分:【普通接口】
package com.rmi.server;
import com.entity.Person;
public interface Server {
String helloWorld(String name);
Person getPerson(String name, int age) ;
}
实现部分:【普通实现】
package com.rmi.server;
import com.entity.Person;
//继承该类, UnicastRemoteObject,暴露远程服务
public class ServerImpl implements Server {
@Override
public String helloWorld(String name) {
// TODO Auto-generated method stub
return name + ",helloworld";
}
@Override
public Person getPerson(String name, int age){
// TODO Auto-generated method stub
return new Person(name, age);
}
}
若其中传递实体类,仍然需要实现序列化。
package com.entity;
import java.io.Serializable;
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
// 无参数的构造器
public Person() {
}
// 初始化全部属性的构造器
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
服务端spring配置文件:其中有相应的注释
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="testSpringRMI" class="com.rmi.server.ServerImpl"/>
<!-- 服务端 配置rmi服务发布 -->
<bean id="rmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 配置service -->
<property name="service" ref="testSpringRMI"></property>
<!-- 客户端使用的serviceName -->
<property name="serviceName" value="testrmiSpring"/>
<!-- 服务接口 -->
<property name="serviceInterface" value="com.rmi.server.Server"/>
<!-- 注册服务端口号,默认是1099 -->
<property name="registryPort" value="1099"/>
</bean>
</beans>
我们使用服务器启动,而不是手动加载配置文件,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>rmiTestServerSpring</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/config/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
ok,服务器启动后,则即可读取配置文件,然后发布服务。
则不用像第一篇博客中在main方法中注册端口以及发布jndi名称以及相应的类。
客户端同样使用spring文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 客户端 调用服务端rmi服务配置-->
<bean id="testrmiSpring" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!--调用服务端url以及serviceName -->
<property name="serviceUrl" value="rmi://:1099/testrmiSpring"/>
<!-- 调用服务端的接口 -->
<property name="serviceInterface" value="com.rmi.server.Server"/>
</bean>
</beans>
我们客户端使用java项目,而不是web项目,则直接加载配置文件如下:
package com.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.entity.Person;
import com.rmi.server.Server;
public class TestSpringClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
Server testrmiSpring = (Server) context.getBean("testrmiSpring");
System.out.println(testrmiSpring.helloWorld("测试rmi"));
Person person=testrmiSpring.getPerson("lhy", 22);
System.out.println(person.getName());
}
}
这样,就可以解决改动重新编译问题了,这就是xml的优点。其中rmi方法,jdk1.5以后,不需要使用rmi命令来生成stub和skeleton类,而是动态自动生成。
倘若服务端,不愿使用服务器,则可以向客户端如此手动加载配置文件,然后即可发布服务。