springboot整合nacos註冊中心和配置中心

不忘初心2021發表於2024-03-08

我的名稱空間是centos

效果圖

可以是yml格式,名稱不要變

springboot版本2.1.6

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ip</groupId>
    <artifactId>nacos-ip</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
        <springcloud.version>Greenwich.RELEASE</springcloud.version>
        <alibaba.version>2.1.0.RELEASE</alibaba.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring cloud依賴管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!--SCA -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SCA -->
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

  配置檔案application.properties

server.port=8081

#nacos註冊
spring.application.name=nacos-service
spring.cloud.nacos.discovery.server-addr=192.168.31.50:8850
spring.cloud.nacos.discovery.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
management.endpoints.web.exposure.include=*

# 設定Nacos客戶端日誌級別為DEBUG
logging.level.com.alibaba.nacos=DEBUG



logging.level.com.ip=debug
logging.level.web=debug

  配置檔案bootstrap.properties

server.port=8081

#nacos配置
#2024.3.1 13:56 add
spring.cloud.nacos.config.server-addr=192.168.31.50:8850
spring.cloud.nacos.config.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.file-extension=properties

# 設定Nacos客戶端日誌級別為DEBUG
logging.level.com.alibaba.nacos=DEBUG



logging.level.com.ip=debug
logging.level.web=debug

  程式碼部分

package com.ip;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author yourheart
 * @Description
 * @create 2021-06-06 10:34
 */
@SpringBootApplication
@EnableDiscoveryClient
public class CloudPingAnApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudPingAnApplication.class,args);
    }
}

  

package com.ip.controller.front;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yourheart
 * @Description
 * @create 2021-06-06 10:59
 */
@Controller
@RequestMapping("/queryIp")
@RefreshScope
public class QueryIpController {


    @Value("${test}")
    private String test;

    /**
     * 根據ip查詢ip資訊
     * @param ip
     * @return
     */
    @RequestMapping("/queryIpCity/{ip}")
    @ResponseBody
    public String queryIpCity(@PathVariable String ip){
        System.out.println("test="+test);
        return "success";
    }
}

  註冊中心配置關聯的版本

springboot版本2.6.13

完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ip</groupId>
    <artifactId>nacos-ip</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
        <springcloud.version>2021.0.5</springcloud.version>
        <alibaba.version>2021.0.5.0</alibaba.version>
    </properties>



    <dependencyManagement>
        <dependencies>
            <!--spring cloud依賴管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!--SCA -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SCA -->
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

  配置檔案application.properties

server.port=8081

#nacos註冊
spring.application.name=nacos-service
spring.cloud.nacos.discovery.server-addr=192.168.31.50:8850
spring.cloud.nacos.discovery.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
management.endpoints.web.exposure.include=*

# 設定Nacos客戶端日誌級別為DEBUG
logging.level.com.alibaba.nacos=DEBUG

logging.level.com.ip=debug
logging.level.web=debug

  配置檔案bootstrap.properties

server.port=8081

#nacos配置
#2024.3.1 13:56 add
spring.cloud.nacos.config.server-addr=192.168.31.50:8850
spring.cloud.nacos.config.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.file-extension=properties

# 設定Nacos客戶端日誌級別為DEBUG
logging.level.com.alibaba.nacos=DEBUG



logging.level.com.ip=debug
logging.level.web=debug

  

請記得在bootstrap.properties檔案中新增配置

spring.cloud.nacos.config.refreshable-dataids=nacos-service.properties,true

後面又找了找,找到了沒過時的配置

三者搭配

# 配置支援共享的 Data Id
spring.cloud.nacos.config.shared-configs[0].data-id=nacos-service.properties

# 配置 Data Id 所在分組,預設預設 DEFAULT_GROUP
spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP

# 配置Data Id 在配置變更時,是否動態重新整理,預設預設 false
spring.cloud.nacos.config.shared-configs[0].refresh=true

  

環境隔離

如果之前使用的是eureka,

使用

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
替換
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>就可以了
註解使用@EnableDiscoveryClient

消費者springboot使用2.4.2版本時,pom依賴加入

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

  消費者springboot使用2.6.13版本,和上面引入一樣的依賴

不引入報錯如下

好了關於nacos註冊中心和配置中心使用就暫時到這裡了

相關文章