-->
侧边栏壁纸
博主头像
断钩鱼 博主等级

行动起来,活在当下

  • 累计撰写 28 篇文章
  • 累计创建 34 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Dubbo的基本使用

halt
2021-11-02 / 0 评论 / 1 点赞 / 1753 阅读 / 0 字

Dubbo的基本使用

简介

Apache Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时 Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。

官方地址:http://dubbo.apache.org/zh/

注册与发现模型

服务注册与发现模型

节点角色说明
节点角色说明
Provider暴露服务的服务提供方
Consumer调用远程服务的服务消费方
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心
Container服务运行容器
调用关系说明
  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

配置

详细信息

https://dubbo.apache.org/zh/docs/references/xml/

标签用途解释
<dubbo:service/>服务配置用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心
<dubbo:reference/> 2引用配置用于创建一个远程服务代理,一个引用可以指向多个注册中心
<dubbo:protocol/>协议配置用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受
<dubbo:application/>应用配置用于配置当前应用信息,不管该应用是提供者还是消费者
<dubbo:module/>模块配置用于配置当前模块信息,可选
<dubbo:registry/>注册中心配置用于配置连接注册中心相关信息
<dubbo:monitor/>监控中心配置用于配置连接监控中心相关信息,可选
<dubbo:provider/>提供方配置当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选
<dubbo:consumer/>消费方配置当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选
<dubbo:method/>方法配置用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息
<dubbo:argument/>参数配置用于指定方法参数配置

SpringBoot整合dubbo的简单使用

目录结构

image-20211102104130107

创建父工程

定义依赖:

父项目集中管理依赖&版本 子项目按需引入即可

<?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.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.mengshuo</groupId>
    <artifactId>dubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
   	
    <modules>
        <module>base-api</module>
        <module>consumer</module>
        <module>provider</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <dubbo.version>3.0.4</dubbo.version>
        <base.api.version>1.0</base.api.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>top.mengshuo</groupId>
                <artifactId>base-api</artifactId>
                <version>${base.api.version}</version>
            </dependency>
            <!-- dubbo -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <!-- 注册中心依赖 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <type>pom</type>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <type>pom</type>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

创建公共API服务

主要用来定义service

<?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">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>top.mengshuo</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>base-api</artifactId>


</project>

添加一个简单接口

package top.mengshuo.base.service;

/**
 * @author mengshuo
 * @since 2021-11-01
 */
public interface HelloService {
    /**
     * 简单demo方法
     *
     * @param name name
     * @return res
     */
    String simpleMethod(String name);
}

创建服务提供者

定义依赖:

提供者 ( provider ) 提供服务,供服务消费者 ( consumer ) 使用

此服务提供者不会消费其他服务,也不会提供rest服务,故没有web依赖

<?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">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>top.mengshuo</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>provider</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>top.mengshuo</groupId>
            <artifactId>base-api</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

添加接口实现

dubbo2.7.7 之后 使用 @org.apache.dubbo.config.annotation.DubboService 标注service服务

@com.alibaba.dubbo.config.annotation.Service && org.apache.dubbo.config.annotation.Service 均已弃用

package top.mengshuo.serivce.impl;

import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import top.mengshuo.base.service.HelloService;

/**
 * @author mengshuo
 * @since 2021-11-01
 */
@DubboService
public class HelloServiceImpl implements HelloService {
    /**
     * 简单demo方法
     *
     * @param name name
     * @return res
     */
    @Override
    public String simpleMethod(String name) {
        return "hello " + name;
    }
}

启用dubbo:

标注 @org.apache.dubbo.config.spring.context.annotation.EnableDubbo

package top.mengshuo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

/**
 * @author mengshuo
 * @since 2021-11-01
 */
@EnableDubbo
@SpringBootApplication
public class ProviderRun {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ProviderRun.class, args);
    }
}

配置文件

dubbo:
  application:
    name: default_provider
  # 注册中心配置
  registry:
    # 配置格式
    # address: 注册中心协议://地址:端口?timeout=连接超时时间, 也可以用下面这种配置方式
    # 注册中心协议
    protocol: zookeeper
    # 注册中心地址
    address: 192.168.59.101
    # 端口
    port: 2181
    # 超时时间
    timeout: 60000
    # 注册简化版的URL到Registry
    simplified: true
  # 协议配置
  protocol:
    # 协议名称
    name: dubbo
    # 端口 -1 自动协商
    port: 20880

服务消费者

定义依赖:

定义依赖

<?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">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>top.mengshuo</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>consumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>top.mengshuo</groupId>
            <artifactId>base-api</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

添加controller测试

dubbo 2.7.7后使用 @org.apache.dubbo.config.annotation.DubboReference 引用service

@com.alibaba.dubbo.config.annotation.Reference && @org.apache.dubbo.config.annotation.Reference 也都已弃用

package top.mengshuo.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mengshuo.base.service.HelloService;

/**
 * @author mengshuo
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @DubboReference
    private HelloService helloService;

    @GetMapping("/{name}")
    public String hello(@PathVariable String name){
        return this.helloService.simpleMethod(name);
    }
}

启用dubbo:

标注 @org.apache.dubbo.config.spring.context.annotation.EnableDubbo

package top.mengshuo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author mengshuo
 * @since 2021-10-31
 */
@EnableDubbo
@SpringBootApplication
public class ConsumerRun {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ConsumerRun.class, args);
    }
}

配置文件

注册中心超时时间应适当设置太小容易无法连接

server:
  port: 8002

dubbo:
  # 应用配置
  application:
    # 应用名称
    name: default_consumer
  registry:
    # 配置格式
    # address: 注册中心协议://地址:端口?timeout=连接超时时间, 也可以用下面这种配置方式
    # 注册中心协议
    protocol: zookeeper
    # 注册中心地址
    address: 192.168.59.101
    # 端口
    port: 2181
    # 超时时间
    timeout: 60000
    # 注册简化版的URL到Registry
    simplified: true
  # 消费者配置
  consumer:
    # 超时配置
    timeout: 3000
    # 重试次数
    retries: 3

测试结果

image-20211102111652821

Demo地址

https://e.coding.net/duangouyu/demo/dubbo_demo.git

1
博主关闭了所有页面的评论