所有分类
  • 所有分类
  • 未分类

Spring Cloud–Nacos+@RefreshScope实现配置的动态更新

简介

说明

本文介绍SpringCloud整合Nacos使用@RefreshScope实现动态更新配置。

官网

Nacos Spring Cloud 快速开始

动态更新的介绍

动态更新的含义:修改应用的配置之后,无需重启应用,就能使用新的配置。

本文的版本

  • spring-boot-starter-parent:2.4.13
  • spring-cloud-dependencies:2020.0.1
  • spring-cloud-alibaba-dependencies:2021.1
  • nacos服务端:1.4.3

效果展示

1.启动Nacos服务端

本处我使用Windows启动Nacos单机:startup.cmd -m standalone

2.Nacos服务端新增命名空间

结果:

3.Nacos服务端新增配置

4.启动应用

5.测试接口

访问:http://localhost:9012/test

6.更新Nacos配置

由hello改成:hi

7.再次测试接口

代码

依赖

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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.13</version>
        <relativePath/>
    </parent>

    <groupId>com.knife.example</groupId>
    <artifactId>order</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>
    <name>order</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
        
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </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>
            <exclusions>
                <exclusion>
                    <groupId> springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--SpringBoot2.4.x之后默认不加载bootstrap.yml文件,需要在pom里加上依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

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

        <!--支持feign缓存-->
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <!--先只取bootstrap.yml等,不取application*.yml文件-->
            <resource>
                <directory>src/main/resources</directory>
                <!-- 是否替换yml或者properties里@xx@表示的maven properties属性值 -->
                <filtering>true</filtering>
                <excludes>
                    <exclude>application*.yml</exclude>
                </excludes>
            </resource>

            <!--添加application.yml等文件-->
            <resource>
                <directory>src/main/resources</directory>
                <!-- 是否替换yml或者properties里@xx@表示的maven properties属性值 -->
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>application-${profileActive}.yml</include>
                    <!--<include>**/application-${profileActive}.yml</include>-->
                </includes>
            </resource>
        </resources>

    </build>

</project>

配置

bootstrap.yml

spring:
  application:
    name: order
  profiles:
    active: @profileActive@
  cloud:
    nacos:
      server-addr: @nacos.server-addr@
      username: @nacos.username@
      password: @nacos.password@
      config:
        file-extension: yml
        namespace: ${spring.profiles.active}
#        # 公共的配置文件
#        shared-configs:
#          - data-id: common-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
#            group: DEFAULT_GROUP
#            refresh: true
      discovery:
        namespace: ${spring.profiles.active}

application.yml

server:
  port: 9012

测试代码

package com.knife.example.business.order.core.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RefreshScope
public class TestController {
    @Value("${custom.name}")
    private String name;

    @GetMapping("test")
    public String test() {
        return name;
    }

}

注意事项

假如Controller注入Service,Service上使用了@RefreshScope并使用@Value获取配置,那么Controller上必须也加上@RefreshScope,否则无法实现动态刷新。 ​

0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录