javascript
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做配置中心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DOS的一个小工具 LOIC
- 下一篇: 洛谷p2234/BZOJ1588 [HN