nacos 作为配置中心的功能及实现

nacos实现配置管理

实现:

在nacos后台Configmanagement/Configurations/+ 进行添加并填写配置信息

微服务配置拉取

image

步骤:

image

实现:

  1. 引入Nacos的配置管理客户端依赖:
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  1. 在userservice中的resource目录下添加bootstrap.yml文件,这个文件是引导文件,优先级高于application.yml
spring:
  application:
    name: userservice # 服务名称
  profiles: 
    active: dev # 开发环境,这里是dev,具体情况具体写
  cloud:
    nacos:
	  server-addr: localhost:8848 # Nacos服务地址
	  config :
	    file-extension: yaml # 文件后缀名

注:@Value 该注解的作用是将我们配置文件的属性读出来,有@Value(“${}”)和@Value(“#{}”)两种方式
例:

// 注入nacos中配置属性
@Value("${pattern.dateformat}")
private String dateformat;

配置热更新

  1. 在类上加注解@RefreshScope, 通常配合@Value使用
  2. 在配置类中添加ConfiurationProperties(prefix="")
    image

多环境配置共享

image
image
image

集群配置

视频教程

posted @ 2023-02-28 10:36  HS文  阅读(86)  评论(0)    收藏  举报