-----------------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