springboot整合springcloud +SpringCloudAlibaba+nacos+dubbo

版本说明:
nacos:1.4.2
spring-boot:2.7.1
spring-cloud:2021.0.3
spring-cloud-alibaba:2021.1
nacos配置文件命名规则:
spring.application.name+“-”+spring.profiles.active+“.”+spring.cloud.nacos.config.file-extension
例子:dubbo-producer-dev.yaml

#如果你导入了spring-cloud-starter-alibaba-nacos-config依赖(如果是父模块导入,子模块也受作用),就必须配置bootstrap.yml这样的配置文件
#nacos客户端默认是properties的文件拓展名,如果不是properties的文件拓展名,需要增加file-extenstion
#重要:nacos配置中心中默认配置文件文件名是服务端的名称,也可以自定义配置文件,使用shared-config 如果使用这个配置项那么将不受file-extension与group的影响
#shared-configs自定义的nacos配置中心配置文件也有优先级,最后边的优先级最大【shared-configs集合中下标最大的优先级最高】
#extension-configs自定义的nacos配置中心配置文件也有优先级,最后边的优先级最大【extension-configs集合中下标最大的优先级最高】
#优先级对比,extension-configs 大于 shared-configs

一.创建父工程

pom文件


<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.0modelVersion>
    <groupId>com.mulitgroupId>
    <artifactId>iotartifactId>
    <version>1.0version>
    <name>iotname>
    <packaging>pompackaging>
    <description>父文件description>
    <properties>
        <java.version>1.8java.version>
        <spring-boot.version>2.7.1spring-boot.version>
        <spring-cloud.version>2021.0.3spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1spring-cloud-alibaba.version>
    properties>
    <modules>
        <module>mulit-webapimodule>
        <module>mulit-prodmodule>
        <module>mulit-commonmodule>
    modules>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.bootgroupId>
                <artifactId>nacos-discovery-spring-boot-actuatorartifactId>
                <version>0.2.11version>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring-boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

二.创建服务提供方

pom文件


<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.0modelVersion>
    <parent>
        <groupId>com.mulitgroupId>
        <artifactId>iotartifactId>
        <version>1.0version>
    parent>
    <groupId>com.mulitgroupId>
    <artifactId>mulit-prodartifactId>
    <version>1.0version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-dubboartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>com.mulitgroupId>
            <artifactId>commonartifactId>
            <version>1.0version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
    dependencies>
    <build>
        <finalName>${project.artifactId}finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

三.创建消费方

pom文件


<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.0modelVersion>
    <parent>
        <groupId>com.mulitgroupId>
        <artifactId>iotartifactId>
        <version>1.0version>
    parent>
    <groupId>com.mulitgroupId>
    <artifactId>mulit-webapiartifactId>
    <version>1.0version>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-dubboartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.mulitgroupId>
            <artifactId>commonartifactId>
            <version>1.0version>
        dependency>
    dependencies>
    <build>
        <finalName>${project.artifactId}finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

四.配置nacos

1.创建命名空间

springboot整合springcloud +SpringCloudAlibaba+nacos+dubbo_第1张图片

2.创建配置文件

springboot整合springcloud +SpringCloudAlibaba+nacos+dubbo_第2张图片

3.提供端与消费端配置文件

服务端配置文件

spring:
  application:
    name: dubbo-producer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        # 这个只能修改 dubbo-producer 注册到 nacos的分组,不会修改 dubbo 注册到 nacos 的分组
        namespace: 83fdf479-f0f2-4cb1-8125-2d4a37242136
        # 这个只能修改 dubbo-producer 注册到 nacos 的分组,不会修改dubbo注册到 nacos 的分组
        group: spring-cloud
dubbo:
  scan:
    base-packages: com.mulit.prod.dubbo
  protocol:
    # dubbo 协议名称
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    address: nacos://127.0.0.1:8848
    # 修改 dubbo 在 nacos 上的命名空间
    parameters[namespace]: 83fdf479-f0f2-4cb1-8125-2d4a37242136
    # 修改 dubbo 在 nacos 上的分组
    group: spring-cloud

消费端配置文件

spring:
  application:
    name: dubbo-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        # 这个只能修改 dubbo-producer 注册到 nacos的分组,不会修改 dubbo 注册到 nacos 的分组
        namespace: 83fdf479-f0f2-4cb1-8125-2d4a37242136
        # 这个只能修改 dubbo-producer 注册到 nacos 的分组,不会修改dubbo注册到 nacos 的分组
        group: spring-cloud
        username: nacos
        password: nacos
dubbo:
#  scan:
#    base-packages: com.zhazha.producer.service.impl
  protocol:
    # dubbo 协议名称
    name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
    port: -1
  registry:
    address: nacos://127.0.0.1:8848
    # 修改 dubbo 在 nacos 上的命名空间
    parameters[namespace]: 83fdf479-f0f2-4cb1-8125-2d4a37242136
    # 修改 dubbo 在 nacos 上的分组
    group: spring-cloud
  application:
    # 关闭 dubbo 在线运维功能,该功能主要通过 telnet 方式进行服务的权重调整
    qos-enable: false

五.项目中的配置文件

1.服务端配置文件

application.yml

#服务端口
server:
  port: 8080

bootstrap.yml

spring:
  application:
    name: dubbo-producer
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        #config-center
        namespace: 6b17522d-b45d-4f7f-8d08-596914d1c494
        username: nacos
        password: nacos

2.消费端配置文件

application.yml

#服务端口
server:
  port: 8080

bootstrap.yml

spring:
  application:
    name: dubbo-consumer
  main:
    allow-bean-definition-overriding: true
    allow-circular-references: true
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        #config-center
        namespace: 6b17522d-b45d-4f7f-8d08-596914d1c494
        username: nacos
        password: nacos

你可能感兴趣的:(spring,boot,spring,cloud,dubbo)