发表于: 2017-08-23 21:36:48
2 951
今天完成的事情:理顺rmi的基本概念,毕竟距离上次写这个过了大半个月了,代码编写。
首先新建一个maven聚合项目
聚合项目给我的感觉就是一个项目拆分成多个模块然后再让他们联系在一起
在项目里建立module,分为core(核心),service(服务),web(客户端)
在core中写model和service,在service中写dao和serviceImpl,在web模块中写controller,报红是因为尝试使用springboot来简化配置文件,现在看到很多的配置文件就不爽,没使用springboot版的正常,明天在尝试一下用springboot把它解决掉。
具体的业务代码就不贴了,和我们之前的任务没区别,重要的是配置文件。
service模块中的spring-mybatis.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:context="http://www.springframework.org/schema/context" 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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--自动扫描-->
<context:component-scan base-package="com.wyc"/>
<!--第一种方式:加载一个properties文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="jdbcUrl" 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"/>
<!--自动扫描mapping.xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--DAO接口所在包名,spring会自动查找其下的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wyc.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--ref为查找当前配置里的bean,即可以查找其他xml配置文件的bean,优先从当前配置文件寻找-->
<!--(事务管理)transaction manager,user JtaTransactionManager for global tx-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--<bean id="studentService" claStudentServiceImplImpl"/>-->
<!--spring RMI service(部分) 开放服务和端口-->
<bean id="studentService" class="com.wyc.service.UserServiceImpl"/>
<bean id="serviceInterface" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="studentService"></property>
<!-- 配置服务名称-->
<property name="serviceName" value="studentService"></property>
<!-- 配置服务接口-->
<property name="serviceInterface" value="com.wyc.service.UserService"></property>
<!-- 配置服务端口-->
<property name="registryPort" value="9090"></property>
</bean>
<!--第二个服务端-->
<bean id="studentService1" class="com.wyc.service.UserServiceImpl"/>
<bean id="serviceInterface1" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="studentService1"/>
<!-- 配置服务名称-->
<property name="serviceName" value="userService1"></property>
<!-- 配置服务接口-->
<property name="serviceInterface" value="com.wyc.service.UserService"></property>
<!-- 配置服务端口-->
<property name="registryPort" value="9091"></property>
</bean>
</beans>
web模块中的spring-mvc.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:context="http://www.springframework.org/schema/context"
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 ">
<!--明天要写控制器就解开这个注释,并且先把项目上传svn-->
<context:component-scan base-package="com.wyc.*"/>
<bean id="StudentService" class= "org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:9090/userService"></property>
<property name="serviceInterface" value="com.wyc.service.UserService"></property>
<!--连接出错时自动重连-->
<property name="refreshStubOnConnectFailure" value="true"></property>
</bean>
<bean id="StudentService1" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:9091/userService1"></property>
<property name="serviceInterface" value="com.wyc.service.UserService"></property>
<!--连接出错时自动重连-->
<property name="refreshStubOnConnectFailure" value="true"></property>
<!--不在容器启动的时候创建server端的连接-->
<property name="lookupStubOnStartup" value="false"></property>
</bean>
<!--定义跳转的文件的前后缀,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web模块中的controller
@Controller
public class Controller {
private Logger log= Logger.getLogger(NewController.class);
@RequestMapping(value="list",method= RequestMethod.GET)
public String showStudent(Model model){
log.info("查询所有用户信息");
//Math.random()是令系统随机选取大于等于0.0且小于1.0的伪随机double值
//下面代码意思是如果大于0.5就赋值1,否则赋值0
int flag=Math.random()>0.5?1:0;
System.out.println("产生随机数是:"+flag);
List<User>studentList=null;
try{
switch (flag) {
case 1:
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-mvc.xml");
UserService studentService = (UserService) context.getBean("UserService");
System.out.println("第一层访问service");
studentList = studentService.getAllUser();
break;
default:
ClassPathXmlApplicationContext context1=new ClassPathXmlApplicationContext("spring-mvc.xml");
UserService studentService1 = (UserService) context1.getBean("UserService1");
System.out.println("第二层访问service1");
studentList = studentService1.getAllUser();
break;
}
}catch (Exception e){
switch (flag) {
case 1:
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-mvc.xml");
UserService studentService = (UserService) context.getBean("UserService1");
System.out.println("第一层访问service");
studentList = studentService.getAllUser();
break;
default:
ClassPathXmlApplicationContext context1=new ClassPathXmlApplicationContext("spring-mvc.xml");
UserService studentService1 = (UserService) context1.getBean("UserService");
System.out.println("第二层访问service1");
studentList = studentService1.getAllUser();
break;
}
}
model.addAttribute("studentList",studentList);
return "list";
}
}
明天计划的事情:尝试使用springboot简化配置,nginx负载均衡
遇到的问题:网上资料少
收获:熟悉RMI
评论