??這里是【微服務(wù)】,關(guān)注我學(xué)習(xí)微服務(wù)不迷路
??如果對你有幫助,給博主一個免費(fèi)的點(diǎn)贊以示鼓勵
歡迎各位??點(diǎn)贊??評論收藏??
??專欄介紹
【微服務(wù)】 目前主要更新微服務(wù),一起學(xué)習(xí)一起進(jìn)步。
??本期介紹
本期主要介紹Nacos集群搭建以及加載文件配置
文章目錄
目錄
服務(wù)集群
需求
搭建
測試
加載配置文件順序
nacos配置DataId介紹
配置yml文件中的DataId
配置console中的DataId
測試
多環(huán)境配置
服務(wù)集群
需求
-
服務(wù)提供者搭建集群
-
服務(wù)調(diào)用者,依次顯示集群中各服務(wù)的信息
搭建
1)修改服務(wù)提供方的controller,打印服務(wù)端端口號
package com.czxy.controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
public class EchoController {
@Resource
private HttpServletRequest request;
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
int serverPort = request.getServerPort();
return "Hello Nacos Discovery " + string + ":" + serverPort;
}
}
?2)編寫yml配置
#端口號
server:
port: 8170
spring:
application:
name: service-provider #服務(wù)名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服務(wù)地址
#端口號
server:
port: 8270
spring:
application:
name: service-provider #服務(wù)名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服務(wù)地址
3)配置idea啟動項(xiàng)
-Dspring.profiles.active=8170
測試
-
啟動3個服務(wù)(2個服務(wù)提供,1個服務(wù)消費(fèi))
- 查看nacos控制臺
加載配置文件順序
-
對
3.4.5章節(jié)/第4步
內(nèi)容進(jìn)行詳解 -
加載配置文件的順序(第4步詳解)
nacos配置DataId介紹
-
nacos 提供了3種方式,配置dataId的加載順序
A: 共享配置:
spring.cloud.nacos.config.shared-dataids
spring.cloud.nacos.config.refreshable-dataids
B: 加載多配置:
spring.cloud.nacos.config.ext-config[n]
C: 內(nèi)部規(guī)則拼接:
spring.cloud.nacos.config.prefix
spring.cloud.nacos.config.file-extension
spring.cloud.nacos.config.group
配置yml文件中的DataId
spring:
application:
name: config-service # 服務(wù)名
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 # nacos 服務(wù)地址
shared-dataids: test1.yml # 1.1 共享配置
refreshable-dataids: test1.yml # 1.2 共享配置,動態(tài)刷新
ext-config: # 2. 配置多個
- data-id: test2-1.yml
group: DEFAULT_GROUP
refresh: true
- data-id: test2-2.yml
group: DEFAULT_GROUP
refresh: true
prefix: test3 # 3.1 前綴,默認(rèn) ${spring.application.name}
file-extension: yaml # 3.2 后綴
group: DEFAULT_GROUP # 3.3 組名
配置console中的DataId
-
nacos控制臺配置
測試
-
后面加載的dataId將覆蓋前面加載的dataId設(shè)置的內(nèi)容
-
查看日志
Located property source: [
BootstrapPropertySource {name='bootstrapProperties-test3-demo.yaml'}, BootstrapPropertySource {name='bootstrapProperties-test3.yaml'}, BootstrapPropertySource {name='bootstrapProperties-test2-2.yml'}, BootstrapPropertySource {name='bootstrapProperties-test2-1.yml'}, BootstrapPropertySource {name='bootstrapProperties-test1.yml'}]
多環(huán)境配置
-
在Nacos為不同的環(huán)境(開發(fā)、測試、生產(chǎn)等)中,提供了多個不同管理級別的概念,包括:
Data ID
、Group
、Namespace
。
概念 | 描述 |
---|---|
Data ID | 數(shù)據(jù)唯一標(biāo)識,可理解為Spring Cloud應(yīng)用的配置文件名 |
Group | 用來對Data ID 做集合管理,相當(dāng)于小分類 |
Namespace | 用于進(jìn)行租戶粒度的配置隔離。相當(dāng)于大分類 |
- 組group配置
spring.cloud.nacos.config.group=?? ??? ??? ??? ?#組名稱文章來源:http://www.zghlxwxcb.cn/news/detail-793529.html
- 命名空間 namespace配置
spring.cloud.nacos.config.namespace=?? ??? ??? ?#namespace的ID文章來源地址http://www.zghlxwxcb.cn/news/detail-793529.html
到了這里,關(guān)于【微服務(wù)】Nacos集群搭建以及加載文件配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!