Apache Dubbo Zookeeper
什么是 Dubbo
概述
| Apache Dubbo (incubating) | ˈdʌbəʊ | 是一款高性能、轻量级的开源 Java RPC 分布式服务框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。她最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo 采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。 | 
- 官网:http://dubbo.apache.org/zh-cn
- GitHub:http://github.com/apache/incubator-dubbo
Dubbo 的服务治理
概述

| 特性 | 描述 | 
|---|---|
| 透明远程调用 | 就像调用本地方法一样调用远程方法;只需简单配置,没有任何 API 侵入 | 
| 负载均衡机制 | Client 端 LB,可在内网替代 F5 等硬件负载均衡器 | 
| 容错重试机制 | 服务 Mock 数据,重试次数、超时机制等 | 
| 自动注册发现 | 注册中心基于接口名查询服务提 供者的 IP 地址,并且能够平滑添加或删除服务提供者 | 
| 性能日志监控 | Monitor 统计服务的调用次调和调用时间的监控中心 | 
| 服务治理中心 | 路由规则,动态配置,服务降级,访问控制,权重调整,负载均衡,等手动配置 | 
| 自动治理中心 | 无,比如:熔断限流机制、自动权重调整等 | 
Dubbo 的核心功能
概述
- Remoting:远程通讯,提供对多种 NIO 框架抽象封装,包括“同步转异步”和“请求-响应”模式的信息交换方式。
- Cluster:服务框架,提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
- Registry:服务注册中心,服务自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
Dubbo 的组件角色
概述

| 组件角色 | 说明 | 
|---|---|
| Provider | 暴露服务的服务提供方 | 
| Consumer | 调用远程服务的服务消费方 | 
| Registry | 服务注册与发现的注册中心 | 
| Monitor | 统计服务的调用次调和调用时间的监控中心 | 
| Container | 服务运行容器 | 
调用关系说明:
- 服务容器 Container负责启动,加载,运行服务提供者。
- 服务提供者 Provider在启动时,向注册中心注册自己提供的服务。
- 服务消费者 Consumer在启动时,向注册中心订阅自己所需的服务。
- 注册中心 Registry返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者 Consumer,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者 Consumer和提供者Provider,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心Monitor。
Dubbo Admin 管理控制台
概述
管理控制台为内部裁剪版本,开源部分主要包含:路由规则,动态配置,服务降级,访问控制,权重调整,负载均衡,等管理功能。
GitHub:http://github.com/apache/incubator-dubbo-ops

# 打包
mvn clean package
# 运行
mvn --projects dubbo-admin-backend spring-boot:run
# 浏览
http://localhost:8080
遇到的问题处理
NodeJS
- 现象:使用 mvn clean package构建 DubboAdmin 控制台时会出现npm install操作
- 解决:新版控制台已改为前后分离模式,前端采用 Vue.js 开发,故需要 NodeJS 支持,请自行安装(运行到此处时会自动下载安装)。官网地址:http://nodejs.cn/
- 其他:配置淘宝镜像加速。官网地址:http://npm.taobao.org/
# 安装 cnpm 命令行工具
npm install -g cnpm --registry=http://registry.npm.taobao.org
# 安装模块
cnpm install [name]
Will not attempt to authenticate using SASL (unknown error)
- 现象:使用 mvn --projects dubbo-admin-backend spring-boot:run启动 DubboAdmin 控制台时,控制台日志中出现Will not attempt to authenticate using SASL (unknown error)提示
- 解决:修改 C:\Windows\System32\drivers\etc\hosts文件,增加192.168.10.131 ubuntu16即可解决
注意: 此处的 192.168.10.131 为 Zookeeper 地址
第一个 Dubbo 应用程序
概述

创建服务接口项目
创建一个名为 hello-dubbo-service-user-api 的项目,该项目只负责定义接口
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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.funtl</groupId>
    <artifactId>hello-dubbo-service-user-api</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
</project>
定义服务接口
package com.funtl.hello.dubbo.service.user.api;
public interface UserService {
    String sayHi();
}
创建服务提供者项目
创建一个名为 hello-dubbo-service-user-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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sdx</groupId>
    <artifactId>hello-dubbo-service-user-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>hello-dubbo-service-user-provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.sdx</groupId>
            <artifactId>hello-dubbo-service-user-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
