转载

第一个dubbo实例

架构

转自官网 。

第一个dubbo实例

节点角色说明

  • Provider:暴露服务的服务提供方
  • Consumer:调用远程服务的服务消费方
  • Registry:服务注册与发现的注册中心
  • Monitor:统计服务的调用次数和调用时间的监控中心
  • Container:服务运行容器

调用关系说明

  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

实例

整体结构

第一个dubbo实例

dubbo-demo

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>com.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>dubbo-demo-interface</module>
        <module>dubbo-demo-xml</module>
    </modules>
    <properties>
        <dubbo.version>2.7.4.1</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

dubbo-demo-interface

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-demo-interface</artifactId>
</project>

HelloService

public interface HelloService {  
    String sayHello(String name);  
}

dubbo-demo-xml-provider

暴露服务的服务提供方。

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo-xml</artifactId>
        <groupId>com.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-demo-xml-provider</artifactId>
    <properties>
        <slf4j-log4j12.version>1.7.25</slf4j-log4j12.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>com.learn</groupId>
            <artifactId>dubbo-demo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Application

public class Application {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
        context.start();
        System.in.read();
    }
}

HelloServiceImpl,服务方的实现

@Service("helloService")
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.learn.dubbo.demo"/>

    <dubbo:application name="dubbo-provider"/>

    <dubbo:protocol name="dubbo" port="20881"/>

    <dubbo:registry address="zookeeper://172.17.0.2:2181?backup=172.17.0.3:2181,172.17.0.4:2181&timeout=10000"/>

    <dubbo:service interface="com.learn.dubbo.demo.HelloService" ref="helloService"/>
</beans>
  • application:应用名称,建议和项目一致
  • protocol:服务提供者协议配置
  • registry:注册中心配置,由于一直报zookeeper连接不上,所以这边把超时时间设置长了
  • service:服务提供者暴露服务配置,把HelloService暴露出去。

dubbo.properties

dubbo.application.qos.port=22222

配置这个,是因为qos-server启动的时候,端口会占用。qos是dubbo的 在线运维命令 ,dubbo2.5.8新版本重构了telnet模块,提供了新的 telnet命令支持 。

log4j.properties

###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

dubbo-demo-xml-consumer

Application,消费方,远程调用服务方的方法。

public class Application {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
        context.start();
        HelloService helloService = context.getBean("helloService", HelloService.class);
        String result = helloService.sayHello("张三");
        System.out.println(result);
    }
}

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-consumer"/>

    <dubbo:registry address="zookeeper://172.17.0.2:2181?backup=172.17.0.3:2181,172.17.0.4:2181&timeout=10000"/>

    <dubbo:reference id="helloService" interface="com.learn.dubbo.demo.HelloService"/>
</beans>
  • referenc:服务消费者引用服务配置

dubbo.properties

dubbo.application.qos.port=33333

log4j.properties同上

运行

先看看zookeeper节点

[zk: localhost:2181(CONNECTED) 5] ls /
[zookeeper]

运行提供方的application,可以看到zookeeper有多个节点了

[zk: localhost:2181(CONNECTED) 2] ls /ddubbo/com.learn.dubbo.demo.HelloService
[configurators, providers]

查看providers的信息:

dubbo%3A%2F%2F192.168.102.186%3A20881%2Fcom.learn.dubbo.demo.HelloService%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26bean.name%3Dcom.learn.dubbo.demo.HelloService%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.learn.dubbo.demo.HelloService%26methods%3DsayHello%26pid%3D9692%26release%3D2.7.4.1%26side%3Dprovider%26timestamp%3D1577178472840

可以看到暴露了当前服务器的地址

dubbo%3A%2F%2F192.168.102.186%3A20881%2Fcom.learn.dubbo.demo.HelloService

运行消费方的application,控制台输出了信息

第一个dubbo实例

zookeeper的节点,多了consumers和routers

[configurators, consumers, providers, routers]

其他

除了 XML 配置 ,还有 属性配置 、 API 配置 、 注解配置 。

schema 、 启动时检查 等示例,可以参考官网。

原文  https://segmentfault.com/a/1190000021387826
正文到此结束
Loading...