发表于: 2017-08-23 16:46:26
3 1208
来总结一波任务9
首先,任务9是在任务8的基础上添加和更改一些东西
任务8详情在上一个总结任务日报里,这里先简单贴一下:
项目表面结构,与任务8没什么不同
hello-core底层module里保持不变,依旧是model和service
然后是hello-service,我一般称为中层,这层一般不怎么改动,就是添加了配置文件spring-tuscany.xml和StudentService.composite
其中StudentService.composite文件需要注意的是,这个地方爆红反而是正确的,如果通过其他方法修改成不爆红的话反而会错,原理不太清楚,可能因为tuscany放弃治疗(维护)后产生的一些问题吧
pom文件里需要把spring版本改成3.0.5RELEASE,我之前的版本是4.3.5,人tuscany不支持.
还有就是添加下列依赖
即可使用,我之前还自己把jar包部署到本地仓库,然后自己起名自己调用...其实都不必这么做,直接依赖就行
然后还有两个test运行文件,分别开启rmi服务和tuscany.
在运行tomcat之前需要启动这两个服务
至此,hello-service其他配置文件与task8相同
然后是hello-web,这是开发人员修改得最频繁的部分了,里面包含了控制器,spring-mvc,以及webapp
task9的控制器如下:
package com.ljm.controller;
import com.ljm.model.Student;
import com.ljm.service.StudentService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;
/**
* Created by liujm on 2017/8/22.
*/
@Controller
@RequestMapping("/Task9")
public class StudentController {
private Logger log = Logger.getLogger(StudentController.class);
//显示所有学生列表
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String showStudent(Model model) throws RemoteException, NotBoundException, MalformedURLException {
//
// StudentService studentService = (StudentService) Naming.lookup("//127.0.0.1:8088/StudentService");
// log.info("查询所有用户信息");
// List<Student> studentList =studentService.getAllStudent();
int flag = Math.random() > 0.5 ? 1 : 0;
System.out.println("产生随机数是:" + flag);
List<Student> studentList = null;
try {
switch (flag) {
case 1:
StudentService studentService = (StudentService) Naming.lookup("//127.0.0.1:8088/StudentService");
System.out.println("第一层访问service");
studentList = studentService.getAllStudent();
break;
default:
StudentService studentService1 = (StudentService) Naming.lookup("//127.0.0.1:8089/StudentService1");
System.out.println("第二层访问service1");
studentList = studentService1.getAllStudent();
break;
}
} catch (Exception e) {
switch (flag) {
case 1:
StudentService studentService1 = (StudentService) Naming.lookup("//127.0.0.1:8088/StudentService");
System.out.println("第一层访问失败,转到访问第二层service1");
studentList = studentService1.getAllStudent();
break;
default:
StudentService studentService = (StudentService) Naming.lookup("//127.0.0.1:8089/StudentService1");
System.out.println("第一层访问失败,转到访问第二层service");
studentList = studentService.getAllStudent();
break;
}
}
model.addAttribute("studentList", studentList);
return "list";
}
}
启动服务后运行tomcat就能用了
详情已上传svn,为task9(plus)
评论