搭建Apache Dubbo工程

Posted cnbai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建Apache Dubbo工程相关的知识,希望对你有一定的参考价值。

此项目是Spring Cloud Alibaba 和 Dubbo 整合 Demo

版本

Apache Dubbo Demo

JDK 1.8

Dubbo 2.7.2

Nacos Client 1.0.0

Spring Boot 2.1.6.RELEASE

Dubbo Registry Nacos 2.6.6

Dubbo Spring Boot Starter 2.7.1

Spring Cloud Greenwich.SR2

Spring Cloud Alibaba 2.1.0.RELEASE

Alibaba Spring Context Support 1.0.2

Spring Cloud Alibaba Nacos Config 2.1.0.RELEASE

创建父工程 apache-dubbo-demo

此工程只负责管理模块

一、项目结构

技术图片

二、pom.xml

<?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/>
    </parent>

    <groupId>com.cnbai</groupId>
    <artifactId>apache-dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <url>http://www.cnbai.xyz</url>

    <description>此工程只负责管理模块 module </description>

    <modules>
        <!-- 管理依赖 -->
        <module>apache-dubbo-dependencies</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <!-- 编译到源码版本 -->
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <!-- 编译到target版本 -->
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <!-- 整个项目的源码编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 输出编码 -->
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <!-- 开源协议(Apache 2.0) -->
    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <!-- 开发者 -->
    <developers>
        <developer>
            <id>cnbai</id>
            <name>cnbai</name>
            <email>baihaibo1024@163.com</email>
        </developer>
    </developers>

    <!-- 管理依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.cnbai</groupId>
                <artifactId>apache-dubbo-dependencies</artifactId>
                <version>${project.version}</version>
                <type>pom</type>
                <!-- 实现多继承 -->
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 多环境配置 -->
    <profiles>
        <profile>
            <!-- 自定义名称 如:dev、pro、release ... -->
            <id>default</id>
            <!-- 默认激活 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <!-- 仅当前环境生效 -->
            <properties>
                <spring-javaformat.version>0.0.12</spring-javaformat.version>
            </properties>
            <build>
                <plugins>
                    <!-- java格式化插件,以Spring编码规范格式化代码 -->
                    <plugin>
                        <groupId>io.spring.javaformat</groupId>
                        <artifactId>spring-javaformat-maven-plugin</artifactId>
                        <version>${spring-javaformat.version}</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/*Tests.java</include>
                            </includes>
                            <excludes>
                                <exclude>**/Abstract*.java</exclude>
                            </excludes>
                            <systemPropertyVariables>
                                <java.security.egd>file:/dev/./urandom</java.security.egd>
                                <java.awt.headless>true</java.awt.headless>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-enforcer-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>enforce-rules</id>
                                <goals>
                                    <goal>enforce</goal>
                                </goals>
                                <configuration>
                                    <rules>
                                        <bannedDependencies>
                                            <excludes>
                                                <exclude>commons-logging:*:*</exclude>
                                            </excludes>
                                            <searchTransitive>true</searchTransitive>
                                        </bannedDependencies>
                                    </rules>
                                    <fail>true</fail>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                        <inherited>true</inherited>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <!-- 配置远程仓库 -->
    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Milestone</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!-- 插件仓库 -->
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <name>Spring Milestone</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

统一依赖管理 apache-dubbo-dependencies

此工程只负责管理依赖

一、项目结构

技术图片

二、pom.xml

