您好,登錄后才能下訂單哦!
1.業務接口類及其實現
/** * 定義一個遠程接口 */ public interface HelloService { /** * 需要遠程調用的方法 * @param msg * @return */ String sayHello(String msg); }
public class HelloServiceImpl implements HelloService { public String sayHello(String msg) { return "server received the msg : " + msg; } }
2.RmiServiceExporter(服務端)
<?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="helloService" class="com.rmi.spring.HelloServiceImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <!--配置RMI的服務實現類--> <property name="service" ref="helloService" /> <!--配置RMI的服務接口--> <property name="serviceInterface" value="com.rmi.spring.HelloService"/> <!--暴露的對外服務名--> <property name="serviceName" value="hello" /> <!--服務本地注冊端口--> <property name="registryPort" value="9123" /> <!--服務對外暴露端口--> <property name="servicePort" value="9123" /> <!--注冊服務--> <property name="alwaysCreateRegistry" value="true" /> </bean> </beans>
3.RmiProxyFactoryBean(客戶端)
<?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="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:9123/hello" /> <property name="serviceInterface" value="com.rmi.spring.HelloService"/> </bean> </beans>
4.測試
public class HelloServer { public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-rmi-server.xml"); System.err.println("rmi 服務啟動!"); } }
public class HelloClient { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-rmi-client.xml"); HelloService helloService = (HelloService)applicationContext.getBean("helloService"); System.err.println(helloService.sayHello("測試測試!")); } }
注:Registry服務的注冊問題,有時會出現服務啟動報錯.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。