bbossaop遠端服務介紹-遠端服務呼叫例項

bboss發表於2009-04-26

環境準備

準備三臺伺服器

n         伺服器A

Ip 172.16.17.56

1185

 

n         伺服器B

Ip 172.16.17.51

1185

n         伺服器C

Ip 172.16.17.52

1185

服務部署

假設我們已經定義了業務元件test.Atest.B,分別實現介面test.ServiceInf。服務中定義了方法handle(),返回值為Object,方法程式碼如下:

public  Object handle(){

    return new Integer(1);

}

ABC三臺伺服器上分別部署管理服務managerid,建立配置檔案service-assemble.xml內容如下:

<manager-config>

 

<manager id=managerid //管理服務id

singlable=“true” //單列模式

 >

<provider type=“provider_a”  //provider實現a

           class=“test.A” />

<provider type=“provider_b” //provider實現b

           class=“test.B” />

<transactions>

<method name=“handle” txtype=“REQUIRED_TRANSACTION”/>

</transactions>

</manager>

<manager-config>

 

然後將service-assemble.xml匯入到主配置檔案manager-provider.xml檔案:

<manager-config>

   

    <managerimport file=“com/chinacreator/spi/rpc/service-assemble.xml” />

</manager-config>

 

在主配置檔案manager-provider.xml中新增以下配置,作為遠端管理元件啟用的控制開關:

<manager-config>

    <properties>

       <property name=“cluster_enable” value=“true”/> //啟用遠端管理元件

       <property name=“cluster_mbean_enable” value=“true”/> //啟用遠端管理元件的mbean監控功能,目前未使用

       <property name=“cluster_name” value=“Cluster”/>  //遠端管理元件名稱

</properties>

    <managerimport file=“com/chinacreator/spi/rpc/service-assemble.xml” />

</manager-config>

 

 

配置伺服器間通訊埠和組播地址

 

在遠端管理元件的配置檔案etc/META-INF/replSync-service-aop.xml

中修改bind_port屬性:

bind_port=“1185”

修改組播地址:

mcast_addr=“228.10.10.178”

 

客服端程式編寫

package com.chinacreator.spi.rpc;

 

import com.chinacreator.spi.BaseSPIManager;

import com.chinacreator.spi.SPIException;

 

public class Test {

    public static void testMutirpcCall() {

        try {

            ServiceInf rpc = (test.ServiceInf)BaseSPIManager

                .getProvider(“(172.16.17.51:1185; 172.16.17.56:1185)/managerid”);

            // RPCTest rpc = (RPCTest)BaseSPIManager.getProvider(“managerid“);

            // RPCTest rpc =

            // (RPCTest)BaseSPIManager.getProvider(“(_self)/managerid“);

            // RPCTest rpc1 = (RPCTest)BaseSPIManager.getProvider(

            // “(172.16.17.52: 1185;172.16.17.56: 1185)/managerid “);

            // RPCTest rpc2 =

            // (RPCTest)BaseSPIManager.getProvider(“(all)/managerid“);

            long s = System.currentTimeMillis();

 

            Integer object = (Integer)rpc. handle();

                System.out.println(BaseSPIManager.getRPCResult(“172.16.17.51”, “1185”, object));

                System.out.println(BaseSPIManager.getRPCResult(“172.16.17.56”, “1185”, object));

               

           

            long e = System.currentTimeMillis();

 

            System.out.println((e – s) / 1000 + );

           

        } catch (SPIException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

 

    public static void testsinglerpcCall() {

        try {

            ServiceInf rpc = (test.ServiceInf)BaseSPIManager.getProvider(“(172.16.17.56:1185)/managerid”);

         

            long s = System.currentTimeMillis();

 

           Object object = rpc. handle();

              

                System.out.println(object);

               

           

            long e = System.currentTimeMillis();

 

            System.out.println((e – s) / 1000 + );

          

        } catch (SPIException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

   

    public static void testAllrpcCall() {

        try {

            ServiceInf rpc = (test.ServiceInf)BaseSPIManager.getProvider(“(all)/managerid”); 

            long s = System.currentTimeMillis();          

                Object object = rpc .handle();

                System.out.println(BaseSPIManager.getRPCResult(“172.16.17.51”, “1185”, object));

                System.out.println(BaseSPIManager.getRPCResult(“172.16.17.52”, “1185”, object));

                System.out.println(BaseSPIManager.getRPCResult(“172.16.17.56”, “1185”, object));

           

           

            long e = System.currentTimeMillis();

 

            System.out.println((e – s) / 1000 + );

          

        } catch (SPIException e) {

         

            e.printStackTrace();

        }

    }

}

啟動伺服器

分別在每臺伺服器上執行類com.chinacreator.remote.RunAop的主方法,既可以啟動伺服器。

注意,客服端不要啟動伺服器。

執行客服端

Test.testAllrpcCall();

        Test.testMutirpcCall();

 Test.testsinglerpcCall();

更詳細的資訊請參考章節【BaseSPIProvider元件介紹中.實現遠端服務呼叫 


相關文章