发表于: 2017-11-22 23:02:12
2 677
今天完成的事
服务器上实现远程调用
首先把两台service部署到服务器上
因为两台service中有main方法,所以普通的打jar包的方法是行不通的,要在pom中添加插件,并指定main方法的位置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 与Servlet版本对应 -->
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.task8.jnshu.server.RmiServer</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions></plugin>
上面标红的地方需要改成自己项目中对应的,至于其他的配置文件,只要遵守maven目录构造放进src/main/resourse,就不要另外的配置,约定优于配置
打出jar之后可以直接上传到服务器,使用java -jar xxx.jar就可以运行main方法
为了保持service的运行,我们要想办法锁住 这个程序,让其不停止
两种方法,亲测可用
接着部署另一台service,需要注意的是两台service应该使serviceName和registryPort不相同,否则会发生报错或者冲突
在服务器上启动过service之后肯能会遇到一个问题:
需要去做其他的事,但是这个xshell窗口因为运行了main方法,所以不能进行其他操作
这里就要使用 java -jar xxx.jar & 让这个命令可以使用ctrl+z 挂载到后台,以进行其他操作
之后可能有人要问,如果我需要把xshell关了也能是service运行呢?
那么我们就先来看一下为什么关闭xshell就会导致service停止:
如上方到后台执行的进程,其父进程还是当前终端shell的进程,而一旦父进程退出,则会发送hangup信号给所有子进程,子进程收到hangup以后也会退出。如果我们要在退出shell的时候继续运行进程,则需要使用nohup忽略hangup信号,或者setsid将将父进程设为init进程(进程号为1)
知道为什么了就直接使用 nohup java -jar xxx,jar & 或者 setsid java -jar xxxx.jar & 命令来保证关了shell不会使service停止
PS:以上命令也可用于其他命令
之后部署web,需要注意的是任务要求中可以随机访问两台service,所以就需要有专门的类来选择使用哪台service,以及其中一台service挂掉自动使用另一台service 这里借(chao)鉴(xi)了于博滔的日报中的代码
public static StudentService getS() {
StudentService studentService;
Random random = new Random();
Integer num = random.nextInt(2)+1;
if(num == 1){
logger.info("============>进入Service0");
try {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
studentService = (StudentService) context.getBean("StudentService0");
}catch (Exception e){
logger.info("============>Service0挂了,用1");
logger.error("错误信息:"+e.getMessage());
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
studentService = (StudentService) context.getBean("StudentService1");
e.printStackTrace();
}
}else{
logger.info("============>进入Service1");
try {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
studentService = (StudentService) context.getBean("StudentService1");
}catch (Exception e){
logger.error("错误信息:"+e.getMessage());
logger.info("============>Service1挂了,用0");
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
studentService = (StudentService) context.getBean("StudentService0");
e.printStackTrace();
}
}
return studentService;
}
在controller中使用
private StudentService studentService (){
return RandomServiceUtil.getS();
}
来调用该类
使用studentService()调用类中的方法
studentService().allStudent()
之后容器运行web
测试接口,并查看日志
关掉其中一台service,查看日志
好的,达成任务要求
接下来关闭一台web
这里需要查看Nginx日志
两台都工作的情况
关掉一台WEB的情况
好的 任务结束
深度思考:
遇到的问题:
明天的计划:
开始任务9
拆分禅道
收获:对RMI的使用和理解更深了
任务进度:任务8已完成
任务开始时间:2017.11.18
延期风险:已延期两天
评论