Zookeeper服务节点的监控及维护(节点注册,删除,故障维护等),Dubbo远程RPC调用服务。
Dubbo可以单独作为服务被消费者调用,分布式都是多服务的,所以会用到类似Zookeeper服务节点监控管理框架。


项目功能简介:
Zookeeper开启2个zkServer分别监听2081,2082端口,即分别为Provider和Provider1提供服务注册中心。
Provider,Provider1分别开启20880,20881端口启动Dubbo服务。
Consumer调用Provider和Provider1提供的服务。

项目结构图:
项目结构图

Provider,Provider1服务提供者,Consumer服务消费者,API服务接口
核心代码:
dubbo-api

public interface DemoService {

    String sayHello(String name);

}

public interface DemoService1 {

    String sayHello(String name);

}

dubbo-provider

public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

}

public class Provider {

    public static void main(String[] args) throws Exception {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();

        System.in.read(); // press any key to exit
    }

}

<?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.xsd        
     http://code.alibabatech.com/schema/dubbo        
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>

    <!-- use multicast registry center to export service -->
    <!-- <dubbo:registry address="multicast://224.1.6.7:1234"/> -->

    <dubbo:registry address="zookeeper://192.168.1.106:2181"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

</beans>

dubbo-provider1

public class DemoServiceImpl1 implements DemoService1 {

    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider111111111: " + RpcContext.getContext().getLocalAddress();
    }

}

public class Provider {

    public static void main(String[] args) throws Exception {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider1.xml"});
        context.start();

        System.in.read(); // press any key to exit
    }

}

<?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.xsd        
     http://code.alibabatech.com/schema/dubbo        
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider1"/>

    <!-- use multicast registry center to export service -->
    <!-- 组播方式易产生网络风暴  <dubbo:registry address="multicast://224.1.6.7:1234"/> -->
    <!-- zookeeper单注册中心多服务注册 <dubbo:registry address="zookeeper://192.168.1.106:2181"/> -->
    <!-- zookeeper多注册中心多服务注册 -->
    <dubbo:registry address="zookeeper://192.168.1.106:2182"/>

    <!-- 单注册中心多服务注册的时候各服务端口号不能相同,否则无法匹配同名服务 -->
    <dubbo:protocol name="dubbo" port="20881"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService1" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl1"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService1" ref="demoService1"/>

</beans>
dubbo-consumer
public class Consumer {

    public static void main(String[] args) {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
        DemoService1 demoService1 = (DemoService1) context.getBean("demoService1"); 
        while (true) {
            try {
                Thread.sleep(1000);
                String hello1 = demoService1.sayHello("world"); // call remote method
                String hello = demoService.sayHello("world");
                System.out.println(hello); // get result
                System.out.println(hello1); // get result

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }


        }

    }
}

<?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.xsd        
     http://code.alibabatech.com/schema/dubbo        
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>

    <!-- use multicast registry center to discover service -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->

    <dubbo:registry id="demo" address="zookeeper://192.168.1.106:2181"/>
    <dubbo:registry id="demo1" address="zookeeper://192.168.1.106:2182"/>

    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService" registry="demo"/>
    <dubbo:reference id="demoService1" check="false" interface="com.alibaba.dubbo.demo.DemoService1"  registry="demo1"/>

<!-- provider,provider1提供相同服务demoService的负载均衡配置(权重算法),默认随机算法
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService" registry="demo" loadbalance="roundrobin"/>  -->

</beans>

pom

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>dubbo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>dubbo-provider1</groupId>
  <artifactId>dubbo-provider1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven</name>
  <url>http://maven.apache.org/</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.1.5.RELEASE</springframework.version>
        <!-- Logging -->
        <slf4j.version>1.7.5</slf4j.version>
        <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version>
        <log4j.version>1.2.17</log4j.version>
  </properties>
  <dependencies>
    <dependency>
        <groupId>dubbo-api</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <!-- Logging with SLF4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-log4j12.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- SPRING -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>  
            <groupId>org.apache.curator</groupId>  
            <artifactId>curator-recipes</artifactId>  
            <version>2.5.0</version>  
        </dependency>  
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
  </dependencies>
</project>


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>dubbo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>dubbo</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven</name>
  <url>http://maven.apache.org/</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.1.5.RELEASE</springframework.version>
  </properties>
    <dependencies>
    <dependency>
        <groupId>dubbo-api</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>  
            <groupId>org.apache.curator</groupId>  
            <artifactId>curator-recipes</artifactId>  
            <version>2.5.0</version>  
        </dependency>  
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
  </dependencies>
</project>


动态调用dubbo服务代码:

public class Consumer2 {

    public static void main(String[] args) {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer2.xml"});
        String url = "dubbo://192.168.1.106:20880/com.alibaba.dubbo.demo.DemoService";//更改不同的Dubbo服务暴露的ip地址&端口  
        ReferenceBean<DemoService> referenceBean = new ReferenceBean<DemoService>();  
        referenceBean.setApplicationContext(context);  
        referenceBean.setInterface(com.alibaba.dubbo.demo.DemoService.class);  
        referenceBean.setUrl(url);  

       while (true) {
            try {
                Thread.sleep(1000);
                referenceBean.afterPropertiesSet();  
                DemoService demoService = referenceBean.get();  
                System.out.println(demoService.sayHello("Tester"));  

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }


       }

    }

}

<?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.xsd        
     http://code.alibabatech.com/schema/dubbo        
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer2"/>
</beans>

项目源码:https://github.com/lichengshengko/demo-dubbo
Windows下Zookeeper伪集群设置参考:https://blog.csdn.net/ceasadan/article/details/52343734(需安装JDK并配置JAVA路径)
Dubbo官方项目下载地址:https://github.com/apache/incubator-dubbo


说明:
RpcContext为Dubbo官方封装好的库文件,主要服务提供者配置信息的获取。
分布式架构设计的难点是服务节点的动态操作及事务的处理。
单点故障处理,上述Provider1节点故障了,服务中心如何快速注册替代服务。
分布式事务,Consumer调用多个服务,但实际业务中有顺序的,如何协调。若其中一个服务超时如何回滚等。
本文章的项目为一个静态节点的小例子,主要用于学习与参考。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