发表于: 2018-02-12 22:18:16

1 618


今天完成的任务

1.学习了一下Spring rmi相关内容。

在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露服务;

在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务。

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.对于分布式有了基本的概念。

2.会使用spring rmi搭简单demo。


遇到的问题


明天的计划

1.明天就回家,过年回来赶紧做完任务,进复盘。


任务8开始时间:2018.2.5

预计完成时间:2018.2.10

已经延期了,中间重构代码,顺带复习了之前的内容,年后再花大概 1星期完成任务8和9吧。

禅道:http://task.ptteng.com/zentao/project-task.html








返回列表 返回列表
评论

    分享到