这篇文章上次修改于 967 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

写在前面

Nacos:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
Dubbo:Apache Dubbo™ 是一款高性能 Java RPC 框架。
其实最开始可以用阿里巴巴的start https://start.aliyun.com/ 替换https://start.spring.io/来新建项目

父maven项目搭建

1.新建普通项目

packaging为pom

2.父pom文件导入依赖

springcloud-alibab github 文档说明
2.2.x 分支对应的是 Spring Cloud Hoxton 与 Spring Boot 2.2.x,最低支持 JDK 1.8。
github:https://github.com/alibaba/spring-cloud-alibaba/blob/2021.x/README-zh.md
其他依赖自行测试

<?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.lin945.alibaba</groupId>
  <artifactId>springcloud_alibaba_study</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>cloud-provider-payment8001</module>
    <module>cloud-consumer-oder80</module>
    <module>cloud-api-commons</module>
    <module>cloud-dubbo-provider</module>
  </modules>

  <name>Maven</name>
  <!-- FIXME change it to the project's website -->
  <url>http://github.com/lin945</url>
  <inceptionYear>2022</inceptionYear>

  <distributionManagement>
    <site>
      <id>website</id>
      <url>scp://webhost.company.com/www/website</url>
    </site>
  </distributionManagement>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.cloud-version>Hoxton.SR12</spring.cloud-version>
    <spring.cloud-alibaba-version>2.1.2.RELEASE</spring.cloud-alibaba-version>
    <spring.boot-version>2.2.1.RELEASE</spring.boot-version>
    <mysql.version>8.0.23</mysql.version>
<!--    <junit.version>4.13</junit.version>-->
    <lombok.version>1.18.18</lombok.version>
    <log4j.version>1.2.17</log4j.version>
    <druid.version>1.1.16</druid.version>
    <mybatis.spring.boot.version>3.0.5</mybatis.spring.boot.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <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.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>

      <!--mybatis-plus-->
      <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.spring.boot.version}</version>
      </dependency>
      <!--junit-->
<!--      <dependency>-->
<!--        <groupId>junit</groupId>-->
<!--        <artifactId>junit</artifactId>-->
<!--        <version>${junit.version}</version>-->
<!--      </dependency>-->
      <!--log4j-->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

</project>

新建子模块

先建服务提供者和消费者

1.provider模块

服务提供者
需要引入nacos的配置和注册中心及dubbo
一般负责连接数据库拿数据

bootstrap.yaml配置

nacos配置管理需要bootstrap.yaml
详细配置见https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

spring:
  application:
    name: dubbo-provider
  cloud:
    nacos:
      server-addr: 0.0.0.0:8848 #你的nacos地址
      config:
        file-extension: yaml
        enabled: true
  profiles:
    active: dev

nacos配置

新建配置dubbo-provider-dev.yaml

server:
  port: 8002
dubbo:
  application:
    name: ${spring.application.name}
  registry:
    address: nacos://0.0.0.0:8848 #你的nacos地址
  protocol:
    name: dubbo
    port: 20080

代码编写

在需要配置的service上添加@Service(group = "dubbo",version = "1.0")注解
注意需要填写group 与 version值否则会找不到服务提供者
主启动类上加
@EnableDubbo
@EnableDiscoveryClient

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">
    <parent>
        <artifactId>springcloud_alibaba_study</artifactId>
        <groupId>com.lin945.alibaba</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>Demo project for Spring Boot</description>
    <artifactId>cloud-dubbo-provider</artifactId>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-openfeign-core</artifactId>
            </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                    <mainClass>com.lin945.alibaba.CloudDubboProviderApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.consumer模块

服务消费者
需要引入nacos的配置和注册中心及dubbo
一般负责从服务提供者拿数据以及和用户通讯

bootstrap.yaml配置

nacos配置管理需要bootstrap.yaml
详细配置见https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

spring:
  application:
    name: dubbo-consumer
  cloud:
    nacos:
      server-addr: 0.0.0.0:8848 #你的nacos地址
      config:
        file-extension: yaml
        enabled: true
  profiles:
    active: dev

nacos配置

在nacos控制台新建配置dubbo-consumer-dev.yaml

server:
  port: 8003
dubbo:
  application:
    name: ${spring.application.name}
  registry:
    address: nacos://0.0.0.0:8848 #你的nacos地址
  protocol:
    name: dubbo
    port: 20080

代码编写

在需要配置的service上添加 @Reference(check = false,group = "dubbo",version = "1.0")注解
注意需要填写group 与 version值否则会找不到服务提供者
主启动类上加
@EnableDubbo
@EnableDiscoveryClient

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">
    <parent>
        <artifactId>springcloud_alibaba_study</artifactId>
        <groupId>com.lin945.alibaba</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>Demo project for Spring Boot</description>
    <artifactId>cloud-dubbo-provider</artifactId>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-openfeign-core</artifactId>
            </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                    <mainClass>com.lin945.alibaba.CloudDubboProviderApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>