亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于Nacos配置管理的統(tǒng)一配置管理、自動刷新詳解

 更新時間:2023年05月17日 11:17:19   作者:夏志121  
這篇文章主要介紹了關(guān)于Nacos配置管理的統(tǒng)一配置管理、自動刷新詳解,Nacos是阿里的一個開源產(chǎn)品,是針對微服務(wù)架構(gòu)中的服務(wù)發(fā)現(xiàn)、配置管理、服務(wù)治理的綜合型解決方案,需要的朋友可以參考下

一、Nacos統(tǒng)一配置管理

1、在Nacos同添加配置文件

5ca657329ce74ac68157ffd1f144fc2a.png

 2、在微服務(wù)中引入Nacos的配置管理客戶端依賴:

        <!--nacos的配置管理依賴-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

3、在userservice中的resource目錄添加一個bootstrap.yml文件,這個文件是引導(dǎo)文件,優(yōu)先級高于application.yml:

spring:
  application:
    name: userservice
  profiles:
    active: dev #環(huán)境
  cloud:
    nacos:
      server-addr: localhost:80 # nacos地址
      config:
        file-extension: yaml  # 文件后綴名

4、在user-service中將pattern.dateformat這個屬性注入到UserController中做測試:

@RestController
@RequestMapping("/user")
public class UserController {
    //注入nacos中的配置屬性
    @Value("${pattern.dateformat}")
    private String dateformat;
    //編寫controller,通過日期格式化器來格式化現(xiàn)在的時間并返回
    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }
    //...略
}

二、配置自動刷新

Nacos中的配置文件變更后,微服務(wù)無需重啟就可以感知,不過需要通過下面兩種配置實現(xiàn):

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

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
    //注入nacos中的配置屬性
    @Value("${pattern.dateformat}")
    private String dateformat;

方式二:使用@ConfigurationProperties注解

@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
    private String dateformat;
}

注意事項:

不是多有的配置都適合放到配置中心,維護起來比較麻煩

建議將一些關(guān)鍵參數(shù),需要運行時調(diào)整的參數(shù)放在nacos

三、多環(huán)境配置共享

微服務(wù)啟動時會從nacos讀取多個配置文件:

[spring.application.name]-[spring.profiles.active].yaml,例如:userservice-dev.yaml

[spring.appliction.name].yaml,例如:userservice.yaml

無論profile如何,[spring.appliction.name].yaml這個文件一定會加載,因此多環(huán)境共享配置可以寫入這個文件

b6cdc552da1c48beb5cfafb87851373a.png

多種配置的優(yōu)先級:

55c0c0b76a7d455aa1817b5cc08f80b9.png

三、多服務(wù)共享配置

不同微服務(wù)之間可以共享配置文件,通過下面的兩種方式來指定

方式一:

spring:
  application:
    name: userservice
  profiles:
    active: dev #環(huán)境
  cloud:
    nacos:
      server-addr: localhost:80 # nacos地址
      config:
        file-extension: yaml  # 文件后綴名
        shared-configs: # 多服務(wù)間共享的配置列表
         -datald: common.yaml # 要共享的配置文件id

方式二:

spring:
  application:
    name: userservice
  profiles:
    active: dev #環(huán)境
  cloud:
    nacos:
      server-addr: localhost:80 # nacos地址
      config:
        file-extension: yaml  # 文件后綴名
        extends-configs: # 多服務(wù)間共享的配置列表
         -datald: extend.yaml # 要共享的配置文件id

多種配置的優(yōu)先級:

c01ac37e83554626a0f93342c5d1a1b4.png

到此這篇關(guān)于關(guān)于Nacos配置管理的統(tǒng)一配置管理、自動刷新詳解的文章就介紹到這了,更多相關(guān)Nacos配置管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論