Spring cloud config集成過程詳解
這篇文章主要介紹了spring cloud config集成過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
Spring Cloud Config 分為
- Config Server:
- 分布式配置中心,是一個獨立的微服務(wù)應用,用來連接配置服務(wù)器并為客戶端提供獲取配置信息
- Config Client:
- 通過指定配置中心來管理應用資源,以及與業(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)化成相應的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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決
這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
Spring?component-scan?XML配置與@ComponentScan注解配置
這篇文章主要介紹了Spring?component-scan?XML配置與@ComponentScan注解配置,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析
這篇文章主要介紹了Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟
這篇文章主要介紹了IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟,首先安裝GenerateSerialVersionUID插件,當出現(xiàn)添加serialVersionUID選項,選中則會自動生成serialVersionUID,感興趣的朋友一起學習下吧2024-02-02
使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
這篇文章主要介紹了使用spring?security?BCryptPasswordEncoder接入系統(tǒng)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
在IntelliJ IDEA中為自己設(shè)計的類庫生成JavaDoc的方法示例
這篇文章主要介紹了在IntelliJ IDEA中為自己設(shè)計的類庫生成JavaDoc的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
SwiftUI中級List如何添加新內(nèi)容(2020年教程)
這篇文章主要介紹了SwiftUI中級List如何添加新內(nèi)容,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01

