Another problem with the traditional approach, especially if the properties were externalized via file or environment variables is that there's no traceability. The latest ones are always taken and we don't know what the properties were before or who changed it.
Spring Cloud Config provides a centralized, externalized, secure and easy way for storing and serving configurations for applications for different environments:. In short, we have a Config Server running as a separate application that hooks to a Git repository.
When we start up a new application Config Client , it gets all the needed properties from the Config Server. It doesn't matter if the application existed when we set the server up or not. As always, we start by using Spring Initializr. Select your preferred version of Spring Boot, add the Config Server dependency and generate it as a Maven project:. By annotating our main class with EnableConfigServer , we mark it as being a config server:. Here, we defined the port on which config server would be running.
Then we specified the Git URL that it needs to hook for properties. Note : By default, Spring searches for the property files at the root. If we have to specify a particular folder we can provide the location via searchPaths. We can start the config server now. Here, we created greeting-service-cloud service with Web , Config Client , and Actuator dependencies. This annotation allows the bean to be refreshed dynamically at runtime:. Apart from application.
Here, we first set the application name. The Spring Config Server will search for this file name in the Git repository and serve its contents. We also have to mention where the Config Server is running by specifying it in spring. So we externalized our properties and have nice traceability of it in our Git repository. Some important points worth noting:.
By default, the configuration values from properties files are ready or fetched at the application startup and not again. If there are some changes to be made, we still have to restart the application. To solve this we added the Actuator dependency to our application. It provides some production-ready endpoints that can give insights regarding our application which can be used for administrative purposes. We have to enable these endpoints manually by specifying management. That is possible, but I'll explain this in an upcoming blog post!
To do this, we create a micro service. Let's call it the config-service. Add the following dependencies to your config-service. SpringApplication; import org. SpringBootApplication; import org. Now let's create a sample service. Add the following dependencies to your example service. Value; import org. RequestMapping; import org. Add spring-boot-starter-actuator to your example service. RefreshScope; import org.
Jeroen Bellen. Encrypt Decrypt Cloud Config Properties. Spring Netflix Zuul. Join our subscribers list to get the latest updates and articles delivered directly in your inbox. Spring Cloud Config Server Implementation We already have the setup ready for this implementation in my previous article.
SpringApplication; import org. SpringBootApplication; import org. This is the same file we defined in our previous app here bootstrap. A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Further Reading on Spring Cloud 1. Spring Boot Redis Cache 2. Spring Cloud Gateway Example 3. Spring Cloud Config 4.
Spring Cloud Netflix Eureka 5. Introduction To Microservices 6. Encrypt Decrypt Cloud Config Properties 7.
0コメント