javascript
Spring Rmi配置
現在遠程調用一般用RPC,webservice或者Rmi,而目前用的比較多的是webservice和Rmi。
webservice和rmi的最主要的區別,rmi的客戶端和服務端都必須是java,webservice沒有這個限制,webservice是在http協議上傳遞xml文本文件。與語言和平臺無關,rmi是在tcp協議上傳遞可序列化的java對象,只能用在java虛擬機上,綁定語言。?RMI是EJB遠程調用的基礎,僅用RMI技術就可以實現遠程調用,使用EJB是為了實現組件,事物,資源池,集群等功能.WebService是通過XML來傳輸數據,可用http等協議因此可在異構系統間傳遞,并且可以穿過防火墻,可在公網上遠程調用。
以下是spring中配置Rmi的一個簡單例子:
1.首先在src下建立一個接口(本人的是com.shinelife.inter):
package com.shinelife.inter;
public interface IBaseServiceRmi {
?? ?public String SayHelloRmi(String name);
}
2.然后實現接口的方法:
package com.shinelife.services;
import com.shinelife.inter.IBaseServiceRmi;
public class BaseServiceRmi implements IBaseServiceRmi {
public String SayHelloRmi(String name) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?return "Hello: "+name;
?? }
}
3.然后就是配置remote.xml:
在src下面新建一個xml文件然后復制添加以下內容(注:頭文件的對象配置很重要,配置不當會報錯):
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring 遠程服務對象配置-->
<beans xmlns="http://www.springframework.org/schema/beans"
?? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
?? ?xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
?? ?xmlns:flex="http://www.springframework.org/schema/flex"
?? ?xsi:schemaLocation="http://www.springframework.org/schema/beans
?????????? http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
?????????? http://www.springframework.org/schema/context
?????????? http://www.springframework.org/schema/context/spring-context-2.5.xsd
?????????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
?????????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
?? <!-- 遠程對象暴露? 開始 -->
?? ?
?? ??? ?<bean id="BaseServiceRmiExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
?? ??? ? <!-- 調用Service --> ?
?? ??? ?<property name="service">
?? ??? ??? ?<ref bean="BaseServiceRmi" />
?? ??? ?</property>
?? ??? ?<!-- 客戶端調用時使用的名字 -->
?? ??? ?<!-- value值是給用戶調用 --> ?
?? ??? ?<property name="serviceName">
?? ??? ??? ?<value>BaseServiceRmi</value>
?? ??? ?</property>
?? ??? ?<!-- service 接口 -->
?? ??? ?<property name="serviceInterface">
?? ??? ??? ?<value>com.shinelife.inter.IBaseServiceRmi
?? ??? ??? ?</value>
?? ??? ?</property>
?? ??? ?<!-- 注冊端口號 --> ?
?? ??? ?<property name="registryPort">
?? ??? ??? ?<value>6100</value>
?? ??? ?</property>
?? ??? ?<property name="servicePort">
?? ??? ??? ?<value>7100</value>
?? ??? ?</property>
?? ?</bean>
? <!-- 遠程對象暴露? 結束 -->
</beans>
4.本人以上的例子是自己建立的一個remote.xml文件,而一般都是直接把這些配置到spring的配置文件applicationContext.xml里,而這種在這里就不研究了。主要說說外部配置Rmi的方式。
光是添加了一個remote.xml是不夠的,還需要在spring的applicationContext.xml里面注入:
<bean id="BaseServiceRmi" class="com.shinelife.services.BaseServiceRmi"></bean>
這樣spring才能找到并加載這個bean。
5.由于本人的是webservice項目,所以啟動服務是用的配置web.xml,也可以用main入口函數加載,對于main加載,就不講述了,這里講述web.xml配置,以下是web.xml的部分內容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?? ?version="2.5"
?? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee? ?
?? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?<context-param>
?????? <param-name>contextConfigLocation</param-name>
?????? <param-value>/WEB-INF/classes/applicationContext.xml,classpath:remote.xml
?????? </param-value>
??? </context-param>
</web-app>
以上是服務端方面的配置,下面是客戶端:
6.將com.shinelife.inter整個包拷貝到客戶端下(注意服務端與客戶端的包名保持一致)
7.在客戶端的src下面新建一個Rmi.xml的文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
? "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?? ?
??? <bean id="BaseServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">?????? ?
???????? <!-- BaseService是調用服務端serviceName的value --> ?
?? ??? ?<property name="serviceUrl">
?? ??? ??? ?<value>rmi://127.0.0.1:6100/BaseServiceRmi</value>
?? ??? ?</property>
?? ??? ? <!-- service接口 --> ?
?? ??? ?<property name="serviceInterface">
?? ??? ??? ?<value>com.shinelife.inter.IBaseServiceRmi</value>
?? ??? ?</property>
?? ?</bean>
</beans>
8.然后建立一個main函數測試Rmi服務:
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import com.shinelife.inter.IBaseServiceRmi;
public class testRmi {
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:Rmi.xml"); ?
??????? IBaseServiceRmi baseService = (IBaseServiceRmi) context.getBean("BaseServiceRmi");
??????? System.out.println( baseService.SayHelloRmi("Joker"));
?? ?}
}
9.測試結果:Hello: Joker
10.總結:Rmi是一個比較高效的遠程調用方式,可以將整個函數方法作為參數進行遠程調用,達到通信的目的,利用Rmi也可以減少很多的工作量。當然,在其他方面本人就不做結論,本人這也是分享一下搭建Rmi的方法。以便以后學習。
轉載于:https://www.cnblogs.com/jokerpan/p/3559478.html
總結
以上是生活随笔為你收集整理的Spring Rmi配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈啰出行app怎么升级
- 下一篇: 如何使 FlashGet 正常合法 下载