- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- SpringCloud微服務(wù)之Config知識總結
創(chuàng )建倉庫
創(chuàng )建文件
master分支
# config-dev.yml config: info: "config info for dev(master)" # config-test.yml config: info: "config info for test(master)" # config-prod.yml config: info: "config info for prod(master)"
dev分支
# config-dev.yml config: info: "config info for dev(dev)" # config-test.yml config: info: "config info for test(dev)" # config-prod.yml config: info: "config info for prod(dev)"
添加依賴(lài)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.2.0.RELEASE</version> </dependency>
添加配置
啟動(dòng)類(lèi)
@SpringBootApplication @EnableEurekaClient @EnableConfigServer public class SpringcloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudConfigServerApplication.class, args); } }
application.yml配置文件
server: port: 8888 spring: application: name: config-server cloud: config: server: # 配置存儲配置信息的Git倉庫 git: uri: https://gitee.com/prochick/spring-cloud-config.git # Git用戶(hù)名 username: xxx # Git密碼 password: xxx # 指定是否開(kāi)啟啟動(dòng)時(shí)直接從git獲取配置 clone-on-start: true eureka: instance: prefer-ip-address: true instance-id: config-server-8888 client: fetch-registry: false register-with-eureka: true service-url: defaultZone: http://localhost:8010/eureka/
訪(fǎng)問(wèn)說(shuō)明
# 獲取配置信息 /{label}/{application}-{profile} # 獲取配置文件信息 /{label}/{application}-{profile}.yml
application
代表應用名稱(chēng),默認為配置文件中的spring.application.name,如果配置了spring.cloud.config.name,則為該名稱(chēng)
label
代表分支名稱(chēng),對應配置文件中的spring.cloud.config.label
profile
代表環(huán)境名稱(chēng),對應配置文件中的spring.cloud.config.profile
測試使用
# 訪(fǎng)問(wèn)http://localhost:8888/master/config-dev來(lái)獲取master分支上dev環(huán)境的配置信息 # 訪(fǎng)問(wèn)http://localhost:8888/master/config-dev.yml來(lái)獲取master分支上dev環(huán)境的配置文件信息 # 訪(fǎng)問(wèn)http://localhost:8888/master/config-test.yml來(lái)獲取master分支上test環(huán)境的配置文件信息 # 訪(fǎng)問(wèn)http://localhost:8888/dev/config-dev.yml來(lái)獲取dev分支上dev環(huán)境的配置文件信息
添加依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.2.0.RELEASE</version> </dependency>
添加配置
配置文件
bootstrap.yml
server: port: 9999 spring: application: name: config-client cloud: config: # 配置中心地址 uri: http://localhost:8888 # 分支名稱(chēng) label: master # 配置文件名稱(chēng) name: config # 配置后綴名稱(chēng) profile: dev eureka: instance: prefer-ip-address: true instance-id: config-client-9999 client: fetch-registry: false register-with-eureka: true service-url: defaultZone: http://localhost:8010/eureka/ # 暴露刷新監控 management: endpoints: web: exposure: include: 'refresh'
控制器類(lèi)
@RestController @RefreshScope public class ConfigController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
測試使用
# 訪(fǎng)問(wèn)http://localhost:9999/configInfo 可以獲取到dev分支下dev環(huán)境的配置
添加依賴(lài)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
添加配置
服務(wù)端配置
server: port: 8888 spring: application: name: config-server # 配置存儲配置信息的Git倉庫 cloud: config: server: git: # 訪(fǎng)問(wèn)地址 uri: https://gitee.com/prochick/spring-cloud-config.git # Git用戶(hù)名 username: xxx # Git密碼 password: xxx # 指定是否開(kāi)啟啟動(dòng)時(shí)直接從git獲取配置 clone-on-start: true # 配置用戶(hù)名和密碼 security: user: name: xxx password: xxx
客戶(hù)端配置
server: port: 9999 spring: application: name: config-client cloud: config: # 配置中心地址 uri: http://localhost:8888 # 分支名稱(chēng) label: master # 配置文件名稱(chēng) name: config # 配置后綴名稱(chēng) profile: dev # 配置中心用戶(hù)名 username: xxx # 配置中心密碼 password: xxx
添加配置
bootstrap.yml
server: port: 9999 spring: application: name: config-client cloud: config: # 分支名稱(chēng) label: master # 配置文件名稱(chēng) name: config # 配置后綴名稱(chēng) profile: dev # 集群綁定 discovery: enabled: true service-id: config-server eureka: instance: prefer-ip-address: true instance-id: config-client-9999 client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:8010/eureka/
測試訪(fǎng)問(wèn)
# 訪(fǎng)問(wèn)eureka-server
# 訪(fǎng)問(wèn)http://localhost:9999/configInfo 可以獲取到dev分支下dev環(huán)境的配置
到此這篇關(guān)于SpringCloud微服務(wù)之Config知識總結的文章就介紹到這了,更多相關(guān)SpringCloud Config內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站