日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud 采用Consul做配置中心

發布時間:2023/12/10 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud 采用Consul做配置中心 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

-----------------pom.xml依賴,主要是spring-cloud-starter-consul-config

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId> </dependency> <!--服務發現依賴--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--用于consul配置--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-config</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency> <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.6</version> </dependency>

-----------------bootstrap.yml

server:port: 9201spring:application:name: springtest-serviceprofiles:active: devcloud:consul:host: 10.129.63.40port: 9001discovery:register: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}service-name: ${spring.application.name}port: 9201healthCheckPath: /actuator/healthhealthCheckInterval: 15sconfig:enabled: trueformat: YAMLprefix: configdefaultContext: applicationprofileSeparator: ','data-key: data

注:discovery段是添加到consul注冊中心的

config段說明:

enabled: true允許配置中心

format:YAML 表示consul中的key-value中的value內容,采用YAML格式,據說有四種 YAML PROPERTIES KEY-VALUE FILES

prefix: config 表示consul用于存儲配置的文件夾根目錄名為config

defaultContext: application 表示配置文件對應的默認應用名稱(優先獲取當前服務名稱配置,沒有的到application里找)

profileSeparator: ',' 表示如果有多個profile(eg: 開發環境dev,測試環境test...) ,則key名中的profile與defaultContext之間,用什么分隔符來表示(例如config/springtest-service,dev/data)

data-key: data 表示最后一層節點的key值名稱,一般默認為data

?

---------------TestConfig.java

import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration;@ConfigurationProperties(prefix = "test-config") @Configuration @Data public class TestConfig {private String testValue; }

--------------App.java

@EnableDiscoveryClient @RestController @SpringBootApplication public class SpringtestServerApplication {@Autowiredprivate DiscoveryClient discoveryClient;@Value("${test.testValue}")private String testValue;@Autowiredprivate TestConfig testConfig;public static void main(String[] args) {SpringApplication.run(SpringtestServerApplication.class, args);}@RequestMapping("/test")public String test(String id) {return "test"+id+"/"+testValue+"/"+testConfig.getTestValue();} }

這里面采用了兩種獲取配置的方式

@Value

@ConfigurationProperties

都能進行配置獲取,但是后者可以實現配置更新后動態更新

?

-------------配置

consul頁面上添加key為:

config/springtest-service/data

value為:

test:testValue: consul--asdf34 testConfig:test-value: consul123--asdf34

?

-------------配置優先級

config/testApp,dev/

config/testApp/

config/application,dev/

config/application/

?

示例代碼:

https://github.com/wanghongqi/springcloudconsul_test/tree/master/springtest_server

總結

以上是生活随笔為你收集整理的Spring Cloud 采用Consul做配置中心的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。