发表于: 2020-08-17 21:53:44
1 1355
今天完成的事情:随机访问Service,处理BUG。
明天计划的事情:深度思考
遇到的问题:一定要让实体类实现Serializable 接口,才能在网络传输,rmi远程方法调用最底层是通过socket通信来实现对象的传输,其数据的传输最后都归结于二进制的数据包。
收获:
随机访问Service
在服务端和客户端搭建好的情况下,我从新创建一个RmiMannager接口来封装服务端的接口,然后再新建一个随机访问类ChooseService,并且根据我自己的服务端接口实现类为
RmiMannagerServiceChoose,去实现上面的接口,并继承ChooseService。
1. RmiMannager接口
public interface RmiMannager {
RmiUserService chooseUserService();
RmiStudentService chooseStudentService();
}
2. ChooseService生成随机访问类
public class ChooseService {
private Logger logger = LoggerFactory.getLogger(ChooseService.class);
public Object chooseService(Object m1, Object m2){
int choose = new Random().nextInt(2);
if(0 == choose){
try{
logger.info("Choose Server1");
return m1;
}catch (Exception e1){
try{
logger.info("Server1 Error,Choose Server2");
return m2;
}catch (Exception e2){
logger.info("Server1 and Server2 Error");
throw new RuntimeException("Rmi Link Error");
}
}
}
if(1 == choose){
try{
logger.info("Choose Server1");
return m2;
}catch (Exception e1){
try{
logger.info("Server1 Error,Choose Server2");
return m1;
}catch (Exception e2){
logger.info("Server1 and Server2 Error");
throw new RuntimeException("Rmi Link Error");
}
}
}
throw new RuntimeException("unknown error");
}
}
3. RmiMannagerServiceChoose
public class RmiMannagerServiceChoose extends ChooseService implements RmiMannager {
@Qualifier("rmiStudentClientA")
@Autowired
private RmiStudentService rmiStudentClientA;
@Qualifier("rmiStudentClientB")
@Autowired
private RmiStudentService rmiStudentClientB;
@Qualifier("rmiUserClientA")
@Autowired
private RmiUserService rmiUserClientA;
@Qualifier("rmiUserClientB")
@Autowired
private RmiUserService rmiUserClientB;
@Override
public RmiUserService chooseUserService() {
return (RmiUserService)chooseService(rmiUserClientA, rmiUserClientB);
}
@Override
public RmiStudentService chooseStudentService() {
return (RmiStudentService)chooseService(rmiStudentClientA, rmiStudentClientB);
}
}
4.控制层直接调用RmiMannager接口就行
@Resource
private RmiMannager userService;
List<Student> listStu = studentService.chooseStudentService().selectAll();
评论