• Nacos Config 客户端的使用


    POM

    此处我们以之前创建的 服务提供者 项目为例

    pom.xml 中增加 org.springframework.cloud:spring-cloud-starter-alibaba-nacos-config 依赖

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

    完整的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.snake</groupId>
            <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
        </parent>
    
        <artifactId>hello-spring-cloud-alibaba-nacos-provider</artifactId>
        <packaging>jar</packaging>
    
        <name>hello-spring-cloud-alibaba-nacos-provider</name>
        <url>http://www.snake.com</url>
        <inceptionYear>2018-Now</inceptionYear>
    
        <dependencies>
            <!-- Spring Boot Begin -->
            <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>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- Spring Boot End -->
    
            <!-- Spring Cloud Begin -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
            <!-- Spring Cloud End -->
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.snake.hello.spring.cloud.alibaba.nacos.provider.NacosProviderApplication</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    #bootstrap.properties

    创建名为 bootstrap.properties 的配置文件并删除之前创建的 application.yml 配置文件,由于已经在服务端配置,此处不再赘述

    # 这里的应用名对应 Nacos Config 中的 Data ID,实际应用名称以配置中心的配置为准
    spring.application.name=nacos-provider-config
    # 指定查找名为 nacos-provider-config.yaml 的配置文件
    spring.cloud.nacos.config.file-extension=yaml
    # Nacos Server 的地址
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    

    注意:在之前的 Spring Cloud Netflix 课程中有提到过 Spring Boot 配置文件的加载顺序,依次为 bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml ,其中 bootstrap.properties 配置为最高优先级

    #启动应用程序

    启动应用后我们可以通过日志看到,已经成功加载到了配置文件

    img

    #配置的动态更新

    Nacos Config 也支持配置的动态更新,操作流程如下:

    • 修改服务端配置,增加一个 user.name 的属性

    img

    • 修改 Controller ,增加一个请求方法,测试配置更新效果
    // 注入配置文件上下文
    @Autowired
    private ConfigurableApplicationContext applicationContext;
    
    // 从上下文中读取配置
    @GetMapping(value = "/hi")
    public String sayHi() {
        return "Hello " + applicationContext.getEnvironment().getProperty("user.name");
    }
    
    • 通过浏览器访问该接口,浏览器显示
    Hello Snake
    
    • 修改服务端配置

    img

    此时观察控制台日志,你会发现我们已经成功刷新了配置

    img

    • 刷新浏览器,浏览器显示
    Hello Snake
    

    注意:你可以使用 spring.cloud.nacos.config.refresh.enabled=false 来关闭动态刷新

    等你看到的时候,想变得有一点点不一样
  • 相关阅读:
    交叉熵--损失函数
    Window安装TensorFlow- GPU环境
    使用Tensorflow object detection API——训练模型(Window10系统)
    使用Tensorflow object detection API——环境搭建与测试
    由位图生成区域
    旋转位图
    获得当前颜色深度
    求多边形的面积
    MaskBlt 拷贝非矩形区域图象
    画多边型
  • 原文地址:https://www.cnblogs.com/snake107/p/11920860.html
Copyright © 2020-2023  润新知