• Dubbo简介

分布式,高性能,透明化的rpc服务框架,提供服务自动注册,自动发现等高效服务治理方案

  • 角色

provider:服务提供方
consumer:服务消费,调用服务方
container:dubbo容器,依赖于spring容器
registry:注册中心,当container 启动的时候,把所有可以提供的服务到registry上面注册。注册中心告诉consumer提供了什么服务和服务在哪里

monitor:监控中心,监控consumer和provider的压力情况(调用次数,频率等)

  •  运行原理

1.启动容器,相当于启动dubbo的provider
2.启动后,provider会去注册中心注册所有可提供的服务列表
3.consumer启动后会去registry获取服务列表和provider的地址,进行订阅
4.当provider有修改的时候,注册中心会把消息推送给consumer(使用了设计模式中的观察者模式)
5.consumer真实调用provider的功能(使用了代理模式,创建一个provider的代理对象,起到保护provider的作用)

  

  • 环境搭建

1.创建maven工程dubbo-service,该maven工程配置服务提供方的接口

package com.test.user;

public interface UserService {
    String getUserNameById(String id);
}

pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dubbo</groupId>
  <artifactId>dubbo-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build/>
</project>

2.创建maven工程dubbo-service-impl,该maven工程配置服务提供方接口的实现

package com.test.impl.user;

import com.test.user.UserService;


public class UserServiceImpl implements UserService{

    public String getUserNameById(String id) {
        return "name:"+id;
    }

}

applicationContext-dubbo.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">



    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo_provider"  />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.140.55:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"  />
    <dubbo:service interface="com.test.user.UserService"  ref="demoService" />
    <bean id="demoService" class="com.test.impl.user.UserServiceImpl" />
     </beans>

pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dubbo</groupId>
  <artifactId>dubbo-service-impl</artifactId>
  <version>0.0.1-SNAPSHOT</version>

          <properties>
              <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
              <spring.version>4.2.3.RELEASE</spring.version>
              <mysql.version>5.1.24</mysql.version>
          </properties>
      <dependencies>
          <dependency>
               <groupId>com.dubbo</groupId>
             <artifactId>dubbo-service</artifactId>
             <version>0.0.1-SNAPSHOT</version>
          </dependency>
      <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
          <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
   </dependencies>
  <build>
    <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-assembly-plugin</artifactId>
         <version>2.4</version>
         <configuration>
             <descriptors>
                 <descriptor>assembly/assembly.xml</descriptor>
             </descriptors>
         </configuration>
        <executions>
          <execution><!-- 配置执行器 -->
            <id>make-assembly</id>
            <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
            <goals>
              <goal>single</goal><!-- 只运行一次  -->
            </goals>
          </execution>
        </executions>
      </plugin>
      </plugins>

  </build>
</project>

log4j.properties

log4j.rootLogger=INFO, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=(%r ms) [%t] %-5p: %c#%M %x: %m%n
log4j.logger.com.genuitec.eclipse.sqlexplorer=WARN
log4j.logger.org.apache=WARN
log4j.logger.org.hibernate=WARN
log4j.logger.org.hibernate.sql=WARN
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=d:/logs/out.log
log4j.appender.R.MaxFileSize=1024KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

3.创建maven工程dubbo-consumer,该maven工程配置服务消费方

package com.dubbo.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.test.user.UserService;

public class User {


    //引入别的工程的类
    @Reference
    private UserService userService;

    public void test(){

        System.out.println(userService);
        String name = this.userService.getUserNameById("111");
        System.out.println(name);
    }

}

applicationContext-dubbo.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">



    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo_consumer"  />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.140.55:2181" />
    <dubbo:annotation   package="com.dubbo.controller"  />
    <bean id="testUser" class="com.dubbo.controller.User" /></beans>

pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dubbo</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>

      <properties>
          <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
          <spring.version>4.2.3.RELEASE</spring.version>
      </properties>

    <dependencies>
      <dependency>
           <groupId>com.dubbo</groupId>
         <artifactId>dubbo-service</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>


      <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion>
            <artifactId>spring</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>


          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>


                  <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>
        </dependencies>
  <build/>
</project>

4.启动服务的提供方

4.1启动zk

4.2在dubbo-service-impl工程中,写测试类启动spring容器

package com.test;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
        ac.start();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

日志输出:

