Spring cloud config集成過程詳解
這篇文章主要介紹了spring cloud config集成過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
Spring Cloud Config 分為
- Config Server:
- 分布式配置中心,是一個獨立的微服務(wù)應(yīng)用,用來連接配置服務(wù)器并為客戶端提供獲取配置信息
- Config Client:
- 通過指定配置中心來管理應(yīng)用資源,以及與業(yè)務(wù)相關(guān)的配置內(nèi)容,并在啟動的時候從配置中心獲取和加載配置信息
Spring boot版本2.1.8.RELEASE
服務(wù)中心使用Consu,啟動Consu
1.配置中心(服務(wù)端)
easy-config
(1)添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--配置中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
(2)配置
在resources下
A. 添加 bootstrap.properties
spring.profiles.active=native本地存儲配置方式
也可以使用git方式
server.port=8091 spring.application.name=easy-config spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port} management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.profiles.active=native spring.cloud.config.server.native.search-locations=classpath:/config/
B. 添加config/easy-api-dev.properties
hello-string=我是來自配置中心的
(3)修改啟動類
添加注解@EnableConfigServer開啟配置服務(wù)支持
package com.tydt.easy.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class EasyConfigApplication { public static void main(String[] args) { SpringApplication.run(EasyConfigApplication.class, args); } }
啟動easy-config
瀏覽器訪問 http://localhost:8091/easy-api/dev
返回結(jié)果
{ "name": "easy-api", "profiles": [ "dev" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/config/easy-api-dev.properties", "source": { "hello-string": "我是來自配置中心的" } } ] }
說明:
配置中心的配置文件會被轉(zhuǎn)化成相應(yīng)的web接口
- /{application}/{profile}[/{label}]
- /{application}/{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}/{profile}.properties
- /{label}/{application}-{profile}.properties
2.客戶端
easy-api
(1)添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
(2)配置
添加配置bootstrap.properties
通過注冊中心的發(fā)現(xiàn)服務(wù),去配置中心查找配置
server.port=8083 spring.application.name=easy-api spring.profiles.active=dev spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-path=/actuator/health spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=easy-config #設(shè)為true,如果無法連接config server,啟動時會拋異常,并停止服務(wù) spring.cloud.config.fail-fast=true
(3)測試方法
package com.tydt.engine.api.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${hello-string}") private String helloString; @RequestMapping("/") public String Hello(){ return "hello,easy-api,"+helloString; } }
3.測試
啟動easy-api
測試地址
http://localhost:8083/
返回結(jié)果
hello,easy-api,我是來自配置中心的
4.更新
修改了配置中心的配置后,如何讀取到新的配置呢
(1)修改測試方法
添加注解 @RefreshScope
package com.tydt.easy.api.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class HelloController { @Value("${hello-string}") private String helloString; @RequestMapping("/") public String Hello(){ return "hello,easy-api,"+helloString; } }
啟動easy-config
啟動easy-api
測試地址
http://localhost:8083/
返回結(jié)果
hello,easy-api,我是來自配置中心的
(2)修改配置
easy-config的resources/config/easy-api-dev.properties
hello-string=我是來自配置中心的111
重啟easy-config
執(zhí)行http://localhost:8083/actuator/refresh
輸出
[ "hello-string" ] http://localhost:8083/
輸出
hello,esay-api,我是來自配置中心的111
說明:
- 如果出現(xiàn)Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
- 無論在 Config Server 中配置什么端口,Config Client 啟動時,會去訪問都默認的 8888 端口
- 出現(xiàn)這種情況可以刪掉以前的配置文件
- 在resources文件夾下,新建 bootstrap.properties 文件( bootstrap.yml)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的實現(xiàn)
二叉排序樹(Binary Sort Tree),又稱二叉查找樹(Binary Search Tree),亦稱二叉搜索樹。本文詳細介紹了二叉排序樹的原理,并且提供了Java代碼的完全實現(xiàn)。需要的可以參考一下2022-01-01SpringBoot中的WebSocketSession原理詳解
這篇文章主要介紹了SpringBoot中的WebSocketSession原理詳解,傳統(tǒng)的?HTTP?協(xié)議是無法支持實時通信的,因為它是一種無狀態(tài)協(xié)議,每次請求都是獨立的,無法保持連接。為了解決這個問題,WebSocket?協(xié)議被引入,需要的朋友可以參考下2023-07-07JAVA如何獲取jvm和操作系統(tǒng)相關(guān)信息
這篇文章主要介紹了JAVA獲取jvm和操作系統(tǒng)相關(guān)信息,使用Java自帶的類進行獲取系統(tǒng)運行的相關(guān)信息,在這整理記錄分享一下,需要的朋友可以參考下2022-10-10SpringDataJpa的使用之一對一、一對多、多對多?關(guān)系映射問題
這篇文章主要介紹了SpringDataJpa的使用?--?一對一、一對多、多對多關(guān)系映射,本文主要講述?@OneToOne、@OneToMany、@ManyToOne、@ManyToMany?這四個關(guān)系映射注解的使用,以及其對應(yīng)的級聯(lián)關(guān)系,需要的朋友可以參考下2022-07-07