虽然 Bubbo 提供了许多功能,但我们只用它的 RPC 通信功能整合到 Spring Cloud Alibaba 一站式解决方案中。
也就是 Bubbo 是解决我们微服务中 RPC 通信的方式而已。
新建项目
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/> <!-- lookup parent from repository --></parent><groupId>com.funtl</groupId><artifactId>hello-apache-dubbo</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging><name>hello-apache-dubbo</name><description>hello-apache-dubbo</description><modules><module>hello-apache-dubbo-dependencies</module><module>hello-apache-dubbo-provider</module></modules><properties><java.version>1.8</java.version><!-- Project revision --><revision>0.0.1-SNAPSHOT</revision><maven.compiler.source>${java.version}</maven.compiler.source><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><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><dependencyManagement><dependencies><dependency><groupId>com.funtl</groupId><artifactId>hello-apache-dubbo-dependencies</artifactId><version>${project.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><profiles><profile><id>default</id><activation><activeByDefault>true</activeByDefault></activation><properties><spring-javaformat.version>0.0.31</spring-javaformat.version></properties><build><plugins><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><exludes><exclude>commons-logging:*:*</exclude></exludes><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>
统一依赖管理
在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

