第一个dubbo实例

in 系统环境
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

架构

转自官网
dubbo-architucture

节点角色说明

调用关系说明

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

实例

整体结构

image.png

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&amp;timeout=10000"/>

    <dubbo:service interface="com.learn.dubbo.demo.HelloService" ref="helloService"/>
</beans>

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&amp;timeout=10000"/>

    <dubbo:reference id="helloService" interface="com.learn.dubbo.demo.HelloService"/>
</beans>

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,控制台输出了信息
image.png
zookeeper的节点,多了consumers和routers

[configurators, consumers, providers, routers]

其他

除了XML 配置,还有属性配置API 配置注解配置
schema启动时检查等示例,可以参考官网。

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看