发表于: 2017-05-28 23:26:18
1 1257
今天完成的事情:
昨天的例子依然是出错的,昨天的问题就是rmi的地址选择本机的ip和localhost都可以运行。将学员系统的service分离出来,

这是将学员系统中的service分离出来了,并且将mybatis的配置放进了spring-mvc.xml中

第二个service,我将分离出来的service项目复制一个,改名为service1,作为第二个service,将其中的服务端口以及服务名都改为service1,

以上是服务端,再就是客户端。

其中只有spring-mvc.xml文件有修改

再就是controller中的随机访问两台service中的一台,随机选择的放法是看师兄的日报学习的。。。代码如下:
@Controller
@RequestMapping("/Task8")
public class StudentController {
private Logger log = Logger.getLogger(StudentController.class);
//显示所有学生列表
@RequestMapping(value="/list",method =RequestMethod.GET)
public String showStudent(Model model){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mvc.xml");
// StudentService studentService1 = (StudentService) context.getBean("studentService");
log.info("查询所有用户信息");
int flag = Math.random()>0.5? 1:0;
System.out.println("产生随机数是:"+flag);
List<Student> studentList = null;
try {
switch (flag) {
case 1:
StudentService studentService = (StudentService) context.getBean("studentService");
System.out.println("第一层访问service");
studentList= studentService.getAllStudent();
break;
default:
StudentService studentService1 = (StudentService) context.getBean("studentService1");
System.out.println("第二层访问service1");
studentList = studentService1.getAllStudent();
break;
}
}catch (Exception e) {
switch (flag) {
case 1:
StudentService studentService1 = (StudentService) context.getBean("studentService1");
System.out.println("第一层访问失败,转到访问第二层service1");
studentList= studentService1.getAllStudent();
break;
default:
StudentService studentService = (StudentService) context.getBean("studentService");
System.out.println("第一层访问失败,转到访问第二层service");
studentList = studentService.getAllStudent();
break;
}
}
model.addAttribute("studentList",studentList);
return "list";
}
@RequestMapping("/Task8")
public class StudentController {
private Logger log = Logger.getLogger(StudentController.class);
//显示所有学生列表
@RequestMapping(value="/list",method =RequestMethod.GET)
public String showStudent(Model model){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mvc.xml");
// StudentService studentService1 = (StudentService) context.getBean("studentService");
log.info("查询所有用户信息");
int flag = Math.random()>0.5? 1:0;
System.out.println("产生随机数是:"+flag);
List<Student> studentList = null;
try {
switch (flag) {
case 1:
StudentService studentService = (StudentService) context.getBean("studentService");
System.out.println("第一层访问service");
studentList= studentService.getAllStudent();
break;
default:
StudentService studentService1 = (StudentService) context.getBean("studentService1");
System.out.println("第二层访问service1");
studentList = studentService1.getAllStudent();
break;
}
}catch (Exception e) {
switch (flag) {
case 1:
StudentService studentService1 = (StudentService) context.getBean("studentService1");
System.out.println("第一层访问失败,转到访问第二层service1");
studentList= studentService1.getAllStudent();
break;
default:
StudentService studentService = (StudentService) context.getBean("studentService");
System.out.println("第一层访问失败,转到访问第二层service");
studentList = studentService.getAllStudent();
break;
}
}
model.addAttribute("studentList",studentList);
return "list";
}
访问成功:


当其中一台service宕机了,让web正常运行:我将其中一台service的端口修改了,但是客户端的spring-mvc.xml中的端口没有修改,让客户端连接不到其中一台service,测试结果可以正常运行。
明天计划的事情:
由于没有服务器了,准备直接在本地测试部署两台WEB,通过Nginx配置两台WEB随机访问,两台WEB可以随机访问两台Service。如果顺利就能结束任务八。。
遇到的问题:
看师兄们的日报解决了。
收获:
在spring mvc中的rmi远程调用
评论