发表于: 2017-06-03 23:26:27
1 1213
今天完成的事:
1、将任务9收尾了下(之前Tuscany虽分离的但并没有跑通)今天拿出来跑了跑看,查了下存在jar包不兼容问题
之前我将spring的版本从4.2.5降到3.0.5后顺便将mybatis的版本从3.2.8降到3.2.4然后前天调试报错无法创建bean StudentDao和StudnetService,今天重新跑将异常去百度查找后知道了mybatis版本低了后调回来就好了。
service与web用Tuscany分离后关键的几处代码如下
service端
<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="StuService" targetNamespace="http://StuService">
<component name="StuComponent">
<implementation.spring location="spring-tuscany.xml"/>
<service name="StuService1">
<tuscany:binding.rmi uri="https://127.0.0.1:8088/StuService1"/>
</service>
</component>
</composite>
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.jnshu.*"/>
<!--第一种方式:加载一个properties文件-->
<bean id="propertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClass}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${serName}"/>
<property name="password" value="${password}"/>
<!--初始化连接池大小-->
<property name="initialSize" value="${initialSize}"/>
<!--连接池最大数量-->
<property name="maxActive" value="${maxActive}"/>
<!--连接池最大空闲-->
<property name="maxIdle" value="${maxIdle}"/>
<!--连接池最小空闲-->
<property name="minIdle" value="${minIdle}"/>
<!--获取连接最大等待时间-->
<property name="maxWait" value="${maxWait}"/>
</bean>
<!--mybatis和spring完美整合,不需要mybatis的配置映射文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
</bean>
<!--DAO接口所在的包名,Spring会自动查找其下的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jnshu.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--<tx:annotation-driven/>-->
<!--(事物管理器)transaction manager,use JtaTransactionManager for global tx-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--(事务管理)transaction manager, use JtaTransactionManager for global tx-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
<?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="StuService1" type="com.jnshu.service.StudentService" target="StuService"/>
<bean id="StuService" name="StuServcie" class="com.jnshu.impl.StudentServiceImpl"/>
<import resource="spring-mybatis.xml"/>
</beans>
pom配置文件中Tuscany部分(pom文件中将spring依赖的版本改成3.0.5其他的配置保持任务8的不变)
<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>
用main方法执行下面代码发布服务
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-tuscany.xml");
Node node = NodeFactory.newInstance().createNode("Calculator.composite");
node.start();
System.out.println("service启动");
如果发布多个服务只需更改一组上面绿色标注的代码打包分别部署就可以了
web端代码(配置文件中将任务8中将连接 spring rmi的代码注释掉其他的不变)
下面是控制器的部分 这里也只是将之前加载context的部分和getbean的部分注释掉,代码逻辑结构都没有发生变化只是换上了Naming.lookup调用service的方法而已。
@RequestMapping("/showList")
public String showList(Model model,HttpServletRequest request,HttpServletResponse response) throws RemoteException, NotBoundException, MalformedURLException {
log.info("showList方法被调用");
List<Student> studentList = new ArrayList<Student>();
// studentList = studentService.getAllStudent();
int flag=Math.random() > 0.5 ? 1 :0;
try {
switch (flag){
case 1:
// ClassPathXmlApplicationContext context11 = new ClassPathXmlApplicationContext("spring-context01.xml");
// StudentService studentService11= (StudentService) context11.getBean("studentService");
StudentService studentService11= (StudentService) Naming.lookup("//localhost:8089/StuService2");
System.out.println("第一层调用service1====》//localhost:8089/StuService2");
studentList=studentService11.getAllStudent();
break;
default:
// ClassPathXmlApplicationContext context12 = new ClassPathXmlApplicationContext("spring-context02.xml");
// StudentService studentService12= (StudentService) context12.getBean("studentService01");
StudentService studentService12= (StudentService) Naming.lookup("//localhost:8088/StuService1");
System.out.println("第一层调用service2====》//localhost:8088/StuService1");
studentList=studentService12.getAllStudent();
break;
}
}catch (Exception e){
switch (flag){
case 1:
// ClassPathXmlApplicationContext context21 = new ClassPathXmlApplicationContext("spring-context02.xml");
// StudentService studentService21= (StudentService) context21.getBean("studentService01");
StudentService studentService21= (StudentService) Naming.lookup("//localhost:8088/StuService1");
System.out.println("第一层调用service1失败改调用service2====》//localhost:8088/StuService1");
studentList=studentService21.getAllStudent();
break;
default:
// ClassPathXmlApplicationContext context22 = new ClassPathXmlApplicationContext("spring-context01.xml");
// StudentService studentService22= (StudentService) context22.getBean("studentService");
StudentService studentService22= (StudentService) Naming.lookup("//localhost:8089/StuService2");
System.out.println("第一层调用service2失败改调用service1====>//localhost:8089/StuService2");
studentList=studentService22.getAllStudent();
break;
}
}
model.addAttribute("studentList", studentList);
return "showlist";
}
任务9Tuscany逻辑并不难,只是apache公司停止维护了,网络上的教程都是好几年前的,直接copy代码是会存在版本不匹配的问题,目前我了解到的是Tuscany2.0.1只支持spring3.1以下的版本。第二是编辑器的差异,网络上很多人都用的代码大部分是没有问题的,我自己将代码复制到idea中出现语法错误,但事实是代码并没有错,直接运行就可以如果将报错的部分改了反而不对了。第三,个人感觉Tuscany2和1区别还是蛮大的(配置方面差异不大)最起码,他们发布service的方法都不同,如果用Tuscany1的方法导Tuscany2的jar包就是不行。
2、去禅道下了萝卜多的原型图看,有很多地方还是存在疑问的静态数据和动态数据有些地方不是很确定
明天计划的事:公司组织集体出游
遇到的问题:
1、不知道复盘要做什么,需要准备哪些东西,复盘前面是否有一些必要的工作流程要走,
2、还有很多内容不熟(懂),真实项目的开发(项目规范约定),部署(如何发布项目,项目发布的规定流程,开发机中的约定)
收获:没感觉
总结:明天放松,整理状态,准备复盘。
评论