Springboot+Springcloud+Nacos實現動態配置(三)

  • 在上一篇文章基礎上,父工程新增如下依賴:
  <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2021.1</version>
        </dependency>
   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
  • alibaba-nacos-discovery-client 子工程新建一個controller
package gc.alibabanacosdiscoveryclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import org.springframework.cloud.context.config.annotation.RefreshScope;


@RefreshScope
@RestController
public class TestController {

    @Value("${name}")
    private String userName;

    @GetMapping("/get")
    public String get() {
        return "return : " + userName;
    }
}

  • 修改配置文件,新建bootstrap.yml 文件
spring:
  profiles:
    active: dev  #${spring.profile.active}
  application:
    name: alibaba-nacos-discovery-client  #${prefix}
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml  #${file-extension}
        namespace: b0183db0-0121-4b23-ae28-57a4872eda3f
        group: DEFAULT_GROUP
  • nacos平臺新增配置
  • 配置文件內容:
spring:
  application:
    name: alibaba-nacos-discovery-client
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

management:
  endpoints:
    web:
      exposure:
        include: "*"
server:
  port: 8084
name: test-config234

本次測試目標動態修改name 的值,並獲取。

啓動子工程

  • 訪問:127.0.0.1:8848/get


  • 修改文件中的配置:


以上就是動態配置內容(沒有深入研究)!僅供學習參考!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章