詳解SpringCloud mysql實(shí)現(xiàn)配置中心
mysql實(shí)現(xiàn)配置中心
本公司配置數(shù)據(jù)的管理是通過(guò)mysql進(jìn)行配置管理,因?yàn)橐呀?jīng)搭建好了,所以自己動(dòng)手重新搭建一遍,熟悉整個(gè)流程。有關(guān)項(xiàng)目源碼后期會(huì)補(bǔ)上github地址
微服務(wù)要實(shí)現(xiàn)集中管理微服務(wù)配置、 不同環(huán)境不同配置 、 運(yùn)行期間也可動(dòng)態(tài)調(diào)整 、 配置修改后可以自動(dòng)更新的需求 ,Spring Cloud Config同時(shí)滿足了以上要求。
一、項(xiàng)目搭建
本次主要用三個(gè)微服務(wù)
(1)Eureka-server: 7001 注冊(cè)中心
(2)config-server : 5001 配置中心
(3)product-server : 8001 商品微服務(wù)
1、Eureka-server注冊(cè)中心
注冊(cè)中心很簡(jiǎn)單,這里不在重復(fù)些,注冊(cè)中心沒(méi)有變化??梢钥粗皩?xiě)的博客 : SpringCloud(3)---Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)
2、配置中心微服務(wù)
1、pom.xml
<!--服務(wù)中心jar包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--配置中心jar包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--連接msql數(shù)據(jù)庫(kù)相關(guān)jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
2、application.yml
#服務(wù)名稱 server: port: 5001 #連接配置信息 spring: application: name: config-server-jdbc profiles: active: jdbc cloud: config: server: default-label: dev jdbc: sql: SELECT akey , avalue FROM config_server where APPLICATION=? and APROFILE=? and LABEL=? ##################################################################################################### # mysql 屬性配置 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test username: root password: root ##################################################################################################### #指定注冊(cè)中心地址 eureka: client: serviceUrl: defaultZone: http://localhost:7001/eureka/
這里主要講下連接配置信息
(1) spring.profiles.active=jdbc ,自動(dòng)實(shí)現(xiàn)JdbcEnvironmentRepository。
(2)sql語(yǔ)句自定義,否則會(huì)默認(rèn)為“SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?”,具體可以參考 JdbcEnvironmentRepository 實(shí)現(xiàn)。
(3)本人數(shù)據(jù)庫(kù)建表為config_server,由于key,value和profile是mysql關(guān)鍵字,所以我都在最前面加了a。當(dāng)然表名字段名都可以自定義。
(4) {application} 對(duì)應(yīng)客戶端的"spring.application.name"屬性;
{aprofile} 對(duì)應(yīng)客戶端的 "spring.profiles.active"屬性(逗號(hào)分隔的列表); 和
{label} 對(duì)應(yīng)服務(wù)端屬性,這個(gè)屬性能標(biāo)示一組配置文件的版本.
(5)只要 select出來(lái)是兩個(gè)字段 ,框架會(huì) 自動(dòng)包裝到environment的map<key,value> 。
3、mysql數(shù)據(jù)
4、springboot啟動(dòng)類
添加 @EnableConfigServer 注解
@SpringBootApplication @EnableConfigServer public class ConfigserverApplication { public static void main(String[] args) { SpringApplication.run(ConfigserverApplication.class, args); } }
3、product-service微服務(wù)
1、pom.xml
<!--服務(wù)中心jar--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--配置中心客戶端jar--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId>
2、bootstrap.yml
#指定注冊(cè)中心地址 eureka: client: serviceUrl: defaultZone: http://localhost:7001/eureka/ #服務(wù)的名稱 spring: application: name: product-service #指定從哪個(gè)配置中心讀取 cloud: config: discovery: service-id: config-server-jdbc enabled: true profile: dev label: dev server: port: 8001
這里為什么用bootstrap.yml而不用application.yml,是因?yàn)槿鬭pplication.yml 和bootStrap.yml 在同一目錄下,
則 bootStrap.yml 的加載順序要高于application.yml ,即bootStrap.yml 會(huì)優(yōu)先被加載。
為何需要把 config server 的信息放在 bootstrap.yml 里?
當(dāng)使用 Spring Cloud 的時(shí)候,配置信息一般是從 config server 加載的,為了取得配置信息(比如密碼等),你需要一些提早的或引導(dǎo)配置。
因此,把 config server 信息放在 bootstrap.yml,用來(lái)加載真正需要的配置信息。
3、ConfigController類(測(cè)試用)
@RestController @RequestMapping("/api/v1/product") public class ConfigController { @Value("${item_url}") private String url; /** * 輸出url */ @RequestMapping("url") public void list(){ System.out.println(url); }
4、測(cè)試
通過(guò)訪問(wèn):http://localhost:8001/api/v1/product/url 進(jìn)入斷點(diǎn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程
這篇文章主要介紹了Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-06-06SpringBoot+Redis實(shí)現(xiàn)分布式緩存的方法步驟
在高并發(fā)的分布式的系統(tǒng)中,緩存是提升系統(tǒng)性能的重要手段,本文主要介紹了SpringBoot+Redis實(shí)現(xiàn)分布式緩存的方法步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07使用java web 在jsp文件及Class中連接MySQL和SQLserver 的驅(qū)動(dòng)方法
這篇文章主要介紹了使用java web 在jsp文件及Class中連接MySQL和SQLserver的驅(qū)動(dòng)方法的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Spring?AOP利用切面實(shí)現(xiàn)日志保存的示例詳解
最近領(lǐng)導(dǎo)讓寫(xiě)個(gè)用切面實(shí)現(xiàn)日志保存,經(jīng)過(guò)調(diào)研和親測(cè),以完美解決。在這里分享給大家,給有需要的碼友直接使用,希望對(duì)大家有所幫助2022-11-11