(455 ms) [main] INFO : com.alibaba.dubbo.config.AbstractConfig#onApplicationEvent :  [DUBBO] The service ready on spring started. service: com.test.user.UserService, dubbo version: 2.5.3, current host: 127.0.0.1
(664 ms) [main] INFO : com.alibaba.dubbo.config.AbstractConfig#exportLocal :  [DUBBO] Export dubbo service com.test.user.UserService to local registry, dubbo version: 2.5.3, current host: 127.0.0.1
(664 ms) [main] INFO : com.alibaba.dubbo.config.AbstractConfig#doExportUrlsFor1Protocol :  [DUBBO] Export dubbo service com.test.user.UserService to url dubbo://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796, dubbo version: 2.5.3, current host: 127.0.0.1
(665 ms) [main] INFO : com.alibaba.dubbo.config.AbstractConfig#doExportUrlsFor1Protocol :  [DUBBO] Register dubbo service com.test.user.UserService url dubbo://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796 to registry registry://192.168.140.55:2181/com.alibaba.dubbo.registry.RegistryService?application=dubbo_provider&dubbo=2.5.3&pid=820&registry=zookeeper&timestamp=1575441533684, dubbo version: 2.5.3, current host: 127.0.0.1
(793 ms) [main] INFO : com.alibaba.dubbo.remoting.transport.AbstractServer#<init> :  [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.122.1:20880, dubbo version: 2.5.3, current host: 127.0.0.1
(808 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#loadProperties :  [DUBBO] Load registry store file C:\Users\chenzhubing\.dubbo\dubbo-registry-192.168.140.55.cache, data: {com.test.user.UserService=dubbo://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=2768&side=provider&timestamp=1575436705357 empty://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=configurators&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7944&side=consumer&timestamp=1575438202677 empty://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=routers&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7944&side=consumer&timestamp=1575438202677}, dubbo version: 2.5.3, current host: 127.0.0.1
(826 ms) [ZkClient-EventThread-12-192.168.140.55:2181] INFO : org.I0Itec.zkclient.ZkEventThread#run : Starting ZkClient event thread.
(849 ms) [main] INFO : org.I0Itec.zkclient.ZkClient#waitForKeeperState : Waiting for keeper state SyncConnected
(863 ms) [main-EventThread] INFO : org.I0Itec.zkclient.ZkClient#processStateChanged : zookeeper state changed (SyncConnected)
(864 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#register :  [DUBBO] Register: dubbo://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796, dubbo version: 2.5.3, current host: 127.0.0.1
(885 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#subscribe :  [DUBBO] Subscribe: provider://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&category=configurators&check=false&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796, dubbo version: 2.5.3, current host: 127.0.0.1
(908 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#notify :  [DUBBO] Notify urls for subscribe url provider://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&category=configurators&check=false&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796, urls: [empty://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&category=configurators&check=false&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796], dubbo version: 2.5.3, current host: 127.0.0.1

4.2在dubbo-consumer中,编写测试类,启动容器

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dubbo.controller.User;


public class Test {
    public static void main(String[] args) {


        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
        User user = ac.getBean("testUser",User.class);
        user.test();

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

日志输出:

(0 ms) [main] INFO : org.springframework.context.support.ClassPathXmlApplicationContext#prepareRefresh : Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6b93c5: startup date [Wed Dec 04 14:41:20 CST 2019]; root of context hierarchy
(41 ms) [main] INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions : Loading XML bean definitions from class path resource [applicationContext-dubbo.xml]
(151 ms) [main] INFO : com.alibaba.dubbo.common.logger.LoggerFactory#? : using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
(551 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#loadProperties :  [DUBBO] Load registry store file C:\Users\chenzhubing\.dubbo\dubbo-registry-192.168.140.55.cache, data: {com.test.user.UserService=empty://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&category=configurators&check=false&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796}, dubbo version: 2.5.3, current host: 127.0.0.1
(570 ms) [ZkClient-EventThread-10-192.168.140.55:2181] INFO : org.I0Itec.zkclient.ZkEventThread#run : Starting ZkClient event thread.
(602 ms) [main] INFO : org.I0Itec.zkclient.ZkClient#waitForKeeperState : Waiting for keeper state SyncConnected
(619 ms) [main-EventThread] INFO : org.I0Itec.zkclient.ZkClient#processStateChanged : zookeeper state changed (SyncConnected)
(645 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#register :  [DUBBO] Register: consumer://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=consumers&check=false&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7404&side=consumer&timestamp=1575441681010, dubbo version: 2.5.3, current host: 192.168.122.1
(665 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#subscribe :  [DUBBO] Subscribe: consumer://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=providers,configurators,routers&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7404&side=consumer&timestamp=1575441681010, dubbo version: 2.5.3, current host: 192.168.122.1
(702 ms) [main] INFO : com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry#notify :  [DUBBO] Notify urls for subscribe url consumer://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=providers,configurators,routers&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7404&side=consumer&timestamp=1575441681010, urls: [dubbo://192.168.122.1:20880/com.test.user.UserService?anyhost=true&application=dubbo_provider&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=820&side=provider&timestamp=1575441533796, empty://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=configurators&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7404&side=consumer&timestamp=1575441681010, empty://192.168.122.1/com.test.user.UserService?application=dubbo_consumer&category=routers&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7404&side=consumer&timestamp=1575441681010], dubbo version: 2.5.3, current host: 192.168.122.1
(822 ms) [main] INFO : com.alibaba.dubbo.remoting.transport.AbstractClient#connect :  [DUBBO] Successed connect to server /192.168.122.1:20880 from NettyClient 192.168.122.1 using dubbo version 2.5.3, channel is NettyChannel [channel=[id: 0x0119e736, /192.168.122.1:52208 => /192.168.122.1:20880]], dubbo version: 2.5.3, current host: 192.168.122.1
(822 ms) [main] INFO : com.alibaba.dubbo.remoting.transport.AbstractClient#<init> :  [DUBBO] Start NettyClient chenzhubing-PC/192.168.122.1 connect to the server /192.168.122.1:20880, dubbo version: 2.5.3, current host: 192.168.122.1
(876 ms) [main] INFO : com.alibaba.dubbo.config.AbstractConfig#createProxy :  [DUBBO] Refer dubbo service com.test.user.UserService from url zookeeper://192.168.140.55:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=dubbo_consumer&check=false&dubbo=2.5.3&interface=com.test.user.UserService&methods=getUserNameById&pid=7404&side=consumer&timestamp=1575441681010, dubbo version: 2.5.3, current host: 192.168.122.1
com.alibaba.dubbo.common.bytecode.proxy0@1ebf294
name:111
12-17 17:17