Beginning with the basics
Let's go back to the previous chapter for a moment. There I have already described in detail the structure of a Spring Boot project. Configuration should be provided in YAML or a properties file with the application or the application-{profile} name. In contrast to a standard Spring Boot application, Spring Cloud is based on the configuration taken from a remote server. However, minimal settings are needed inside the application; for example, its name and config server address. That's why a Spring Cloud application creates a bootstrap context, which is responsible for loading properties from the external sources. Bootstrap properties are added with the highest priority and they cannot be overridden by local configuration. Bootstrap context, which is a parent for the main application context, uses bootstrap.yml instead of application.yml. Usually, we put the application name and Spring Cloud Config settings, as follows:
spring:
application:
name: person-service
cloud:
config:
uri: http://192.168.99.100:8888
Bootstrap context startup can be easily disabled by setting the spring.cloud.bootstrap.enabled property to false. We can also change the name of the bootstrap configuration file using the spring.cloud.bootstrap.name property or even changing the location by setting spring.cloud.bootstrap.location. The profile mechanisms are also available here, so we are allowed to create, for example, bootstrap-development.yml, which is loaded on an active development profile. This and some other features are available in the Spring Cloud Context library, which is added as a parent dependency to the project classpath together with any other Spring Cloud library. One of these features is some additional management endpoints included with the Spring Boot Actuator:
- env: A new POST method for Environment, log levels update, and @ConfigurationProperties rebind
- refresh: This reloads bootstrap context and refreshes all beans annotated with @RefreshScope
- restart: This restarts Spring ApplicationContext
- pause: This stops Spring ApplicationContext
- resume: This starts Spring ApplicationContext
The next library together with Spring Cloud Context, which is included as a parent dependency with Spring Cloud projects, is Spring Cloud Commons. It provides a common abstraction layer for mechanisms such as service discovery, load balancing, and circuit breakers. These include, among other things, frequently used annotations such as @EnableDiscoveryClient or @LoadBalanced. I'll present more details about them in the following chapters.