虽然 Bubbo 提供了许多功能,但我们只用它的 RPC 通信功能整合到 Spring Cloud Alibaba 一站式解决方案中。
也就是 Bubbo 是解决我们微服务中 RPC 通信的方式而已。

新建项目

image.png
什么都不用选直接点完成
image.png

POM

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.7</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.funtl</groupId>
  12. <artifactId>hello-apache-dubbo</artifactId>
  13. <version>1.0.0-SNAPSHOT</version>
  14. <packaging>pom</packaging>
  15. <name>hello-apache-dubbo</name>
  16. <description>hello-apache-dubbo</description>
  17. <modules>
  18. <module>hello-apache-dubbo-dependencies</module>
  19. <module>hello-apache-dubbo-provider</module>
  20. </modules>
  21. <properties>
  22. <java.version>1.8</java.version>
  23. <!-- Project revision -->
  24. <revision>0.0.1-SNAPSHOT</revision>
  25. <maven.compiler.source>${java.version}</maven.compiler.source>
  26. <maven.compiler.target>${java.version}</maven.compiler.target>
  27. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  28. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  29. </properties>
  30. <licenses>
  31. <license>
  32. <name>Apache License, Version 2.0</name>
  33. <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
  34. <distribution>repo</distribution>
  35. </license>
  36. </licenses>
  37. <developers>
  38. <developer>
  39. <id>wu</id>
  40. <name>Brett Wu</name>
  41. <email>brettwu@aliyun.com</email>
  42. </developer>
  43. </developers>
  44. <dependencyManagement>
  45. <dependencies>
  46. <dependency>
  47. <groupId>com.funtl</groupId>
  48. <artifactId>hello-apache-dubbo-dependencies</artifactId>
  49. <version>${project.version}</version>
  50. <type>pom</type>
  51. <scope>import</scope>
  52. </dependency>
  53. </dependencies>
  54. </dependencyManagement>
  55. <profiles>
  56. <profile>
  57. <id>default</id>
  58. <activation>
  59. <activeByDefault>true</activeByDefault>
  60. </activation>
  61. <properties>
  62. <spring-javaformat.version>0.0.31</spring-javaformat.version>
  63. </properties>
  64. <build>
  65. <plugins>
  66. <plugin>
  67. <!-- 格式化代码插件 -->
  68. <groupId>io.spring.javaformat</groupId>
  69. <artifactId>spring-javaformat-maven-plugin</artifactId>
  70. <version>${spring-javaformat.version}</version>
  71. </plugin>
  72. <plugin>
  73. <groupId>org.apache.maven.plugins</groupId>
  74. <artifactId>maven-surefire-plugin</artifactId>
  75. <configuration>
  76. <includes>
  77. <include>**/*Tests.java</include>
  78. </includes>
  79. <excludes>
  80. <exclude>**/Abstract*.java</exclude>
  81. </excludes>
  82. <systemPropertyVariables>
  83. <java.security.egd>file:/dev/./urandom</java.security.egd>
  84. <java.awt.headless>true</java.awt.headless>
  85. </systemPropertyVariables>
  86. </configuration>
  87. </plugin>
  88. <plugin>
  89. <groupId>org.apache.maven.plugins</groupId>
  90. <artifactId>maven-enforcer-plugin</artifactId>
  91. <executions>
  92. <execution>
  93. <id>enforce-rules</id>
  94. <goals>
  95. <goal>enforce</goal>
  96. </goals>
  97. <configuration>
  98. <rules>
  99. <bannedDependencies>
  100. <exludes>
  101. <exclude>commons-logging:*:*</exclude>
  102. </exludes>
  103. <searchTransitive>true</searchTransitive>
  104. </bannedDependencies>
  105. </rules>
  106. <fail>true</fail>
  107. </configuration>
  108. </execution>
  109. </executions>
  110. </plugin>
  111. <plugin>
  112. <groupId>org.apache.maven.plugins</groupId>
  113. <artifactId>maven-install-plugin</artifactId>
  114. <configuration>
  115. <skip>true</skip>
  116. </configuration>
  117. </plugin>
  118. <plugin>
  119. <groupId>org.apache.maven.plugins</groupId>
  120. <artifactId>maven-javadoc-plugin</artifactId>
  121. <configuration>
  122. <skip>true</skip>
  123. </configuration>
  124. <inherited>true</inherited>
  125. </plugin>
  126. </plugins>
  127. </build>
  128. </profile>
  129. </profiles>
  130. <repositories>
  131. <repository>
  132. <id>spring-milestone</id>
  133. <name>Spring Milestone</name>
  134. <url>https://repo.spring.io/milestone</url>
  135. <snapshots>
  136. <enabled>false</enabled>
  137. </snapshots>
  138. </repository>
  139. <repository>
  140. <id>spring-snapshot</id>
  141. <name>Spring Snapshot</name>
  142. <url>https://repo.spring.io/snapshot</url>
  143. <snapshots>
  144. <enabled>true</enabled>
  145. </snapshots>
  146. </repository>
  147. </repositories>
  148. <pluginRepositories>
  149. <pluginRepository>
  150. <id>spring-milestone</id>
  151. <name>Spring Milestone</name>
  152. <url>https://repo.spring.io/milestone</url>
  153. <snapshots>
  154. <enabled>false</enabled>
  155. </snapshots>
  156. </pluginRepository>
  157. <pluginRepository>
  158. <id>spring-snapshot</id>
  159. <name>Spring Snapshot</name>
  160. <url>https://repo.spring.io/snapshot</url>
  161. <snapshots>
  162. <enabled>true</enabled>
  163. </snapshots>
  164. </pluginRepository>
  165. </pluginRepositories>
  166. </project>

统一依赖管理

