Spring Cloud Alibaba 使用Nacos作為配置管理中心


為什么需要配置中心?

動態配置管理是 Nacos 的三大功能之一,通過動態配置服務,我們可以在所有環境中以集中和動態的方式管理所有應用程序或服務的配置信息。

動態配置中心可以實現配置更新時無需重新部署應用程序和服務即可使相應的配置信息生效,這極大了增加了系統的運維能力。

服務配置中心

工程改造

繼續使用之前的工程:spring-cloud-alibaba-service-user

pom.xml中增加

<!-- nacos 配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 使用 bootstrap -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

將application.yaml變更為bootstrap.yaml,並且增加nacos配置

server:
  port: 8080
spring:
  application:
    name: service-user
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        #指定命名空間 對應dev環境
        namespace: 7e3699fa-09eb-4d47-8967-60f6c98da94a
        #指定分組 案例組
        group: EXAMPLE-GROUP
        #指定集群環境 華南
        cluster-name: HuaNan
        #指定配置文件的類型,默認是 properties
        file-extension: properties
         #前綴默認應用名稱${spring.application.name}
        prefix: ${spring.application.name}

獲取配置文件的規則為:${spring.cloud.nacos.config.prefix}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

按照bootstrap.yaml中的配置意思就是,在dev命名空間EXAMPLE-GROUP組中尋找DataId為:service-user.properties的配置文件。

Nacos創建配置文件

進入Nacos Web界面菜單配置管理->配置列表->進入dev命名空間 創建配置文件

業務服務使用配置

NacosConfigController.java

import com.gtiee.example.common.exception.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Nacos Config
 *
 * @author wentao.wu
 */
@RequestMapping(value = "/nacos/config/")
@RestController
public class NacosConfigController {
    @Value("${nacos.config.msg}")
    private String msg;
    @GetMapping("/getMsg")
    public Response<String> getMsg() {
        Response<String> response = new Response<>();
        response.setCode("1");
        response.setMsg(msg);
        return response;
    }
}

重啟spring-cloud-alibaba-service-user工程,請求訪問接口:http://localhost:8080/nacos/config/getMsg 請求成功返回值為

{
    "code": "1",
    "msg": "這是一條存放在配置中心的消息",
    "errorCode": null,
    "errorMsg": null,
    "result": null
}

代碼中的成員變量msg讀取的就是配置中心鍵值對的鍵為nacos.config.msg的配置。

動態刷新配置

需要動態刷新配置,只需要在NacosConfigController.java中增加類注解@RefreshScope

@RequestMapping(value = "/nacos/config/")
@RestController
@RefreshScope//標注該類對配置進行監聽,動態刷新
public class NacosConfigController {
	...省略重復代碼
}

增加注解后在Nacos Web界面中對配置中的nacos.config.msg對應值進行修改為: 你好,Nacos!。修改完成后重新發布即可,當Nacos Client接收到Nacos Server推送的配置變更消息則會立即刷新變量。再次訪問http://localhost:8080/nacos/config/getMsg 請求成功返回值為

{
    "code": "1",
    "msg": "你好,Nacos!",
    "errorCode": null,
    "errorMsg": null,
    "result": null
}

源碼代碼存放地址

gitee: https://gitee.com/SimpleWu/spring-cloud-alibaba-example.git
cnblogs: https://www.cnblogs.com/SimpleWu
持續更新目錄:https://www.cnblogs.com/SimpleWu/p/15476427.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM