发表于: 2017-05-23 23:30:04
1 1151
今天完成的事:
换一种思路接着搞昨天没搞完的事
昨天是将service部分放到服务器上没成功(网上说是Linux对localhost的地址解析与Windows不一样造成的,几种方法试过了都不灵)今天于是掉个个儿我把service放在本地,将web放在服务器上连接测试,service暴露的IP为本地电脑的ipv4 地址但是结果不成功异常如下:

没有找出什么原因(也没太认真找。因为任务中还有两项功能没实现,部署多台service,web能随机访问,当其中一台挂掉了web可以自动连接另外一台)
部署两台service我的做法是rmi暴露服务的配置不一样 其他的都相同:(如下)
<bean id="studentService01" class="com.jnshu.impl.StudentServiceImpl"/>
<bean id="baseServiceExporter1" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="studentService01"></property>
<!-- 配置服务名称-->
<property name="serviceName" value="studentService01"></property>
<!-- 配置服务接口-->
<property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
<!-- 配置服务端口-->
<property name="registryPort" value="8888"></property>
</bean>
<bean id="studentService" class="com.jnshu.impl.StudentServiceImpl"/>
<bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="studentService"></property>
<!-- 配置服务名称-->
<property name="serviceName" value="studentService"></property>
<!-- 配置服务接口-->
<property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
<!-- 配置服务端口-->
<property name="registryPort" value="7777"></property>
</bean>
然后分别打包(此处我只会打jar包或war包)

用两个cmd命令行运行:

两个service启动是没问题的,web的端的配置如下:连接也没问题
<bean id="studentService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:7777/studentService"></property>
<property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
</bean>
<bean id="studentService01" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:8888/studentService01"></property>
<property name="serviceInterface" value="com.jnshu.service.StudentService"></property>
</bean>
但是我将其中一个service挂掉后 web端访问就有问题加载配置文件就报错(这里曾闪过一个念头将两个分开配,放在两个配置文件里,然后分别加载context,那个出错了就换另外一个,后面觉得挺麻烦就没做)然后就去百度看到这篇文章(http://www.360doc.com/content/15/0615/23/46383_478397641.shtml)
将上面红色部分的属性加上后main方法测试加载context果然不报错了,但是get出来的service bean去执行操作数据库的方法时还是会错(此处知道会错,因为我关了对应的service)后通过try catch捕获后 执行get另外一个service 成功(刚开始我还以为随机访问service的功能差不多了)但是 当我重新打开之前关闭的service时,web端我get service不报错,但用service去执行数据库的操作时出异常。

(想来应该只有上面博客里边说的stub文件加载的问题吧)
想来想去最终还是决定将rmi web端get service 的配置文件分开(虽然心里觉得这种做法应该不大对,但是目前想不到其他的办法)
最终测试没有问题
这里service的调度转换我是这样写的(只是今天测试用的,没有随机性在里头,任务中要求的随机访问,应该是要生成随机数来做的)
public class getService {
public static StudentService getStudentService(){
// ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context01.xml");
if (getService02()!=null){
return getService02();
}else if (getService01()!=null){
return getService01();
}
return null;
}
public static StudentService getService01(){
StudentService studentService=null;
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context01.xml");
studentService= (StudentService) context.getBean("studentService");
System.out.println("==========连接1方法被调用==连接成功");;
// return studentService;
} catch (Exception e) {
studentService=null;
System.out.println("Service01连接失败!");
}finally {
return studentService;
}
}
public static StudentService getService02(){
StudentService studentService=null;
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context02.xml");
studentService= (StudentService) context.getBean("studentService01");
System.out.println("==========连接2方法被调用==连接成功");;
// return studentService;
} catch (Exception e) {
studentService=null;
System.out.println("Service02连接失败!");
}finally {
return studentService;
}
}
}
明天计划的事:应该能结束任务8
遇到的问题:在上面过程中
收获:一点点
总结:今天早上打开电脑,昨晚师兄建议我dang下来的skill项目下好了,看了目录结构大概知道了公司项目的拆分,不过在代码中一到公司内部用的框架就有点看不懂了。今天之所以继续用自己的拆分方式(service和web)是因为想实现自己的想法和逻辑,不想半途而废,打断了自己的亲手代码实现后的乐趣(虽然这可能不符合公司项目开发的约定)。
评论