- Mastering Spring Cloud
- Piotr Minkowski
- 439字
- 2021-08-27 18:57:01
Discovery and distributed configuration
Service discovery and distributed configuration management are vital parts of the microservices architecture. The technical implementation of these two different mechanisms is pretty similar. It comes down to storing parameters under specific keys in a flexible key-value storage. Actually, there are several interesting solutions available on the market which provide both of these functionalities. Spring Cloud integrates with the most popular of them. But there is also one exception where Spring Cloud has its own implementation created only for distributed configuration. This feature is available under the Spring Cloud Config project. In contrast, Spring Cloud does not provide its own implementation for service registration and discovery.
As usual, we can divide this project into the server and client-side support. The server is the one, central place where all of the external properties for applications are managed across all of the environments. Configuration can be maintained simultaneously in several versions and profiles. This is achieved by using Git as a storage backend. The mechanism is really smart and we will discuss it in detail in Chapter 5, Distributed Configuration with Spring Cloud Config. The Git backend is not the only one option for storing properties. The config files could also be located on a file system or server classpath. The next option is to use Vault as a backend. Vault is an open source tool for managing secrets such as tokens, passwords, or certificates released by HashiCorp. I know that many organizations pay particular attention to security issues such as storing credentials in a secure place, so it could be the right solution for them. Generally, we can also manage security on the configuration server access level. No matter which backend is used for storing properties, Spring Cloud Config Server exposes an HTTP, resource-based API which provides easy access to them. By default, this API is secured with basic authentication, but it is also available to set an SSL connection with private/public key authentication.
A server can be run as an independent Spring Boot application with properties exposed over the REST API. To enable it for our project we should add the spring-cloud-config-server dependency. There is also support on the client-side. Every microservice that uses a configuration server as a properties source needs to connect to it just after startup, before creating any Spring beans. Interestingly, the Spring Cloud Config Server can be used by non Spring applications. There are some popular microservice frameworks that integrate with it on the client side. To enable Spring Cloud Config Client for your application you need to include the spring-cloud-config-starter dependency.