Dubbo3开发-简介和示例
2024-09-02 00:00:05  阅读数 2194

1. Dubbo3 简介

Apache Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。

使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。

Dubbo3 基于 Dubbo2 演进而来,在保持原有核心功能特性的同时, Dubbo3 在易用性、超大规模微服务实践、云原生基础设施适配、安全设计等几大方向上进行了全面升级。

Dubbo 提供了构建云原生微服务业务的一站式解决方案,可以使用 Dubbo 快速定义并发布微服务组件,同时基于 Dubbo 开箱即用的丰富特性及超强的扩展能力,构建运维整个微服务体系所需的各项服务治理能力。

Dubbo3 提供的基础能力包括:

  • 服务发现
  • 流式通信
  • 负载均衡
  • 流量治理
  • 超大规模微服务集群实践
    高性能的跨进程通信协议
    地址发现、流量治理层面,轻松支持百万规模集群实例

Dubbo 计划提供丰富的多语言客户端实现,其中 Java、Golang 版本是当前稳定性、活跃度最好的版本,其他多语言客户端正在持续建设中。

 

2. Dubbo3新特性

  • 全新服务发现模型
    相比于 2.x 版本中的基于接口粒度的服务发现机制,3.x 引入了全新的基于应用粒度的服务发现机制。
    好处:
    进一步提升了 Dubbo3 在大规模集群实践中的性能与稳定性。
    打通与其他异构微服务体系的地址互发现障碍(Spring Cloud、Kubernetes Service、gRPC 等)。

  • 下一代 RPC 通信协议
    定义了全新的 RPC 通信协议 – Triple。
    Triple是基于 HTTP/2 上构建的 RPC 协议,完全兼容 gRPC,并在此基础上扩展出了更丰富的语义。
    使用 Triple 协议,用户将获得以下能力:

    • 更容易到适配网关、Mesh架构,Triple 协议让 Dubbo 更方便的与各种网关、Sidecar 组件配合工作。
    • 多语言友好,推荐配合 Protobuf 使用 Triple 协议,使用 IDL 定义服务,使用 Protobuf 编码业务数据。
    • 流式通信支持。Triple 协议支持 Request Stream、Response Stream、Bi-direction Stream。
  • 扩展点分离
    Dubbo3 的 maven 也发生了一些变化,org.apache.dubbo:dubbo:3.0.0 将不再是包含所有资源的 all-in-one 包,一些可选的依赖已经作为独立组件单独发布, 因此如果用户使用了不在 dubbo 核心依赖包中的独立组件,如 registry-etcd、rpc-hessian 等,需要为这些组件在 pom.xml 中单独增加依赖包。

 

3. 简单案例

3-1) 新建一个包含多模块的pom父子项目dubble-samples-spring-boot,结构如下


├── dubbo-samples-spring-boot-consumer
│ ├── pom.xml
│ └── src
├── dubbo-samples-spring-boot-interface
│ ├── pom.xml
│ └── src
├── dubbo-samples-spring-boot-provider
│ ├── pom.xml
│ └── src
└── pom.xml


3-2) 添加关键maven依赖

<properties>
        <dubbo.version>3.0.7</dubbo.version>
        <junit.version>4.13.1</junit.version>
        <slf4j-log4j12.version>1.7.25</slf4j-log4j12.version>
        <spring-boot.version>2.3.1.RELEASE</spring-boot.version>
        <spring-boot-maven-plugin.version>2.1.4.RELEASE</spring-boot-maven-plugin.version>
</properties>
...
<dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

提供方和消费方的关键依赖

<!-- dubbo -->
        <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>

        <!-- dubbo starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <!-- spring starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>


3-3) 定义服务接口
在dubbo-samples-spring-boot-interface模块中定义服务接口。

DemoService.java

package org.apache.dubbo.springboot.demo;

import java.util.concurrent.CompletableFuture;

public interface DemoService {

    String sayHello(String name);

}


3-4) 提供方实现接口并暴露服务
dubbo-samples-spring-boot-provider模块中编写服务接口提供方的实现。

DemoServiceImpl.java

package com.mcp.dubbo.springboot.demo.provider;


import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.springboot.demo.DemoService;

@DubboService
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name;
    }

}

注: 其中特别注意@DubboService注解。


3-5) 配置提供方的application.yml 文件

dubbo-samples-spring-boot-provider/resources/application.yml

dubbo:
  application:
    name: dubbo-springboot-demo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    id: zk-registry
    address: zookeeper://127.0.0.1:2181
  config-center:
    address: zookeeper://127.0.0.1:2181
  metadata-report:
    address: zookeeper://127.0.0.1:2181

3-6) 定义服务提供方 Spring Boot 主函数

dubbo-samples-spring-boot-provider/ProviderApplication.java

package com.mcp.dubbo.springboot.demo.provider;


import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
    public static void main(String[] args) throws Exception {
        // comment this out if you are running zookeeper on your local env
        //new EmbeddedZooKeeper(2181, false).start();

        SpringApplication.run(ProviderApplication.class, args);
        System.out.println("dubbo service started");
        new CountDownLatch(1).await();
    }
}
  1. @EnableDubbo必须配置。
  2. EmbeddedZooKeeper这句可以注释如果你使用的本地或远程外置安装的ZooKeeper。


3-7) 引用远程服务,加载 Spring 配置,并调用远程服务

dubbo-samples-spring-boot-consumer模块中编写服务消费方的实现。

dubbo-samples-spring-boot-consumer/ConsumerApplication.java

package com.mcp.dubbo.springboot.demo.consumer;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.springboot.demo.DemoService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Service;

@SpringBootApplication
@Service
@EnableDubbo
public class ConsumerApplication {

    @DubboReference
    private DemoService demoService;

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
        ConsumerApplication application = context.getBean(ConsumerApplication.class);
        String result = application.doSayHello("KG");
        System.out.println("result: " + result);
    }

    public String doSayHello(String name) {
        return demoService.sayHello(name);
    }
}
  1. @DubboReference注意特别注意,它类似远程的@Autowired注解来注入远程服务。


3-8) 配置消费方的application.yml 文件

dubbo-samples-spring-boot-consumer/application.yml

dubbo:
  application:
    name: dubbo-springboot-demo-consumer
  protocol:
    name: dubbo
    port: -1
  registry:
    id: zk-registry
    address: zookeeper://127.0.0.1:2181
  config-center:
    address: zookeeper://127.0.0.1:2181
  metadata-report:
    address: zookeeper://127.0.0.1:2181


3-8) 运行

  • 先启动服务提供方ProviderApplication。
  • 再启动服务消费方ConsumerApplication。
  • 运行结果如下:
提供方.png


消费方.png