RMI發布多個服務(介面)

2021-08-30 07:47:58 字數 3959 閱讀 4938

在使用spring rmi時可能會遇到發布多個服務的需求,假設乙個服務要操作檔案,另乙個服務要往螢幕上列印「hello world」。難道要把這兩個風馬牛不相及的服務寫在乙個介面裡嗎!如果真這樣那結構也太差了。

網上有人問這個問題:「看樣子這個問題難住了一些人,因為他們想在乙個servicename裡面發布多個serviceinte***ce。其實轉換個思維也就不會被難倒了。倘若在乙個servicename裡面發布多個serviceinte***ce,那麼service又該怎麼配呢,難道用個list將serviceinte***ce和service對應起來?顯然是行不通的。

解決辦法很簡單:配置多個rmiserviceexporter的bean,使不同的服務(屬性:service)用不同的服務名(屬性:servicename)和介面(屬性:serviceinte***ce)。給個demo就更清晰了:

1:  

<

bean

id="oper1"

class="***.oper1"

>

bean

>

2:  

<

bean

id="rmiservice_oper1"

class="org.springframework.remoting.rmi.rmiserviceexporter"

>

3:  

<

property

name="servicename"

>

4:  

<

value

>oper1

value

>

5:  

property

>

6:  

<

property

name="service"

>

7:  

<

ref

local="oper1"

/>

8:  

property

>

9:  

<

property

name="serviceinte***ce"

>

10:  

<

value

>***.ioper1

value

>

11:  

property

>

12:  

<

property

name="registryport"

>

13:  

<

value

>9990

value

>

14:  

property

>

15:  

bean

>

16:  

17:  

<

bean

id="oper2"

class="***.oper2"

>

bean

>

18:  

<

bean

id="rmiservice"

class="org.springframework.remoting.rmi.rmiserviceexporter"

>

19:  

<

property

name="servicename"

>

20:  

<

value

>oper2

value

>

21:  

property

>

22:  

<

property

name="service"

>

23:  

<

ref

local="oper2"

/>

24:  

property

>

25:  

<

property

name="serviceinte***ce"

>

26:  

<

value

>***.ioper2

value

>

27:  

property

>

28:  

<

property

name="registryport"

>

29:  

<

value

>9990

value

>

30:  

property

>

31:  

bean

>

service、servicename和serviceinte***ce都不一樣,埠可以相同。

客戶端的配置也很簡單了:

1:  

<

bean

id="rmiclient_oper1"

class="org.springframework.remoting.rmi.rmiproxyfactorybean"

>

2:  

<

property

name="serviceurl"

>

3:  

<

value

>rmi:

value

>

4:  

property

>

5:  

<

property

name="serviceinte***ce"

>

6:  

<

value

>***.ioper1

value

>

7:  

property

>

8:  

bean

>

9:  

<

bean

id="rmiclient_oper2"

class="org.springframework.remoting.rmi.rmiproxyfactorybean"

>

10:  

<

property

name="serviceurl"

>

11:  

<

value

>rmi:

value

>

12:  

property

>

13:  

<

property

name="serviceinte***ce"

>

14:  

<

value

>***.ioper2

value

>

15:  

property

>

16:  

bean

>

main裡執行:

1:  

public static void main(string args)

這主要是個思路問題,沒什麼巧的。

rmi介面連線失敗

普通rmi 1.實現類要extends unicastremoteobject,繼承服務介面 2.客戶段與伺服器段 的介面要放在 同名 的包內 3.介面要extends remote,介面方法要throws remoteexception 同樣介面實現類的相應方法也要throws remoteexc...

Spring使用POJO發布RMI

spring可以把我們的pojo作為rmi服務進行發布 我們的業務類介面 public inte ce paymentservice 我們的業務類實現 public class paymentserviceimpl 我們為了把paymentservice中的方法作為rmi輸出,可以這樣進行配置 be...

基於RMI和C S的服務發現

主要用於有多個伺服器的情況下,伺服器啟動後回向註冊中心註冊。當有客戶端上線時,客戶端通過服務編號,從註冊中心得到此服務的所有伺服器列表,然後選擇效能最優的伺服器連線。2.1服務發現的主要目的 幫助客戶端發現效能良好的伺服器。客戶端對伺服器的選擇可以實現 負載均衡 服務發現支援伺服器的 熱插拔 2.2...