<?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.cnbai</groupId>
    <artifactId>apache-dubbo-dependencies</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <url>http://www.cnbai.xyz</url>

    <description>此工程只负责管理依赖 dependency </description>

    <properties>
        <!-- 
        	Springboot 2.2.x 必须用 Hoxton,Springboot 2.1.x 必须用 Greenwich
        	版本尽量别修改,改个小版本都有可能会报错
         -->
        <dubbo.version>2.7.2</dubbo.version>
        <dubbo-kryo.version>2.7.2</dubbo-kryo.version>
        <dubbo-actuator.version>2.7.1</dubbo-actuator.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
        <nacos-client.version>1.0.0</nacos-client.version>
        <dubbo-registry-nacos.version>2.6.6</dubbo-registry-nacos.version>
        <spring-cloud-alibaba.verion>2.1.0.RELEASE</spring-cloud-alibaba.verion>
        <alibaba-spring-context-support.version>1.0.2</alibaba-spring-context-support.version>
    </properties>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>cnbai</id>
            <name>cnbai</name>
            <email>baihaibo1024@163.com</email>
        </developer>
    </developers>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Cloud Begin -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.verion}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Spring Cloud End -->

            <!-- Dubbo Begin -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-actuator</artifactId>
                <version>${dubbo-actuator.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.spring</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${alibaba-spring-context-support.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo-registry-nacos</artifactId>
                <version>${dubbo-registry-nacos.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>${nacos-client.version}</version>
            </dependency>
                <!-- kryo 高速序列化 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-serialization-kryo</artifactId>
                <version>${dubbo-kryo.version}</version>
            </dependency>
            <!-- Dubbo End -->
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Milestone</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <name>Spring Milestone</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshot</id>
            <name>Spring Snapshot</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

服务提供者 apache-dubbo-provider

服务提供者

一、项目结构

技术图片

二、apache-dubbo-provider/pom.xml

<?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>com.cnbai</groupId>
        <artifactId>apache-dubbo-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>apache-dubbo-provider</artifactId>
    <packaging>pom</packaging>
    <url>http://www.cnbai.xyz</url>
    <inceptionYear>2020-Now</inceptionYear>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>cnbai</id>
            <name>cnbai</name>
            <email>baihaibo1024@163.com</email>
        </developer>
    </developers>

    <modules>
        <module>apache-dubbo-provider-api</module>
        <module>apache-dubbo-provider-service</module>
    </modules>
</project>

三、apache-dubbo-provider-api/pom.xml

<?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>com.cnbai</groupId>
        <artifactId>apache-dubbo-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>apache-dubbo-provider-api</artifactId>
    <packaging>jar</packaging>
    <url>http://www.cnbai.xyz</url>
    <inceptionYear>2020-Now</inceptionYear>

    <description>服务提供者接口模块,该模块只负责定义接口</description>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>cnbai</id>
            <name>cnbai</name>
            <email>baihaibo1024@163.com</email>
        </developer>
    </developers>
</project>

四、EchoService

package com.cnbai.apache.dubbo.provider.api;

/**
 * 测试动态化配置
 *
 * @author bai
 * @since 2020/5/12
 */
public interface EchoService {

    /**
     * 测试 nacos 动态化配置文件
     *
     * @param string   string
     * @return {@link String}
     * @author bai
     * @since 2020/5/12
     */
    String echo(String string);
}

五、apache-dubbo-provider-service/pom.xml

<?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>com.cnbai</groupId>
        <artifactId>apache-dubbo-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>apache-dubbo-provider-service</artifactId>
    <packaging>jar</packaging>
    <url>http://www.cnbai.xyz</url>
    <inceptionYear>2020-Now</inceptionYear>

    <description>服务提供者接口实现</description>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>cnbai</id>
            <name>cnbai</name>
            <email>baihaibo1024@163.com</email>
        </developer>
    </developers>

    <dependencies>
        <!-- Spring Boot Begin -->
        <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>
        <!-- Spring Boot End -->

        <!-- Apache Dubbo Begin -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-kryo</artifactId>
        </dependency>
        <!-- Apache Dubbo End -->

        <!-- Projects Begin -->
        <dependency>
            <groupId>com.cnbai</groupId>
            <artifactId>apache-dubbo-provider-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <!-- Projects End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.cnbai.apache.dubbo.provider.ProviderApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

六、EchoServiceImpl

package com.cnbai.apache.dubbo.provider.service;

import com.cnbai.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

/**
 * 测试负载均衡,动态化配置
 *
 * @author bai
 * @since 2020/5/12
 */
@Service(version = "1.0.0")
public class EchoServiceImpl implements EchoService {

    /**
     * 测试负载均衡
     */
    @Value("${dubbo.protocol.port}")
    private String port;

    /**
     * 测试负载均衡,动态化配置
     */
    @Override
    public String echo(String string) {
        return "Echo Hello Dubbo " + string + " i am from port: " + port;
    }
}

七、ProviderApplication

package com.cnbai.apache.dubbo.provider;

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

/**
 * 服务提供者启动类
 *
 * @author bai
 * @since 2020/5/12
 */
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

八、application.yml

spring:
  application:
    name: dubbo-provider
  # 定义两个 id 相同的 bean 起冲突时,以后定义的为主
  main:
    allow-bean-definition-overriding: true

dubbo:
  scan:
    base-packages: com.cnbai.apache.dubbo.provider.service
  protocol:
    name: dubbo
    # 自动分配端口号 20880
    # port: -1
    port: 20881
    serialization: kryo
  registry:
    address: nacos://192.168.205.130:8848
  # 负载均衡策略 random 随机(默认),roundrobin 轮循,leastactive 最少活跃调用数,consistenthash 一致性 Hash
  provider:
    loadbalance: roundrobin

服务消费者 apache-dubbo-consumer

服务消费者

一、项目结构

技术图片

二、pom.xml

<?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>com.cnbai</groupId>
        <artifactId>apache-dubbo-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>apache-dubbo-consumer</artifactId>
    <packaging>jar</packaging>
    <url>http://www.cnbai.xyz</url>
    <inceptionYear>2020-Now</inceptionYear>

    <description>服务消费者</description>

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>cnbai</id>
            <name>cnbai</name>
            <email>baihaibo1024@163.com</email>
        </developer>
    </developers>

    <dependencies>
        <!-- Spring Boot Begin -->
        <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>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- Spring Cloud End -->

        <!-- Apache Dubbo Begin -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-kryo</artifactId>
        </dependency>
        <!-- Apache Dubbo End -->

        <!-- Projects Begin -->
        <dependency>
            <groupId>com.cnbai</groupId>
            <artifactId>apache-dubbo-provider-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <!-- Projects End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.cnbai.apache.dubbo.consumer.ConsumerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

三、EchoController

package com.cnbai.apache.dubbo.consumer.controller;

import com.cnbai.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试负载均衡,动态化配置
 *
 * @author bai
 * @since 2020/5/12
 */
@RefreshScope
@RestController
public class EchoController {

    @Reference(version = "1.0.0")
    private EchoService echoService;

    /**
     * 测试动态化配置
     */
    @Value("${user.name}")
    private String username;

    /**
     * 测试 nacos 动态化配置
     *
     * @param string string
     * @author cnbai
     * @return {@link String}
     * @since 2020/5/12
     */
    @GetMapping(value = "/echo/{string}")
    public String echo(@PathVariable String string) {
        return echoService.echo(string) + " " + username;
    }
}

四、ConsumerApplication

package com.cnbai.apache.dubbo.consumer;

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

/**
 * 服务消费者启动类
 *
 * @author bai
 * @since 2020/5/12
 */
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

五、bootstrap.properties

##########################################################################
#
# Spring Boot 配置文件的加载顺序:
# bootstrap.properties
# > bootstrap.yml
# > application.properties
# > application.yml
#
# 注:bootstrap.properties 配置为最高优先级一定会加载
#
##########################################################################
spring.application.name=dubbo-consumer-config
spring.cloud.nacos.config.server-addr=192.168.205.130:8848
spring.cloud.nacos.config.file-extension=yaml

六、dubbo-consumer-config.yaml

注:
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉

技术图片

spring:
  application:
    name: dubbo-consumer
  main:
    allow-bean-definition-overriding: true

dubbo:
  scan:
    base-packages: com.cnbai.apache.dubbo.consumer.controller
  protocol:
    name: dubbo
    port: -1
    serialization: kryo
  registry:
    address: nacos://192.168.205.130:8848

server:
  port: 8080

# Dubbo 端口检查
endpoints:
  dubbo:
    enabled: true
    # Dubbo 健康检查
management:
  health:
    dubbo:
      status:
        defaults: memory
        extras: threadpool
  # web 健康检查
  endpoints:
    web:
      exposure:
        include: "*"

# 测试 Nacos 动态化配置
user:
  name: "只狼"
  # name: "条狗"  

代码

https://github.com/baihaibo1024/apache-dubbo-demo





以上是关于搭建Apache Dubbo工程的主要内容,如果未能解决你的问题,请参考以下文章

基于spring及zookeeper的dubbo工程搭建

dubbo--01--项目简单搭建

Dubbo+Zookeeper平台的搭建

Dubbo源码环境搭建

Idea搭建Apache Dubbo开源分布式服务框架

dubbo-admin控制台搭建