发表于: 2018-02-05 21:04:14
1 702
今天完成的事情:
1.RMI(remote method invocation)--远程方法调用。
要开发一个基于RMI的应用程序,必须创建四个主要的类:
- 远程对象接口
- 远程对象实现
- RMI服务器端
- RMI客户端
2.做了简单demo。
dao
public class User implements Serializable{
private String name;private int age;
。。。。
远程对象接口。
public interface IBaseService {
public String getHelloWord(String name);
public String getUser(User user);
}
远程对象实现。
public class BaseServiceImpl implements IBaseService {
public String getHelloWord(String name) {
return "欢迎"+ name + "的到来!!!";
}
public String getUser(User user) {
return "名字"+ user.getName()+ "-->"+"年龄:"+user.getAge();
}
}
RMI服务端
service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="baseRmiService" class="com.example.BaseServiceImpl" />
<bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 调用Service -->
<property name="service" ref="baseRmiService" />
<!-- value值是提供给客户端调用 -->
<property name="serviceName" value="baseService" />
<!-- service接口 -->
<property name="serviceInterface" value="com.example.IBaseService" />
<!-- 注册端口 -->
<property name="registryPort" value="1200" />
</bean>
</beans>
main
public class serviceTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("service.xml");
IBaseService ibs = (IBaseService) context.getBean("baseRmiService");
System.out.println("baseRmiService启动...");
}
RMI客户端。
client.xml
<bean id="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- baseService是调用服务端serviceName的value,1200是服务端注册的端口 -->
<property name="serviceUrl" value="rmi://localhost:1200/baseService" />
<!-- service接口 -->
<property name="serviceInterface" value="com.example.IBaseService" />
</bean>
main.
public class clientTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client.xml");
IBaseService ibs = (IBaseService) context.getBean("baseService");
System.out.println(ibs.getHelloWord("你"));
User user = new User();
user.setName("TOM");
user.setAge(24);
System.out.println(ibs.getUser(user));
}
}
service启动该后保持等待状态。
client连接
遇到的问题:
1.还没想好之前的系统怎么拆分。
收获:
1.对分布式有了基本的概念。
2.spring rmi的基本使用方法。
明天的计划:
1.继续理解分布式。
2.试着拆分之前的项目。
任务8开始时间:2018.2.5
预计完成时间:2018.2.10
禅道:http://task.ptteng.com/zentao/project-task.html
评论