Spring Cloud is the popular distributed service framework nowadays, and it provides many useful components. For example: configuration center, Eureka service discovery, message bus, fuse mechanism, etc.
The configuration center is relatively basic among the many components of Spring Cloud. It provides unified management of configuration files and can easily switch to uninterrupted environments.
Its specific structure is as follows:
Spring Cloud is based on Spring Boot, and Spring Cloud is inseparable from Spring Boot, so our projects are all based on Spring Boot.
We usually place configuration files on git, which is easy to use for version control. Next, let’s talk about the construction of the configuration center.
Configuration center construction
First, introduce the dependency BOM of Spring Boot and Spring Cloud:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>
Here we need to take a closer look at the official documentation. The Finchley version of Spring Cloud is based on Spring Boot 2.0 and cannot work under Spring Boot 1.5. The Edgware version is based on 1.5 and does not work properly under 2.0. Everyone should pay attention to this to avoid unnecessary trouble.
Here we use Spring Cloud's Edgware and Spring Boot version 1.5.
Then pour in the necessary dependencies as follows:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency></dependencies>
Finally, write the start class of the configuration center, as follows:
@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}@EnableConfigServer marks that this service is a configuration center service. The specific information is configured in the application.properties file:
#Server port server.port=9000# configuration file git address spring.cloud.config.server.git.uri=https://github.com/liubo-tech/spring-cloud-properties# configuration file temporary file directory spring.cloud.config.server.git.basedir=/d:/config-repo
Everyone understands the service port and git address, and the third is the directory where temporary files are configured. After the configuration center is called, the configuration center will pull the configuration file from git and cache it locally. This is the directory for the configuration cache, and it can also be configured without configuration and use the system default. In this way, the configuration center is set up, and the cluster can be built through Nginx to make it highly available.
Access configuration file formats are as follows:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.propertiesin:
Client calls
In the past, configuration files were placed in projects, which made it very troublesome when switching different environments, and some sensitive information of configurations was also exposed to developers.
This can be avoided using a unified configuration center, let's see how the client calls it.
First, import the jar package that must be relied on, as follows:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency></dependencies>
As long as the jar of the Spring Cloud Config Client is under the classpath of the project, it will get the configuration from the configuration center when the project starts, and specify the configuration center through the spring.cloud.config.uri property in the bootstrap configuration file.
This makes the client's project must have a bootstrap.yml or bootstrap.properties file, otherwise the client will not load the configuration file from the configuration center.
We create bootstrap.properties as follows:
#Configure the center address spring.cloud.config.uri=http://localhost:9000
Specify the address of the configuration center. In the above example, we specify 9000 of the port of the configuration center. We configure the name of the application in the application.properties file:
spring.application.name=eg-config
Our application is called "eg-config". When the project starts, the eg-config file will be loaded from the configuration center. Next, let's make an example, create a bean and inject values from the configuration center
@Componentpublic class MyBean { @Value("${my.name}") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}Where name will load the property my.name from the configuration center. The startup class is as follows:
@SpringBootApplicationpublic class Application { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args); //Get the bean and print the name field MyBean bean = applicationContext.getBean(MyBean.class); System.out.println(bean.getName()); }} After startup, the results of the console printing are as follows: test
This will be introduced in the configuration center. For details, please refer to the project example: https://github.com/liubo-tech/spring-cloud-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.