第一個dubbo實例

架構

轉自官網
dubbo-architucturehtml

節點角色說明

  • Provider:暴露服務的服務提供方
  • Consumer:調用遠程服務的服務消費方
  • Registry:服務註冊與發現的註冊中心
  • Monitor:統計服務的調用次數和調用時間的監控中心
  • Container:服務運行容器

調用關係說明

  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

pomspring

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

HelloServiceapache

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

dubbo-demo-xml-provider

暴露服務的服務提供方。
pomsegmentfault

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

Applicationapi

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>
  • application:應用名稱,建議和項目一致
  • protocol:服務提供者協議配置
  • registry:註冊中心配置,因爲一直報zookeeper鏈接不上,因此這邊把超時時間設置長了
  • service:服務提供者暴露服務配置,把HelloService暴露出去。

dubbo.propertiesapp

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>
  • 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,控制檯輸出了信息
image.png
zookeeper的節點,多了consumers和routers

[configurators, consumers, providers, routers]

其餘

除了XML 配置,還有屬性配置API 配置註解配置
schema啓動時檢查等示例,能夠參考官網。

相關文章
相關標籤/搜索