Spring Cloud Chinese Manual: https://springcloud.cc/spring-cloud-consul.html
That is to say, when we use consul config, it is best to establish the corresponding directory structure in consul that is suitable for a certain service and the directory structure suitable for all services.
This column consul web UI: springboot provides us with three environment dev: default development environment test: test environment prod: production environment
PS:
1. Every time we modify the configuration information on consul, we will send a /refresh request to our code to refresh our project. By making our values dynamically refresh, there is no need to restart the project. However, when we use @Value to obtain the value, although we can get the value and refresh the project, we will not change the value. We will still get the value configured on consul at the start of the project and refresh it in real time.
2. When we configure the same properties as a service column in the global application file such as: server-sms, when the server-sms project is started, we first look for the configuration under our own directory, and then look for the global configuration. If we are in our own directory,
Practical combat:
pom.xml file configuration:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency>
bootstarp.xml configuration: (If it is configured in the application, it will not take effect, bootstrap.yml will be loaded first than application.properties)
spring: cloud: consult: host: 100.1.00.0 #host: 00.0.100.200 port: 8500 enabled: true config: enabled: true #Default is true -- format: YAML # indicates that there are four types of formats of the file above the consul PROPERTIES KEY-VALUE FILES data-key: configuration # indicates that the KEY value above the consul (or the name of the file) is data by default
FORMAT gives examples to illustrate two types (the YAML of the server-sms project is used in this column):
PROPERTIES:
YAML:
Value method:
1. Apply the value through EnabledConfigurationProperties annotation (modify the property value on the consult after starting the project, and the property value in the project will change immediately):
Corresponding bean:
//The name should be written the same as the name above the consul. Multi-level directories can be written on the consul. For example: userInfo: name: LiaoWenWen //The corresponding writing ConfigurationProperties(prefix="userInfo ") If the project uses more configuration things, it is recommended to use this form @ConfigurationProperties() public class UserInfo { private String name;//It should be consistent with the consul. public String getName() { return name; } public void setName(String name) { this.name = name; }Controller code takes the value:
@Autowired private UserInfo userInfo; @ApiOperation(value="get consulInfo", notes="get consulInfo") public String getConsulInfo() { return userInfo.getName(); }Project startup class:
@SpringBootApplication @EnableDiscoveryClient @EnableConfigurationProperties({UserInfo.class}) public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args);} 2. Get the value through Value annotation (modify the value on the consul after the project is started, and the value in the project does not change. Only the value obtained by starting the project again is the modified value. Sending /refresh request is invalid)
@Value private String name; @ApiOperation(value="get consulInfo", notes="get consulInfo") public String getConsulInfo(){ return name; }3.static value
@Component @Configuration public class ConsulKV { public static String smsUrl; public static String username; public static String password; @Value("${ssbp.smsUrl}") public void setSmsUrl(String smsUrl) { ConsulKV.smsUrl = smsUrl; } @Value("${ccb.job.login.username}") public void setUsername(String username) { ConsulKV.username = username; } @Value("${ccb.job.login.password}") public void setPassword(String password) { ConsulKV.password = password; } Sample code address: https://github.com/LiaoWenn/spring-cloud-consul-config
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.