Javadubbo

Dubbo3开发-简介和示例

2022-09-01  本文已影响0人  ElliotG

1. Dubbo3 简介

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

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

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

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

Dubbo3 提供的基础能力包括:

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

 

2. Dubbo3新特性

 

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) 运行

提供方.png
消费方.png
上一篇 下一篇

猜你喜欢

热点阅读