电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring Cloud Config 使用本地配置文件方式

瀏覽:23日期:2023-07-01 15:57:15
一、簡介

在分布式系統中,由于服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。

在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。

在spring cloud config 組件中,分兩個角色,一是config server,二是config client。

二、配置2.1 Spring Cloud Config Server項目

1 pom.xml中導入Config Server需要的包

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId></dependency>

2 在Application類中添加@EnableConfigServer注解

package com.sunbufu;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication { public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args); }}

3 修改配置文件application.yml,指定本地客戶端配置文件的路徑

spring: profiles: active: native cloud: config: server:native: searchLocations: F:/conf

4 準備客戶端配置文件

Spring Cloud Config 使用本地配置文件方式

client-dev.yml文件的內容:

server: #設置成0,表示任意未被占用的端口 port: 8081nickName: world2.2 Spring Cloud Config Client項目

1 pom.xml中導入Config Client需要的包(注意,此處跟Config Server的配置不同)

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency>

2 在src/main/resources中,新建bootstrap.yml文件

bootstrap文件會在application文件之前加載,一般是不會變的。

spring: application: name: client cloud: config: uri: http://127.0.0.1:8888 profile: dev label: master

資源文件映射如下:

/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

3 新建HelloController用來顯示讀取到的配置

package com.sunbufu.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @Value('${nickName}') private String nickName; @RequestMapping('/hello') public String hello() {return 'hello ' + nickName; }}

Spring Cloud Config 使用本地配置文件方式

三、總結

源碼地址 :https://github.com/sunbufu/sunbufu-cloud

總覺的使用svn或者git不如直接修改配置文件方便,特此記錄下來。

spring cloud config本地讀取配置文件1、創建maven項目,引入spring boot 起步依賴

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wxz</groupId> <artifactId>cloud-config-demo3</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-config-demo3</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <dependencyManagement><dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope> </dependency></dependencies> </dependencyManagement> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

新建config-server 模塊,引入依賴

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>com.wxz</groupId><artifactId>cloud-config-demo3</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wxz</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-server</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.1.2.RELEASE</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>2、配置文件:

spring: cloud: config: server:native: search-locations: classpath:/shared profiles: active: native application: name: config-serverserver: port: 8769

在resources下新建目錄shared,里面新建文件config-client-dev

server: port: 8762foo: foo version 1

在啟動類添加

@EnableConfigServer

新建config-client模塊

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>com.wxz</groupId><artifactId>cloud-config-demo3</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wxz</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.1.2.RELEASE</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

新建配置文件bootstrap.yml(bootstrap比application具有優先的讀取順序)

spring: cloud: config: uri: http://localhost:8769 fail-fast: true application: name: config-client profiles: active: dev

新建一個controller進行測試:

package com.wxz.configclient.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @author Wangxingze * @date 2019-08-26 12:58 */@RestControllerpublic class Test { @Value('${foo}') public String foo; @GetMapping('/t') public String t(){return foo; }}

依次啟動server client,啟動時可以看到讀取了配置文件和啟動黛安克,訪問:

Spring Cloud Config 使用本地配置文件方式

注意:spring boot 和cloud的版本以及config依賴的版本

<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.7.RELEASE</version><relativePath/> <!-- lookup parent from repository -->

我這里好像沒有指定spring cloud 的版本呀

config的相關依賴使用:<version>2.1.2.RELEASE</version>

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 自清洗过滤器_全自动过滤器_全自动反冲洗过滤器_量子过滤器-滑漮滴 | 纸布|钩编布|钩针布|纸草布-莱州佳源工艺纸布厂 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | 户外环保不锈钢垃圾桶_标识标牌制作_园林公园椅厂家_花箱定制-北京汇众环艺 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 无味渗透剂,泡沫抑尘剂,烷基糖苷-威海威能化工有限公司 | 视频直播 -摄影摄像-视频拍摄-直播分发 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 螺纹三通快插接头-弯通快插接头-宁波舜驰气动科技有限公司 | 英语词典_成语词典_日语词典_法语词典_在线词典网 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | 法兰螺母 - 不锈钢螺母制造厂家 - 万千紧固件--螺母街 | 中药二氧化硫测定仪,食品二氧化硫测定仪|俊腾百科 | 公交驾校-北京公交驾校欢迎您! 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 中控室大屏幕-上海亿基自动化控制系统工程有限公司 | 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 华中线缆有限公司-电缆厂|电缆厂家|电线电缆厂家 | ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 软文世界-软文推广-软文营销-新闻稿发布-一站式软文自助发稿平台 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 邢台人才网_邢台招聘网_邢台123招聘【智达人才网】 | 江西自考网 | 无线对讲-无线对讲系统解决方案-重庆畅博通信 | 环氧树脂地坪漆_济宁市新天地漆业有限公司 |