Spring Cloud Alibaba 使用nacos 注冊(cè)中心
背景
在第一篇nacos介紹的時(shí)候提到nacos 是注冊(cè)中心又是配置中心懈费,上一篇我們介紹了Spring Cloud Alibaba 使用nacos 注冊(cè)中心的使用這一篇我們講述 配置中心的使用蹈丸。
nacos配置中心
之前的講述了 配置文件的配置。這次我們只需要在之前的代碼中 添加maven nacos配置中心的依賴
這個(gè)是配置到spring cloud alibaba 教程總pom版本控制 這個(gè)pom文件中。這樣還是利用maven 自身的jar包依賴孝凌。discovery-server难礼、cloud-discovery-client-common倆個(gè)模塊自動(dòng)引入nacos配置中心的依賴
<!-- nacos config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
修改對(duì)應(yīng)yml文件添加
server端yaml配置文件
spring:
cloud:
config:
server-addr: 47.99.209.72:8848
file-extension: yaml
最后分別呈現(xiàn)結(jié)果
server:
port: 9012
spring:
profiles:
active: dev
application:
name: cloud-discovery-server
cloud:
nacos:
config:
server-addr: 47.99.209.72:8848
#指定文件后綴
file-extension: yaml
discovery:
server-addr: 47.99.209.72:8848
修改http接口
package com.xian.cloud.controller;
import lombok.extern.slf4j.Slf4j;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: xlr
* @Date: Created in 2:57 PM 2019/10/27
*/
@RestController
@RequestMapping("server")
@Slf4j
# 提供分布式的配置動(dòng)態(tài)刷新
@RefreshScope
public class DiscoverCotroller {
@Value( "${nacos.yaml.age}" )
private String age;
/**
* 對(duì)外提供的服務(wù) HTTP接口
* @param name
* @return
*/
@GetMapping("/hello")
public String hello(@RequestParam String name) {
log.info("invoked name = " + name+ " age = " + age);
return "hello " + name + " age = " + age;
}
}
然后在配置中心配置創(chuàng)建配置文件
點(diǎn)擊登錄nacos配置中心
創(chuàng)建 cloud-discovery-server-dev.yaml
配置
nacos.yaml.age= 30
啟動(dòng)服務(wù)
訪問 curl http://localhost:9012/server/hello?name=tom
日志打印
說明nacos配置中心已經(jīng)生效
然后我們修改cloud-discovery-server-dev.yaml 配置文件age 為20參數(shù) 重新發(fā)布一下
會(huì)看到后臺(tái)日志打印
2019-10-27 19:53:08.884 INFO 44618 --- [.99.209.72_8848] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$87d25f89] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-27 19:53:09.091 INFO 44618 --- [.99.209.72_8848] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'cloud-discovery-server-dev.yaml', group: 'DEFAULT_GROUP'
2019-10-27 19:53:09.092 INFO 44618 --- [.99.209.72_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='cloud-discovery-server-dev.yaml'}, NacosPropertySource {name='cloud-discovery-server.yaml'}]}
2019-10-27 19:53:09.093 INFO 44618 --- [.99.209.72_8848] o.s.boot.SpringApplication : The following profiles are active: dev
2019-10-27 19:53:09.103 INFO 44618 --- [.99.209.72_8848] o.s.boot.SpringApplication : Started application in 0.277 seconds (JVM running for 883.2)
2019-10-27 19:53:09.119 INFO 44618 --- [.99.209.72_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [nacos.yaml.age]
不重啟服務(wù),再次請(qǐng)求.age 已發(fā)送改變
以上就是nacos的配置中心的配置陷遮。
然后我們講解一下 nacos配置的匹配規(guī)則
dataID
在 Nacos Config Starter 中,
dataId (也就是上面cloud-discovery-server-dev.yaml)的拼接格式如下
{spring.profiles.active} . ${file-extension}
prefix 默認(rèn)為 spring.application.name 的值垦江,也可以通過配置項(xiàng) spring.cloud.nacos.config.prefix來配置帽馋。
spring.profiles.active 即為當(dāng)前環(huán)境對(duì)應(yīng)的 profile,詳情可以參考 Spring Boot文檔
注意比吭,當(dāng) activeprofile 為空時(shí)绽族,對(duì)應(yīng)的連接符 - 也將不存在,dataId 的拼接格式變成 {file-extension}
file-extension 為配置內(nèi)容的數(shù)據(jù)格式梗逮,可以通過配置項(xiàng) spring.cloud.nacos.config.file-extension來配置项秉。 目前只支持 properties 類型绣溜。
group
group 默認(rèn)為 DEFAULT_GROUP慷彤,可以通過 spring.cloud.nacos.config.group 配置。
自動(dòng)注入
Nacos Config Starter 實(shí)現(xiàn)了 org.springframework.cloud.bootstrap.config.PropertySourceLocator接口怖喻,并將優(yōu)先級(jí)設(shè)置成了最高底哗。
在 Spring Cloud 應(yīng)用啟動(dòng)階段,會(huì)主動(dòng)從 Nacos Server 端獲取對(duì)應(yīng)的數(shù)據(jù)锚沸,并將獲取到的數(shù)據(jù)轉(zhuǎn)換成 PropertySource 且注入到 Environment 的 PropertySources 屬性中跋选,所以使用 @Value 注解也能直接獲取 Nacos Server 端配置的內(nèi)容。
動(dòng)態(tài)刷新
Nacos Config Starter 默認(rèn)為所有獲取數(shù)據(jù)成功的 Nacos 的配置項(xiàng)添加了監(jiān)聽功能哗蜈,在監(jiān)聽到服務(wù)端配置發(fā)生變化時(shí)會(huì)實(shí)時(shí)觸發(fā) org.springframework.cloud.context.refresh.ContextRefresher 的 refresh 方法 前标。
如果需要對(duì) Bean 進(jìn)行動(dòng)態(tài)刷新,請(qǐng)參照 Spring 和 Spring Cloud 規(guī)范距潘。推薦給類添加 @RefreshScope 或 @ConfigurationProperties 注解炼列,
以上就是nacos配置中心的全部?jī)?nèi)容。
補(bǔ)充 命名空間
不管是 nacos的注冊(cè)中心 還是nacos的配置中心 都有一個(gè)namespace屬性音比。這個(gè)屬性 是針對(duì)我們nacos 控制臺(tái)命名空間俭尖。
特殊說明一下,命名空間的概念 因?yàn)榕渲弥行暮妥?cè)中心是通用的概念洞翩。所以放在里配置中心這里講解稽犁。
我們?cè)诳刂婆_(tái)創(chuàng)建lms的一個(gè)命名空間
會(huì)有命名空間ID e071c3ab-b280-4ae7-a081-044fff5613ad
我們把這個(gè)ID 放到配置文件里面 對(duì)應(yīng)配置修改為
server:
port: 9013
spring:
profiles:
active: dev
application:
name: cloud-discovery-server
cloud:
nacos:
config:
server-addr: 47.99.209.72:8848
file-extension: yaml
namespace: e071c3ab-b280-4ae7-a081-044fff5613ad
discovery:
server-addr: 47.99.209.72:8848
namespace: e071c3ab-b280-4ae7-a081-044fff5613ad
重新啟動(dòng)會(huì)發(fā)現(xiàn),服務(wù)注冊(cè)到了 lms的命名空間上了骚亿。
反思
dataID已亥、group 組合使用。{spring.profiles.active} . ${file-extension} 来屠。其中active參數(shù)的變化可以幫我們做到環(huán)境的隔離虑椎。group的變化秫舌。又可以幫我們做到項(xiàng)目組、版本的區(qū)分绣檬。這樣配置就不會(huì)亂
配合倆種參數(shù)的變化達(dá)到我們想要的動(dòng)態(tài)變化區(qū)分足陨。其實(shí)這塊的設(shè)計(jì)滿足了我們大部分的場(chǎng)景的需要和支持
在加上命名空間的概念。我們有多了一種情況的變化 與選擇娇未。不過這樣多了墨缘,當(dāng)我們集群環(huán)境足夠大的時(shí)候,反而會(huì)讓很多同學(xué)找不到頭緒零抬。這就需要我們提前約定好镊讼。規(guī)則的使用
如何喜歡可以關(guān)注分享本公眾號(hào)。
版權(quán)聲明:本文為博主原創(chuàng)文章平夜,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議蝶棋,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。轉(zhuǎn)載請(qǐng)附帶公眾號(hào)二維碼