Spring boot从领事服务器获取属性
问题描述:
我有一个弹簧启动应用程序,我想获取我在代理上的属性。Spring boot从领事服务器获取属性
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages={"com.commons"})
public class MainAppProxy implements CommandLineRunner {
@Value("${proxy.endpoint}")
private String endpointAddress;
我application.properties是在src /主/资源
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>
属性存储上领事格式为: 商务/的SOAPProxy/proxy.endpoint
当应用程序引导,似乎它找到领事,但它不能像试用consul @Value(“$ {proxy.endpoint}”)之前那样得到值。我怎样才能获得领事上的属性?
答
您可以使用三种方法,从领事加载配置
- 键/值
- YAML
- 文件
我在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);
}
}
行家扶养添加这样
<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>
这是配置代理键/值
现在在启动所有配置应用负载
你需要从领事获取配置? – wthamira