• Dubbo Gateway - 网关设计


    背景

    先说结论dubbo目前版本(2.7.1 & 3.X)无法使用开源网关组件。

    为什么?

    在这里插入图片描述

    通过Dubbo服务与注册的设计可以看出Dubbo服务的基本特点:

    1. 注册/发现对象 - Dubbo服务接口
    2. 注册/发现载体 - Dubbo URL (元信息:接口、版本、分组等)

    一个Dubbo URL示例 ↓↓↓

    dubbo://192.168.50.233:20880/com.paranoia.api.HelloServiceanyhost=true&application=dubbo-provider&bean.name=providers:dubbo:com.paranoia.api.HelloService&default.deprecated=false&default.dynamic=false&default.register=true&deprecated=false&dubbo=2.0.2&dynamic=false&generic=false&interface=com.paranoia.api.HelloService&methods=hello&pid=8744®ister=true&release=2.7.1&side=provider×tamp=1559126008855

    也就可以看出Dubbo中没有微服务服务自省的概念。

    服务自省:简单讲就是微服务架构中,注册中心管理的对象是应用(服务),而非对外的服务接口。

    Dubbo计划在2.7.3版本实现"服务自省"。

    在这里插入图片描述

    实现“服务自省”有什么好处?

    • 拓展Dubbo生态,接洽SC生态的部分开源组件(eureka/consul、zuul/gateway、zipkin…)
    • 降低Dubbo注册中心压力(一个服务/一个服务下N个service)

    等不到,刚需

    Plan A:

    Dubbo + SpringCloud + Zookeeper + Nacos

    Plan B:

    Dubbo + SpringCloud + Zookeeper

    Plan C:

    Dubbo(2.7.2) + SpringCloud + Nacos

    Plan D:

    Dubbo(2.7.3) + Spring-Cloud-Gateway + Nacos

    拓展:为什么要把注册中心从 Zookeeper 迁移到 Nacos

    从降低运维成本,提高服务稳定性的角度讲,我们不希望维护两套注册中心,所以我们最终好像大概也许可能要选择Plan B.

    Dubbo2.7.2已经支持nacos的元数据配置,也就是说我们等六月初RELEASE正式发布,就可以使用一个Plan C : Dubbo+Sppring Cloud + Nacos 。 但无奈项目中使用的Dubbo 3.x版本并没有这个规划…,Plan D 也是这个问题

    分层设计

    graph TD;
        Spring-Cloud-Gateway-->facade-0;
        Spring-Cloud-Gateway-->facade-1;
        facade-0-->dubbo-provider-0;
        facade-0-->dubbo-provider-1;
        facade-1-->dubbo-provider-0;
        facade-1-->dubbo-provider-1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    项目

    定位

    Plan A

    Plan B

    Gateway

    网关

    Spring Cloud GateWay + Nacos

    Spring Cloud GateWay + zk

    facade

    业务逻辑层

    Spring Cloud + Nacos + Dubbo + zk

    Spring Cloud + Dubbo + zk

    'demo无'

    公共逻辑层

    dubbo-provider

    服务层

    dubbo + zk

    dubbo + zk


    可行性

    Plan A : Dubbo+SpringCloud+Zookeeper+Nacos

    dubbo-provider

    pom

            
                com.paranoia
                api
                1.0-SNAPSHOT
            
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
                org.apache.dubbo
                dubbo
            
            
                org.apache.zookeeper
                zookeeper
            
            
                com.github.sgroschupf
                zkclient
            
            
                org.springframework.cloud
                spring-cloud-starter-zookeeper-config
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
            
                org.apache.httpcomponents
                httpclient
                4.5.2
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    service

    import org.apache.dubbo.config.annotation.Service;
    @Service
    public class HelloServiceImpl implements HelloService {
        public String hello(String name) {
            return "hi , how are you ? my friend " + name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    启动类添加注解

    @EnableDubbo(scanBasePackages = "com.paranoia.demo.service")
    
    • 1

    配置文件

    server:
      port: 8081
    
    spring:
      profiles:
        active: dev
      application:
        name: dubbo-provider
      cloud:
        zookeeper:
          client:
            sasl: false
          enabled: true
          connect-string: localhost:2181
    
    dubbo:
      application.name: ${spring.application.name}
      registry.address: zookeeper://127.0.0.1:2181
      config-center.address: zookeeper://127.0.0.1:2181
      consumer.timeout: 3000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    SCA-facade

    pom:同时集成SpringCloud和Dubbo

    
                org.springframework.cloud
                spring-cloud-starter-alibaba-nacos-discovery
                0.9.0.RELEASE
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
            
                org.springframework.cloud
                spring-cloud-commons
                2.1.1.RELEASE
            
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
            
                org.apache.dubbo
                dubbo
            
            
                org.apache.zookeeper
                zookeeper
            
            
                com.github.sgroschupf
                zkclient
            
            
                org.springframework.cloud
                spring-cloud-starter-zookeeper-config
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
            
            
                com.paranoia
                api
                1.0-SNAPSHOT
            
            
                org.projectlombok
                lombok
                true
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    rest接口:使用dubbo-provider中的服务

    import org.apache.dubbo.config.annotation.Reference;
    @RestController
    public class HelloController {
    
        @Reference
        HelloService helloService;
    
        @GetMapping("/hi")
        public String sayHi(@RequestParam String name) {
            System.out.println("SCA-facade get request name = " + name);
            return helloService.hello(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    启动类注解

    @EnableDubbo
    @EnableDiscoveryClient
    
    • 1
    • 2

    配置文件
    配置nacos作为CLoud的注册中心,
    配置zk作为Dubbo的注册中心

    spring:
      application:
        name: SCA-FACADE
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        zookeeper:
          client:
            asl: false
          enabled: true
          connect-string: localhost:2181
    server:
      port: 8084
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    dubbo:
      application.name: ${spring.application.name}
      registry.address: zookeeper://127.0.0.1:2181
      config-center.address: zookeeper://127.0.0.1:2181
      consumer.timeout: 3000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    SCA-gateway

    我们采用Spring-Cloud-Gateway作为Cloud的网关

    pom

            
                org.springframework.cloud
                spring-cloud-starter-gateway
            
            
                org.springframework.cloud
                spring-cloud-gateway-webflux
            
            
                org.springframework.cloud
                spring-cloud-starter-alibaba-nacos-discovery
                0.9.0.RELEASE
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    启动类添加注解

    @EnableDiscoveryClient
    
    • 1

    配置文件
    这里我们添加一个routes规则:GET请求&&path="/hi"的请求转发到SCA-FACADE服务

    server:
      port: 8083
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    spring:
      application:
        name: SC-gateway
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        gateway:
          default-filters:
          routes:
            - id: zk_route
              uri: lb://SCA-FACADE
                # path中是server服务中提供的接口
                # Method : 指定访问的请求类型
              predicates:
                  - Path=/hi
                  - Method=GET
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    facade需要在底层服务启动之后再启动,否则nacos会寻址失败导致项目启动失败。

    检验负载均衡

    两个dubbo-provider服务,两个facde服务,一个gateway服务

    IMAGE

    PORTS

    NAMES

    dubbo-provider

    8080/tcp, 0.0.0.0:18081->8081/tcp

    dubbo-provider-0

    dubbo-provider

    8080/tcp, 0.0.0.0:8081->8081/tcp

    dubbo-provider-1

    facade

    8080/tcp, 0.0.0.0:8084->8084/tcp

    facade-0

    facade

    8080/tcp, 0.0.0.0:18084->8084/tcp

    facade-1

    sc-gateway

    8080/tcp, 0.0.0.0:8083->8083/tcp

    gateway-0

    多次请求网关地址:

    curl http://47.106.221.***:8083/hiname=dubbo-gateway

    查看各个image控制台,发现无论是spring-cloud的还是dubbo的负载均衡都没有问题。

    Plan B : Dubbo+SpringCloud+Zookeeper+Nacos

    SC-facase-zk

    相比Plan A ,facade只需要修改配置中心依赖以及配置文件

    pom : 去掉naocs-dicovery 替换成 zk-discovery

            
                org.springframework.cloud
                spring-cloud-starter-zookeeper-discovery
                2.1.1.RELEASE
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置文件:

    spring:
      application:
        name: SCA-FACADE-ZK
      cloud:
        #替换nacos为zk
        zookeeper:
          client:
            asl: false
          enabled: true
          connect-string: 47.106.221.253:2181
          discovery:
            enabled: true
            register: true
    
    server:
      port: 8084
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    dubbo:
      application.name: ${spring.application.name}
      registry.address: zookeeper://47.106.221.253:2181
      config-center.address: zookeeper://47.106.221.253:2181
      consumer.timeout: 3000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    SC-gateway-zk

    pom : 同SC-facade-zk

            
                org.springframework.cloud
                spring-cloud-starter-zookeeper-discovery
                2.1.1.RELEASE
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置文件

    server:
      port: 8081
    
    spring:
      application:
        name: dubbo-provider
      cloud:
        #替换nacos为zk
        zookeeper:
          client:
            sasl: false
          enabled: true
          connect-string: 47.106.221.253:2181
    
    dubbo:
      application.name: ${spring.application.name}
      registry.address: zookeeper://47.106.221.253:2181
      config-center.address: zookeeper://47.106.221.253:2181
      consumer.timeout: 3000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    启动服务dubbo-provider , sc-facase-zk , sc-gateway-zk

    查看zk中数据:

    在这里插入图片描述

  • 相关阅读:
    面向对象设计原则-一句话总结设计原则
    mysql优化之索引
    Effective C++条款09:绝不在构造和析构过程中调用virtual函数
    STM32:GPIO功能描述和工作方式
    [附源码]计算机毕业设计springboot本地助农产品销售系统
    干货 | 一文搞定 pytest 自动化测试框架
    Games104现代游戏引擎笔记 面向数据编程与任务系统
    文本层次语义元素
    【网上教学】实现线上签到和收批作业的方法
    可落地的DDD(6)-工程结构
  • 原文地址:https://blog.csdn.net/sebeefe/article/details/126327728