其中 dubbo-dependencies-zookeeper包含以下依赖
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
</dependency>
主要增加了以下依赖:
- org.apache.dubbo:dubbo-spring-boot-starter:2.7.1:Dubbo Starter
- org.apache.dubbo:dubbo-spring-boot-actuator:2.7.1:Dubbo 的服务状态检查
- com.sdx:hello-dubbo-service-user-api:1.0.0-SNAPSHOT:刚才创建的接口项目,如果无法依赖别忘记先- mvn clean install到本地仓库。
通过 @Service 注解实现服务提供方
package com.sdx.hello.dubbo.service.user.provider.api.impl;
import com.funtl.hello.dubbo.service.user.api.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
/**
 * @author sdx
 * @version 1.0.0
 * @description
 * @date 2019/7/22
 */
@Service(version = "1.0.0")
@Component
public class UserServiceImpl implements UserService {
    @Override
    public String sayHi() {
        return "hello dubbo";
    }
}
Application
package com.sdx.hello.dubbo.service.user.provider;
import org.apache.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloDubboServiceUserProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloDubboServiceUserProviderApplication.class, args);
        Main.main(args);
    }
}
application.yml
# Spring boot application
spring:
  application:
    name: hello-dubbo-service-user-provider
# UserService service version
user:
  service:
    version: 1.0.0
# Dubbo Config properties
dubbo:
  configcenter:
     zookeeper: //127:0:0:1:2181
  ## Base packages to scan Dubbo Component:@com.alibaba.dubbo.config.annotation.Service
  scan:
    basePackages: com.sdx.hello.dubbo.service.user.provider.api
  ## ApplicationConfig Bean
  application:
    id: hello-dubbo-service-user-provider
    name: hello-dubbo-service-user-provider
    qos-port: 22222
    qos-enable: true
  ## ProtocolConfig Bean
  protocol:
    id: dubbo
    name: dubbo
    port: 12347
    status: server
  ## RegistryConfig Bean
  registry:
    id: zookeeper
    address: zookeeper://127.0.0.1:2181
    metadata-report:
      address: zookeeper://127.0.0.1:2181
# Enables Dubbo All Endpoints
management:
  endpoint:
    dubbo:
      enabled: true
    dubbo-shutdown:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-properties:
      enabled: true
  # Dubbo Health
  health:
    dubbo:
      status:
        ## StatusChecker Name defaults (default : "memory", "load" )
        defaults: memory
        ## StatusChecker Name extras (default : empty )
        extras: load,threadpool
创建服务消费者项目
创建一个名为 hello-dubbo-service-user-consumer 的项目,该项目用于消费接口(调用接口)
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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sdx</groupId>
    <artifactId>hello-dubbo-service-user-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-dubbo-service-user-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.sdx</groupId>
            <artifactId>hello-dubbo-service-user-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
通过 @Reference 注入 UserService
package com.sdx.hello.dubbo.service.user.consumer.controller;
import com.funtl.hello.dubbo.service.user.api.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author sdx
 * @version 1.0.0
 * @description
 * @date 2019/7/22
 */
@RestController
public class UserController {
    @Reference(version = "1.0.0")
    private UserService userService;
    @RequestMapping(value = "hi")
    public String sayHi() {
        return userService.sayHi();
    }
}
Application
package com.sdx.hello.dubbo.service.user.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloDubboServiceUserConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloDubboServiceUserConsumerApplication.class, args);
    }
}
application.yml
# Spring boot application
spring:
  application:
    name: hello-dubbo-service-user-consumer
server:
  port: 9092
# UserService service version
# Dubbo Config properties
dubbo:
  scan:
    basePackages: com.sdx.hello.dubbo.service.user.consumer
  ## ApplicationConfig Bean
  application:
    id: hello-dubbo-service-user-consumer
    name: hello-dubbo-service-user-consumer
  ## RegistryConfig Bean
  registry:
    id: zookeeper
    address: zookeeper://localhost:2181
# Dubbo Endpoint (default status is disable)
endpoints:
  dubbo:
    enabled: true
management:
  server:
    port: 9091
  # Dubbo Health
  health:
    dubbo:
      status:
        ## StatusChecker Name defaults (default : "memory", "load" )
        defaults: memory
  # Enables Dubbo All Endpoints
  endpoint:
    dubbo:
      enabled: true
    dubbo-shutdown:
      enabled: true
    dubbo-configs:
      enabled: true
    dubbo-services:
      enabled: true
    dubbo-references:
      enabled: true
    dubbo-properties:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
启动 Dubbo Admin 控制台
查看是否成功注册服务,效果图如下:
