SpringCloud Nacos配置中心管理超詳細(xì)講解
一、Nacos配置管理
Nacos除了可以做注冊(cè)中心,同樣可以做配置管理來(lái)用
1.1 統(tǒng)一配置管理
當(dāng)微服務(wù)部署越來(lái)越多,達(dá)到數(shù)十,數(shù)百時(shí),逐個(gè)修改微服務(wù)配置就會(huì)很麻煩,且容易出錯(cuò)。我們需要一種統(tǒng)一配置管理方案,可以集中管理所有實(shí)例的配置。

Nacos一方面更可以將配置集中管理另一方面在配置變更時(shí),及時(shí)通知微服務(wù),實(shí)現(xiàn)配置的熱更新。
1.1.1在nacos中添加配置文件
如何在nacos中添加配置文件

然后在彈出的表單中,填寫(xiě)配置信息:

注意:項(xiàng)目的核心配置,需要熱更新的配置才有放到nacos管理的必要。基本不會(huì)變更的一些配置還是保存在微服務(wù)本地比較好。
1.1.2 從微服務(wù)拉取配置
微服務(wù)要拉取nacos中管理的配置,并且與本地的application.yml配合合并才能完成項(xiàng)目的啟動(dòng)。
但是如果尚未讀取application.yml,又如何得知nacos地址呢?
因此spring引入了一種新的配置文件:bootstrap.yaml文件,會(huì)在application.yml之前被讀取,流程如下:

引入nacos-config依賴
首先,在user-serivice服務(wù)中,引入nacos-config的客戶端依賴:
<!--nacos配置管理依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2)添加bootstrap.yaml
然后,在user-service中添加一個(gè)bootstrap.yaml文件,內(nèi)容如下:
spring:
application:
name: userservice # 服務(wù)名稱
profiles:
active: dev #開(kāi)發(fā)環(huán)境,這里是dev
cloud:
nacos:
server-addr: localhost:8848 # Nacos地址
config:
file-extension: yaml # 文件后綴名
這里會(huì)根據(jù)spring.cloud.nacos.server-addr獲取nacos地址,再根據(jù)
${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}作為文件id,來(lái)讀取配置。
本例中,就是去讀取userservice-dev.yaml:

3)讀取nacos配置
在user-service中的UserController中添加業(yè)務(wù)邏輯,讀取pattern.dateformat配置:

完整代碼:
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Value("${pattern.dateformat}")
private String dateformat;
@GetMapping("now")
public String now(){
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
}
}在頁(yè)面訪問(wèn),可以看到效果:

1.2 配置熱更新
我們最終的目的,是修改nacos中的配置后,微服務(wù)中無(wú)需重啟即可讓配置生效,也就是配置熱更新。
要實(shí)現(xiàn)配置熱更新,可以使用兩種方式:
1.2.1 方式一
在@Value注入的變量所在類上添加注解@RefreshScope:

1.2.2 方式二
使用@ConfigurationProperties注解代替@Value注解。
在user-service服務(wù)中,添加一個(gè)類,讀取patterrn.dateformat屬性:
package cn.itcast.user.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@Data
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
private String dateformat;
}在UserController中使用這個(gè)類代替@Value:

完整代碼:
import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private PatternProperties patternProperties;
@GetMapping("now")
public String now(){
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
}
}1.3 配置共享
其實(shí)微服務(wù)啟動(dòng)時(shí),會(huì)去nacos讀取多個(gè)配置文件,例如:
[spring.application.name]-[spring.profiles.active].yaml,例如:userservice-dev.yaml[spring.application.name].yaml,例如:userservice.yaml
而[spring.application.name].yaml不包含環(huán)境,因此可以被多個(gè)環(huán)境共享。
下面我們通過(guò)案例來(lái)測(cè)試配置共享
1)添加一個(gè)環(huán)境共享配置
我們?cè)趎acos中添加一個(gè)userservice.yaml文件:

2)在user-service中讀取共享配置
在user-service服務(wù)中,修改PatternProperties類,讀取新添加的屬性:

在user-service服務(wù)中,修改UserController,添加一個(gè)方法:

3)運(yùn)行兩個(gè)UserApplication,使用不同的profile


這樣,UserApplication(8081)使用的profile是dev,UserApplication2(8082)使用的profile是test。
啟動(dòng)UserApplication和UserApplication2
訪問(wèn)http://localhost:8081/user/prop,結(jié)果:

訪問(wèn)http://localhost:8082/user/prop,結(jié)果:

可以看出來(lái),不管是dev,還是test環(huán)境,都讀取到了envSharedValue這個(gè)屬性的值。
4)配置共享的優(yōu)先級(jí)
當(dāng)nacos、服務(wù)本地同時(shí)出現(xiàn)相同屬性時(shí),優(yōu)先級(jí)有高低之分:

到此這篇關(guān)于SpringCloud Nacos配置中心管理超詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringCloud Nacos配置中心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jackson2的JsonSchema實(shí)現(xiàn)java實(shí)體類生成json方式
這篇文章主要介紹了Jackson2的JsonSchema實(shí)現(xiàn)java實(shí)體類生成json,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
springBoot 過(guò)濾器去除請(qǐng)求參數(shù)前后空格實(shí)例詳解
這篇文章主要為大家介紹了springBoot 過(guò)濾器去除請(qǐng)求參數(shù)前后空格實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
詳解如何在Spring中為@Value注解設(shè)置默認(rèn)值
在Spring開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到需要從配置文件中讀取屬性的情況,@Value注解是Spring提供的一種便捷方式,能夠讓我們輕松地將配置文件中的屬性注入到Spring Bean中,2024-10-10
Spring中數(shù)據(jù)訪問(wèn)對(duì)象Data Access Object的介紹
今天小編就為大家分享一篇關(guān)于Spring中數(shù)據(jù)訪問(wèn)對(duì)象Data Access Object的介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
java判斷今天,昨天,前天,不能用秒間隔的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java判斷今天,昨天,前天,不能用秒間隔的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03

