发表于: 2017-06-12 12:55:29
2 1172
今天完成的事情:
1、
rmi需要建立两个项目,一个是服务端的项目,一个是客户端的项目。
服务端项目启动后,再由之后启动的客户端项目去调用服务端的方法。
那么问题是,客户端需要调用的那个service类、在客户端自己的项目中需要有这么一个类文件吗?
期初我以为这两个项目完全是彼此独立的,谁也不依赖谁。
纠结了好一阵。请教师兄后认识到客户端是需要依赖服务端的。
明天计划的事情:
1、继续当前的。
遇到的问题:
1、难难难!没有头绪无从下手。
问题一:用Java代码的方式能使客户端访问服务端的service。使用spring的RmiServiceExporter发布RMI服务的时候,设置了registryHost属性,结果就报,拒绝连接:
Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
java代码方式:
①建立远程调用接口并声明方法,注意:这是双方通信的接口,需要继承java.rmi.Remote
②开发远上述程接口的实现类,注意:该实现类还必需继承java.rmi.server.UnicastRemoteObject
③注册并启动rmi server
④客户端查找并使用远程服务
服务端:
----------------------------------------------------------------------------------------
客户端:
StudentService studentService = (StudentService) Naming.lookup("rmi://localhost:7777/StudentService");
Student student = studentService.selectByPrimaryKey(Long.valueOf("17"));
System.out.println(student);
换成spring,
服务端:
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("RMI服务伴随Spring的启动而启动了.....");
客户端:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("rmi-client.xml");
StudentService studentService = (StudentService) context.getBean("studentService");
到目前为止,无论怎么做,用spring集成rmi分离service,都绕不开这个报错。
Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
收获:
1、各种压力,万一这个service明天还分离不成功咋办。
评论