This is the part 2 of Spring Boot with Consul series I am writing and if you missed part 1 I highly recommend you follow that one here. In this session I am planning to explain how to do the following;
Ok lets get started.
This is a file provided by Spring Boot which loads before application.yml and thus enables to have any load time critical configurations. So we move our Consul related configurations to this file.
spring:
application:
name: web
cloud:
consul:
host: localhost
port: 8500
config:
watch:
enabled: true
The above file now specifies the configurations spring.application.name, spring.cloud.consul.host, spring.cloud.consul.port, etc.
Now our application Specific configurations will go into this file
my:
property:
conf: 10
Inorder to maintain configurations in a centralized manner, we can create Key/Value in Consul as following;
Goto Key/Value table in Consul
By default the root of all configurations will be "config" so we should create a Key/Value by the name config/consul_service_name/any/property.
Thus in our case config/web/my/property/conf. There are more easier and automated ways to do this which will be have look at later. For the time being please create manually which will give a good learning and feel of Consul.
Click save.
You can use the property using the @Value annotation as below;
@RestController
@RequestMapping("/")
@RefreshScope
public class GreetingController {
@Value("${my.property.conf}")
private String conf;
@GetMapping
public String greet() {
return "Hello , "+conf;
}
}
And take special note of the @RefreshScope annotation. This is provided by Spring Cloud to refresh any bean in case it's configurations changed. spring.cloud.consul.config.watch.enabled property which is set in bootstrap.yml starts a Consul Config watcher which refreshes any bean annotation with this annotation as soon as it is changed.
Now you can run the Spring Boot service and when you hit http://localhost:port you should get the following result.
This is because the default value in application.yml for property my.property.conf is set to 10 by default.
Now we can change the value for my.property.conf in Consul to 200 and save.
As soon as we do that, the log Refresh keys changed: [my.property.conf] is made in Spring Boot "web" service.
Now when you hit the app endpoint again you see the configuration is dynamically changed to 200.
That's all for this tutorial. The source code is available at https://github.com/shazin/spring-boot-consul. Let's learn more in coming weeks. Register with us for more informative posts like this one.
https://fedorovkarb.ru Renumax - средство для удаления царапин на машине Эффективное средство для удаления царапин с ЛКП кузова автомобиля. Активная формула средства позволяет максимально быстро восстановить первоначальный вид верхнего покрытия машины.
Read MoreГоднота спасибо _________________ [URL=https://ua.onlinebestrealmoneygames.xyz/avtomati-gralni-onlayn-ukraina/]Автомати гральні онлайн украина[/URL].
Read MoreDeveloping Java Applications with Unicode Data
. Read MorePlease login or register to post a comment.
There are currently no comments.