hello-apache-dubbo工程下创建统一依赖管理,创建目录 hello-apache-dubbo-dependencies
hello-apache-dubbo-dependencies 下创建 pom.xml 文件,内容如下:

pom

<?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 https://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.6.7</version>
        <relativePath/>
    </parent>

    <groupId>com.funtl</groupId>
    <artifactId>hello-apache-dubbo-dependencies</artifactId>
    <version>${revision}</version>
    <packaging>pom</packaging>
    <name>hello-apache-dubbo-dependencies</name>
    <description>Spring Cloud Alibaba Dependencies</description>

    <properties>
        <java.version>1.8</java.version>
        <revision>1.0.0-SNAPSHOT</revision>

        <!-- dubbo  -->
        <dubbo.version>2.7.15</dubbo.version>
        <dubbo.registry.nacos>2.6.7</dubbo.registry.nacos>
        <!--<dubbo.actuator.version>3.0.7</dubbo.actuator.version>-->
        <!-- Spring Settings 设置spring-cloud版本,此版本需要跟 spring boot 对应,不然会报错
        可以上官网查询对应版本 https://spring.io/projects/spring-cloud#learn-->
        <spring-cloud.version>2021.0.2</spring-cloud.version>

        <!-- 设置 alibaba 版本,此版本要需要跟 spring boot 对应,不然会报错
        官网地址:https://github.com/alibaba/spring-cloud-alibaba/blob/2021.x/README-zh.md-->
        <spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>
        <spring.cloud.commons>3.1.2</spring.cloud.commons>
        <spring.context.support.version>1.0.11</spring.context.support.version>

        <!--<sentinel.version>1.8.3</sentinel.version>
        <seata.version>1.4.2</seata.version>
        <nacos.client.version>1.4.2</nacos.client.version>
        <nacos.config.version>0.8.0</nacos.config.version>

        <maven-source-plugin.version>3.2.1</maven-source-plugin.version>
        <maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
        <maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
        <flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>-->

    </properties>

    <dependencyManagement>
        <dependencies>
            <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.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.spring</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.context.support.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-commons</artifactId>
                <version>${spring.cloud.commons}</version>
            </dependency>


            <!-- Apache 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.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo-registry-nacos</artifactId>
                <version>${dubbo.registry.nacos}</version>
            </dependency>
            <!-- Apache Dubbo  End-->

            <dependency>
                <groupId>com.alibaba.spring</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.context.support.version}</version>
            </dependency>

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

Dubbo 服务注册与发现

概述

由于我们已经有了 Nacos 注册中心,Sentinel 熔断限流控制中心,所以我们不再使用 Zookeeper 和DubboAdmin 来管理我们的Dubbo应用程序,Dubbo仅 当作我们微服务中的 RPC 通信框架,真正实现对内RPC,对外REST

创建服务提供者

pom

创建一个名为hello-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>hello-apache-dubbo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <groupId>com.funtl</groupId>
    <artifactId>hello-apache-dubbo-provider</artifactId>
    <packaging>pom</packaging>
    <name>hello-apache-dubbo-provider</name>
    <description>hello-apache-dubbo-provider</description>

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

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>wu</id>
            <name>Brett Wu</name>
            <email>brettwu@aliyun.com</email>
        </developer>
    </developers>
</project>

hello-apache-dubbo-provider模块下创建:

  • hello-apache-dubbo-provider-api暴露接口的模块
  • hello-apache-dubbo-provider-service实现接口的模块

    创建服务提供者接口模块

    pom

    创建一个 hello-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
          <groupId>com.funtl</groupId>
          <artifactId>hello-apache-dubbo-provider</artifactId>
          <version>1.0.0-SNAPSHOT</version>
      </parent>
    
      <groupId>com.funtl</groupId>
      <artifactId>hello-apache-dubbo-provider-api</artifactId>
      <packaging>jar</packaging>
      <name>hello-apache-dubbo-provider-api</name>
      <description>hello-apache-dubbo-provider-api</description>
    
      <licenses>
          <license>
              <name>Apache License, Version 2.0</name>
              <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
              <distribution>repo</distribution>
          </license>
      </licenses>
    
      <developers>
          <developer>
              <id>wu</id>
              <name>Brett Wu</name>
              <email>brettwu@aliyun.com</email>
          </developer>
      </developers>
    
      <dependencies>
          <!-- Dubbo -->
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo</artifactId>
          </dependency>
      </dependencies>
    </project>
    

    定义接口

    ```java pachage com.funtl.apache.dubbo.provider.api;

public interface EchoService{ String echo(String string); }

<a name="FJvln"></a>
### 创建服务提供者接口实现
<a name="KskNa"></a>
#### pom
创建一个 `hello-apache-dubbo-provider-service`的模块,`pom.xml`配置如下:
```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>hello-apache-dubbo-provider</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.funtl</groupId>
    <artifactId>hello-apache-dubbo-provider-service</artifactId>
    <packaging>jar</packaging>
    <name>hello-apache-dubbo-provider-service</name>
    <description>hello-apache-dubbo-provider-service</description>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>wu</id>
            <name>Brett Wu</name>
            <email>brettwu@aliyun.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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator-autoconfigure</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </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>
        <!-- Apache Dubbo End -->

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

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

</project>

Application

package com.funtl.apache.dubbo.provider;

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


@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

service

实现类

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

import com.funtl.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Service;


@DubboService(version = "1.0.0")
public class EchoServiceImpl implements EchoService {

    @Override
    public String echo(String messege) {
        return "[echo] Dubbo Hello, " + messege;
    }
}

application

配置文件:

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

dubbo:
  scan:
    base-packages: com.funtl.apache.dubbo.provider.service
  protocol:
    name: dubbo
    port: -1
  registry:
    address: nacos://192.168.3.192:8848