1. 程式人生 > >dubbo - 多註冊中心與分組聚合 demo

dubbo - 多註冊中心與分組聚合 demo

目錄

1. 程式簡單架構

2. dubbo-admin的配置

3. dubbo_parent的配置

4. dubbo_api的配置

5. dubbo_provider的配置

6. dubbo_customer的配置


 

1. 程式簡單架構

本demo,以zookeeper三個服務端叢集的方式作為 註冊中心,使用dubbo-admin作為監控端。主要演示dubbo的多註冊中心與分組聚合的程式碼。有所不足,敬請諒解。期間遇到了消費端找不到可用的服務端,本人將dubbo:service和dubbo:reference加入version即可解決。程式先啟動zookeeper,然後是tomcat,最後是服務端和消費端。

2. dubbo-admin的配置

需要在tomcat - webapps - ROOT 下,加入dubbo-admin-2.5.4.war。

然後在ROOT - WEB-INF - dubbo.properties 檔案中,修改為:

dubbo.registry.address=zookeeper://192.168.103.217:2181||zookeeper://192.168.103.217:2182||zookeeper://192.168.103.217:2183
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

唯一改動就是註冊中心的url的改動。因為我要開三個zookeeper的服務端,所以要有三個url,對應每一個zookeeper的服務端。

當準備工作做好的時候,先啟動三個zookeeper的服務端(我是以叢集的方式)。這裡有疑問的,請聯絡我。

接下來啟動tomcat(cmd方式)。在tomcat 的 bin目錄下 執行 startup.bat。

3. dubbo_parent的配置

我是以模組化的方式構建dubbo demo 的。idea 中建立模組化的檔案,大家不懂的可以百度。

先來看下工程結構:

 

然後配置該父模組的pom.xml:(主要寫了需要的依賴,)

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.21.0-GA</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jboss.netty/netty -->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.5.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/taglibs/standard -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>

4. dubbo_api的配置

<packaging>jar</packaging> 它的pom.xml只有這裡需要改動。

這裡,我寫了兩個介面。如下:

package com.test.service;

public interface DubboService {

    void sayHello(String name);
}
package com.test.service;

import java.util.List;

public interface MergeService {
    List<String> getMergeResult();
}

5. dubbo_provider的配置

pom.xml中,需要加入依賴:

        <dependency>
            <groupId>com.test</groupId>
            <artifactId>Dubbo_API</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

然後<packaging>war</packaging> 這裡需要注意下。

它的工程結構如下:

它的實現類,讀者自己定義即可。

接下來看下resources下的檔案:

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="Dubbo_Provider"/>
    <dubbo:registry address="zookeeper://192.168.103.217:2181" id="cluster1" check="false" file="${catalina.home}/dubbo-registry/dubbo-registry.properties"/>
    <dubbo:registry address="zookeeper://192.168.103.217:2182" id="cluster2" default="false" check="false" file="${catalina.home}/dubbo-registry/dubbo-registry2.properties"/>
    <dubbo:registry address="zookeeper://192.168.103.217:2183" id="cluster3" default="false" check="false" file="${catalina.home}/dubbo-registry/dubbo-registry3.properties"/>
    <dubbo:protocol name="dubbo" port="20882"/>
    <dubbo:service interface="com.test.service.DubboService" registry="cluster1,cluster2,cluster3"
                   protocol="dubbo" group="a" ref="dubboService" version="1.0.0"/>
    <dubbo:service interface="com.test.service.DubboService" registry="cluster1,cluster2,cluster3"
                   protocol="dubbo" group="b" ref="dubboService2" version="1.0.0"/>
    <dubbo:service interface="com.test.service.DubboService" registry="cluster1,cluster2,cluster3"
                   protocol="dubbo" group="a" ref="dubboService3" version="1.0.1"/>
    <dubbo:service interface="com.test.service.MergeService" registry="cluster1"
                   protocol="dubbo" group="merge-a" ref="mergeService" version="2.0.0"/>
    <dubbo:service interface="com.test.service.MergeService" registry="cluster1,cluster2,cluster3"
                   protocol="dubbo" group="merge-b" ref="mergeService2" version="2.0.0"/>
    <bean id="dubboService" class="com.mzs.service.impl.DubboServiceImpl"/>
    <bean id="dubboService2" class="com.mzs.service.impl.DubboServiceImpl2"/>
    <bean id="dubboService3" class="com.mzs.service.impl.DubboServiceImpl3"/>
    <bean id="mergeService" class="com.mzs.service.impl.MergeServiceImpl"/>
    <bean id="mergeService2" class="com.mzs.service.impl.MergeServiceImpl2"/>

</beans>

最後看下它的測試檔案:

package com.mzs.testProvider;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class TestProvider {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("provider.xml");
        applicationContext.start();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

6. dubbo_customer的配置

工程結構如下:

看下customer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="Dubbo_Customer"/>
    <dubbo:registry address="zookeeper://192.168.103.217:2181" id="cluster1" check="false" file="${catalina.home}/dubbo-registry/dubbo-registry.properties"/>
    <dubbo:registry address="zookeeper://192.168.103.217:2182" id="cluster2" default="false" check="false" file="${catalina.home}/dubbo-registry/dubbo-registry2.properties"/>
    <dubbo:registry address="zookeeper://192.168.103.217:2183" id="cluster3" default="false" check="false" file="${catalina.home}/dubbo-registry/dubbo-registry3.properties"/>
    <dubbo:reference interface="com.test.service.DubboService" group="a" registry="cluster1,cluster2,cluster3"
                     timeout="500000" id="dubboService1" version="1.0.0"/>
    <dubbo:reference interface="com.test.service.DubboService" group="a" registry="cluster1,cluster2,cluster3"
                     timeout="500000" id="dubboService3" version="1.0.1"/>
    <dubbo:reference interface="com.test.service.DubboService" group="b" registry="cluster1,cluster2,cluster3"
                     timeout="500000" id="dubboService2"/>
    <dubbo:reference interface="com.test.service.MergeService" group="merge-a" registry="cluster1"
                     timeout="500000" id="mergeService" version="2.0.0"/>
    <dubbo:reference interface="com.test.service.MergeService" group="merge-b" registry="cluster1,cluster2,cluster3"
                     timeout="500000" id="mergeService2" version="2.0.0"/>

</beans>

以及它的測試檔案:

package com.mzs.testCustomer;

import com.test.service.DubboService;
import com.test.service.MergeService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class TestCustomer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("customer.xml");
        applicationContext.start();
        /*DubboService dubboService1 = (DubboService) applicationContext.getBean("dubboService1");
        DubboService dubboService2 = (DubboService) applicationContext.getBean("dubboService2");
        DubboService dubboService3 = (DubboService) applicationContext.getBean("dubboService3");*/
        MergeService mergeService = (MergeService) applicationContext.getBean("mergeService");
        MergeService mergeService1 = (MergeService) applicationContext.getBean("mergeService2");
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(3);
                /*dubboService1.sayHello("Tom");
                dubboService2.sayHello("Tom");
                dubboService3.sayHello("Selina");*/
                List<String> list = mergeService.getMergeResult();
                System.out.println(list);
                List<String> list1 = mergeService1.getMergeResult();
                System.out.println(list1);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

最後先啟動服務端,再啟動消費端,即可成功執行程式。