发表于: 2017-06-03 23:25:57
1 1236
今天完成的事情:
在师兄们的指导下,完成了任务九。
先贴pom.xml文件
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-base-runtime</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-spring-runtime</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId> org.apache.tuscany.sca</groupId >
<artifactId>tuscany-binding-rmi-runtime</artifactId>
<version> 2.0.1</version>
<scope> runtime</scope>
</dependency>
</dependencies>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-base-runtime</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-spring-runtime</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId> org.apache.tuscany.sca</groupId >
<artifactId>tuscany-binding-rmi-runtime</artifactId>
<version> 2.0.1</version>
<scope> runtime</scope>
</dependency>
</dependencies>
主要还是service端的配置文件。
spring-tuscany.xml:
<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sca="http://www.springframework.org/schema/sca"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/sca
http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd" >
<sca:service name="StudentService" type="com.jnshu.service.StudentService" target="StudentService"/>
<bean id="StudentService" name="StudentServcie" class="com.jnshu.service.StudentServiceImpl"/>
<import resource="spring-mybatis.xml"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sca="http://www.springframework.org/schema/sca"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/sca
http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd" >
<sca:service name="StudentService" type="com.jnshu.service.StudentService" target="StudentService"/>
<bean id="StudentService" name="StudentServcie" class="com.jnshu.service.StudentServiceImpl"/>
<import resource="spring-mybatis.xml"/>
</beans>
StudentService.composite:
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="StudentService" targetNamespace="http://StudentService">
<component name="StudentComponent">
<implementation.spring location="spring-tuscany.xml"/>
<service name="StudentService" >
<tuscany:binding.rmi uri="https://127.0.0.1:8088/StudentService"/>
</service>
</component>
</composite>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="StudentService" targetNamespace="http://StudentService">
<component name="StudentComponent">
<implementation.spring location="spring-tuscany.xml"/>
<service name="StudentService" >
<tuscany:binding.rmi uri="https://127.0.0.1:8088/StudentService"/>
</service>
</component>
</composite>
其中implementation.spring 和 tuscany:binding.rmi 会爆红,但是这不影响,不报红可能就是有问题的。不敢相信。
service端的测试类:
public class Test {
public static void main(String[] args) throws IOException, NotBoundException {
Node node = NodeFactory.newInstance().createNode("StudentService.composite");
node.start();
System.out.println("service启动");
StudentService studentService = (StudentService) Naming.lookup("//127.0.0.1:8088/StudentService");
System.out.println("查询所有用户信息");
List<Student> studentList =studentService.getAllStudent();
System.out.println(studentList);
}
public static void main(String[] args) throws IOException, NotBoundException {
Node node = NodeFactory.newInstance().createNode("StudentService.composite");
node.start();
System.out.println("service启动");
StudentService studentService = (StudentService) Naming.lookup("//127.0.0.1:8088/StudentService");
System.out.println("查询所有用户信息");
List<Student> studentList =studentService.getAllStudent();
System.out.println(studentList);
}
}
设置的端口可能会被占用,占用报错是
再就是Client客户端的Comtroller:
@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";
}
@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";
}
其中需要注意的是,他不需要service测试类中的
Node node = NodeFactory.newInstance().createNode("StudentService.composite");
node.start();
System.out.println("service启动");
node.start();
System.out.println("service启动");
加上就会报错,

上面的service启动属于service端的。在客户端是不需要的,这是我犯过的错,应该不会有其他人犯这个错误了。
相对与任务吧来讲客户端没有什么别的。
明天计划的事情:
准备进入复盘。去wiki浏览要进项目的资料。
遇到的问题:
配置文件的问题,上面提到过。
收获:
今天结束了任务九,其实做完了,发现代码不难,但是男的是配置,而且网上相关tuscany的例子真的是很少,还没有我们官网的日报详细。后面做任务的师弟可以对象这我们成功的例子操作。能比较顺利的爬出坑,还是要感谢前面踩坑的师兄。给他们点个赞。。
评论