配置中心(Nacos)

Posted csdn_20210509

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置中心(Nacos)相关的知识,希望对你有一定的参考价值。

添加依赖

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

配置

在配置中心创建命名空间(微服务

在命名空间微服务下创建配置文件product-test.yaml

server: 
  port: 8000
spring:
  cloud: 
    nacos: 
      discovery:
        namespace: example
        group: test
test: test


添加bootstrap.yml文件

server: 
  servlet: 
  	context-path: /product-service
spring:
  profiles:
    active: test
  application:
    name: product
  cloud:
    nacos:
      config:
        file-extension: yaml
        namespace: example
        group: product

从配置文件读取test值

@RestController
public class ProductController 
    @Value("$test")
    private String test;

    @GetMapping
    public String test() 
        return test;
    
    
    @GetMapping("product")
    public String product() 
        return "product";
    



此时修改配置文件中的值,发现程序中不会动态的加载
添加@RefreshScope注解

@RefreshScope
此时动态修改配置中test的值了

以上是关于配置中心(Nacos)的主要内容,如果未能解决你的问题,请参考以下文章

Nacos:服务注册及配置中心

Nacos源码1.4.1配置中心客户端

Spring Cloud Alibaba Nacos服务注册和配置中心

nacos简介以及作为注册/配置中心与Eureka、apollo的选型比较

Nacos——服务注册和发现中心配置中心

Spring Cloud Alibaba Nacos服务注册和配置中心