• SpringCloud 配置管理:Nacos


    统一配置管理

    将配置交给 Nacos 管理的步骤:

    1. Nacos 中添加配置文件。

    2. 在微服务中引入 nacos 的 config 依赖。

    3. 在微服务中添加 bootstrap.yml,配置 nacos 地址、当前环境、服务名称、文件后缀名。这些决定了程序启动时去 nacos 读取哪个文件。

    具体操作:

    1)在 Nacos 中添加配置信息

    2)在弹出表单中填写配置信息

     

    3)配置获取的步骤如下

    4)引入 Nacos 的配置管理客户端依赖

    1. <!--nacos连接注册中心-->
    2. <dependency>
    3. <groupId>com.alibaba.cloud</groupId>
    4. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    5. <version>2.2.2.RELEASE</version>
    6. </dependency>
    7. <!--nacos 配置中心-->
    8. <dependency>
    9. <groupId>com.alibaba.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    11. <version>2.2.2.RELEASE</version>
    12. </dependency>

    5)在 resource 目录添加一个 bootstrap.yml 文件,这个文件是引导文件,优先级高于 application.yml

    1. spring:
    2. application:
    3. name: nacos-provider-payment
    4. cloud:
    5. nacos:
    6. discovery:
    7. namespace: 18b79e76-8c78-4494-b4fe-be0e587ff6d7
    8. # 服务注册地址 ip加端口号
    9. server-addr: 127.0.0.1:8848
    10. config:
    11. prefix: providerId # dataId ${prefix}-${spring.profiles.active}.${file-extension}
    12. server-addr: 127.0.0.1:8848
    13. username: nacos
    14. password: nacos
    15. namespace: 18b79e76-8c78-4494-b4fe-be0e587ff6d7
    16. group: DEFAULT_GROUP
    17. file-extension: yaml

    6)测试:将(Nacos 配置内容中的)dataId.result: 111 这个属性注入到 UserController 中

    1. package com.pooj.cloudnacosconfig3355.controller;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.cloud.context.config.annotation.RefreshScope;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. @RequestMapping("/user") ///user/getData
    8. @RefreshScope
    9. public class CloudNacos {
    10. @Value("${dataId.result}")
    11. private String dataid;
    12. @RequestMapping(value = "/getData")
    13. public String getDate(){
    14. return dataid;
    15. }
    16. }

    配置热更新

    Nacos 配置更改后,微服务可以实现热更新,两种方式如下:

    1. 通过 @Value 注解注入,结合 @RefreshScope 来刷新。

    2. 通过 @ConfigurationProperties 注入,自动刷新。

    注意事项:

        启动类: @EnableDiscoveryClient

    • 不是所有的配置都适合放到配置中心,否则维护起来比较麻烦。

    • 建议将一些关键参数,需要运行时调整的参数放到 nacos 配置中心,一般都是自定义配置。

    方式一:在 @Value 注入的变量所在类上添加注解 @RefreshScope

    方式二:使用 @ConfigurationProperties 注解

    1. @Component
    2. @Data
    3. @ConfigurationProperties(prefix="pattern")
    4. public class PatternProperties {
    5. private String dateformat;
    6. }

    配置共享

    多环境配置共享

    微服务会从 nacos 读取的配置文件:

    • [服务名]-[spring.profile.active].yaml环境配置(例如 userservice-dev.yaml)。

    • [服务名].yaml默认配置,多环境共享(例如 userservice.yaml)。

    • 无论 profile 如何变化,[服务名].yaml 这个文件一定会加载,因此多环境共享配置可以写入这个文件。

    配置加载优先级:

    多服务配置共享

    不同服务之间共享配置文件的两种方式:

    1. 通过 shared-configs 指定

    2. 通过 extension-configs 指定

    方式一:通过 shared-configs 指定

    1. spring:
    2. application:
    3. name: userservice # 服务名称
    4. profiles:
    5. active: dev # 环境
    6. cloud:
    7. nacos:
    8. server-addr: localhost:8848 # Nacos 地址
    9. config:
    10. file-extension: yaml # 文件后缀名
    11. shared-configs: # 多微服务间共享的配置列表
    12. - dataId: common.yaml # 要共享的配置文件 id

    方式二:通过 extension-configs 指定

    1. spring:
    2. application:
    3. name: userservice # 服务名称
    4. profiles:
    5. active: dev # 环境
    6. cloud:
    7. nacos:
    8. server-addr: localhost:8848 # Nacos 地址
    9. config:
    10. file-extension: yaml # 文件后缀名
    11. extends-configs: # 多微服务间共享的配置列表
    12. - dataId: extend.yaml # 要共享的配置文件 id

    多种配置的优先级:


     

    如果出现:

    Error processing condition on org.springframework.cloud.commons.httpclient.HttpClientConfigur....

    
        org.apache.httpcomponents
        httpclient
        4.5.3
    

  • 相关阅读:
    下篇 | 使用 🤗 Transformers 进行概率时间序列预测
    在实际的项目需求中了解技术架构的演进
    从零开始 Spring Cloud 15:多级缓存
    背包问题。。。
    MyBatis:关联查询
    楼房销售系统
    MATLAB R2023a完美激活版(附激活补丁)
    可阅读随机字符串与随机字符串
    vue3项目中使用富文本编辑器
    Mybatis的多表操作之多对多查询与练习
  • 原文地址:https://blog.csdn.net/zhanglixin999/article/details/126596876