发表于: 2017-10-09 20:14:56

1 820


今天完成的事情:

在spring中配置rmi

定义远程接口

package service;

/**
* created by 姚远 on 2017/10/9.
*/
public interface TestService {
   String
hello(String name);
}

如上,这个接口没有继承java.rmi.Remote接口,接口中的每个抽象方法也没有抛出RemoteException异常,也就是和普通的接口没有任何区别

定义实现类

public class TestServiceImpl implements TestService {
public String hello(String name) {
Logger logger=Logger.getLogger(TestServiceImpl.class);
       logger.info("进入Service方法");

       return "Hello"+name;
   }
}

如上,实现类必须实现远程接口内的所有抽象方法,同时也不需要继承java.rmi.UnicastRemoteObject类,和普通的实现类并没有区别。

在XML文件中配置RMI服务端

server1.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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
   <!-- RMI服务端 -->
   <!-- RMI服务端远程接口实现类 -->
   <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <!-- 将远程接口实现类对象设置到RMI服务中 -->
       <property name="serviceName" value="TestServiceImpl" />
       <!-- 设置RMI服务名,为RMI客户端依据此服务名获取远程接口实现类对象引用奠定基础 -->
       <property name="service" ref="TestService" />
       <!-- 将远程接口设置为RMI服务接口 -->
       <property name="serviceInterface" value="service.TestService" />
       <!-- 为RMI服务端远程对象注册表设置端口号-->
       <property name="registryPort" value="11011" />
       <property name="servicePort" value="11011" />
</bean>
   <!--端口11097-->

   <bean id="TestService" class="service.TestServiceImpl" />
</beans>

server2.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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
   <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="serviceName" value="TestServiceImpl" />
       <property name="service" ref="TestService" />
       <property name="serviceInterface" value="service.TestService" />
       <property name="registryPort" value="11010" />
       <property name="servicePort" value="11010" />
       <!--自定义端口11010-->
   </bean>

   <bean id="TestService" class="service.TestServiceImpl" />
</beans>

启动服务端

RmiServer1

package core;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* created by 姚远 on 2017/10/9.
*/
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("Server.xml");
       System.out.println("服务端1");
       Object lock = new Object();
       synchronized (lock) {
lock.wait();
       }
}
}

RmiServer2

package core;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* created by 姚远 on 2017/10/9.
*/
public class RmiServer2 {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("Server2.xml");
       System.out.println("服务端2");
       Object lock = new Object();
       synchronized (lock) {
lock.wait();
       }
}
}

将Rmi配置到Spring之后要启动服务也很简单,只要加载配置文件的上下文,在spring容器中生成相应的bean即可。

在XML中配置Rmi客户端

Client1

<?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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   <!--RMI客户端-->
   <bean id="TestService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceUrl" value="rmi://localhost:11097/TestServiceImpl" />
       <property name="serviceInterface" value="service.TestService" />
   </bean>

</beans>

Client2

<?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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   <bean id="TestService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceUrl" value="rmi://localhost:11010/TestServiceImpl" />
       <property name="serviceInterface" value="service.TestService" />
   </bean>

</beans>

启动Rmi客户端

public class RmiClient {
private static final Logger LOG = Logger.getLogger(RmiClient.class);

   public static void main(String[] args) {
double m =Math.random();
       int rr =(int)(m*10);
       if(rr%2!=0) {
try{ApplicationContext ctx = new ClassPathXmlApplicationContext(
"client.xml");
               TestService testMethod = (TestService) ctx.getBean("TestService");
               String result = testMethod.hello("from服务器一");
               LOG.info(result);}
catch (BeanCreationException e){
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"client2.xml");
               TestService testMethod = (TestService) ctx.getBean("TestService");
               String result = testMethod.hello("from服务器二");
               LOG.info(result);
           }
}else {
try{ApplicationContext ctx = new ClassPathXmlApplicationContext(
"client2.xml");
               TestService testMethod = (TestService) ctx.getBean("TestService");
               String result = testMethod.hello("from服务器二");
               LOG.info(result);}
catch (BeanCreationException e){
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"client.xml");
               TestService testMethod = (TestService) ctx.getBean("TestService");
               String result = testMethod.hello("from服务器一");
               LOG.info(result);
           }
}

}
}

先生成一个随机数,乘以10后为0到9

根据奇偶来判断是连接服务器1还是服务器2

这里为了使其中一个服务器挂掉也可以正常运行,采用try catch块来接住异常并调用另一个服务器

大概就是这样,先来跑一下看看

启动两个服务端

服务端如果不关闭的话是一直运行的,启动客户端

经过几次测试发现调用服务器一和调用服务器二的次数是一半一半的

如果挂掉其中一个,那么客户端仍然可以正常工作,并且只访问另一个服务端。两个都挂掉就报错了











返回列表 返回列表
评论

    分享到