[英]Spring boot Get properties from consul server
我有一個春季啟動應用程序,我想獲得我在領事代理上的屬性。
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages={"com.commons"})
public class MainAppProxy implements CommandLineRunner {
@Value("${proxy.endpoint}")
private String endpointAddress;
我的application.properties在src / main / resources下
spring.application.name=SOAPProxy
spring.cloud.consul.host=http://10.0.1.241
spring.cloud.consul.port=8500
spring.cloud.config.discovery.enabled=false
在pom.xml中我有以下配置(短版)
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
這些屬性以以下格式存儲在consul上:Business / SOAPProxy / proxy.endpoint
當應用程序啟動時,它似乎找到了consul,但它在嘗試consul @Value之前無法獲得值(“$ {proxy.endpoint}”)如何獲得領事的屬性?
您可以使用三種方式從領事加載配置
我在yaml中使用加載配置。
這是我的bootstrap.yml文件(你也可以使用.property文件)
spring:
application:
name: SOAPProxy
---
spring:
profiles: default
cloud:
consul:
config:
data-key: data
prefix: config
format: yaml
host: localhost
port: 8500
discovery:
prefer-ip-address: true
我的啟動應用程序注釋如下
@EnableDiscoveryClient
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootConsulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsulApplication.class, args);
}
}
maven dependancy像這樣添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
這是consul agent key / value的配置
現在在啟動時所有配置加載到應用程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.