发表于: 2017-10-10 22:03:44

1 754


今天完成的事情:

在项目中整合rmi

用了两个server和两个client

思路和之前一样没什么区别,客户端只实现了一个student页面

controller

@Controller
public class Dogg {
private static Logger logger=Logger.getLogger(Dogg.class);
   //显示所有学生列表
   @RequestMapping(value="/home",method =RequestMethod.GET)
   public String showStudent(Model model) {
logger.info("查询所有用户信息");
       List<Student> studentList = null;
       double m = Math.random();
       int rr = (int) (m * 10);
       System.out.println("产生随机数是:" + rr);
       if (rr % 2 != 0) {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring/client1.xml");
               StudentService studentService = (StudentService) ctx.getBean("StudentService");
               studentList = studentService.studentAll();
               logger.info("访问server1");
           } catch (BeanCreationException e) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring/client2.xml");
               StudentService studentService = (StudentService) ctx.getBean("StudentService");
               studentList = studentService.studentAll();
               logger.info("访问server1失败,访问server2");
           }
} else {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring/client2.xml");
               StudentService studentService = (StudentService) ctx.getBean("StudentService");
               studentList = studentService.studentAll();
               logger.info("访问server2");
           } catch (BeanCreationException e) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring/client1.xml");
               StudentService studentService = (StudentService) ctx.getBean("StudentService");
               studentList = studentService.studentAll();
               logger.info("访问server2失败,访问server1");
           }
}model.addAttribute("studentList",studentList);
       return "list";

   }
}

两个server单开了两个项目

也很简单,这里调用了数据库,实体类要实现序列化

public class Student implements Serializable {
private static final long serialVersionUID = 1883838732853579826L;

SUID的作用就是确定在运行期间序列化和反序列化的类是一样的版本。如果序列化之后改了类,那么反序列化的时候就会抛异常

启动两个服务端

启动客户端

启动了客户端2,访问到了server2

关闭server2,刷新页面

随机了两次之后可以看到,再启动客户端2的时候连接server2失败了,就又去启动客户端1了

关闭server1

报异常了,无法连接

关于这两个端口

<!-- 为RMI服务端远程对象注册表设置端口号-->
<property name="registryPort" value="11010" />
<property name="servicePort" value="11010" />

registryPort是服务注册端口,这个必须要指定,默认使用1099。这个是客户端连接使用的端口

servicePort是服务的数据传输端口,该端口是真正服务端与客户端进行数据通信交互的端口。在注册端口发现有客户端连接后,进而后续分配的端口。默认为0表示匿名随机端口。

还准备了一波小课堂,明天讲


返回列表 返回列表
评论

    分享到