(1) Create a Spring Boot project
Create a Maven project module named app-config based on Spring Boot.
(2) Configure POM.xml
Pom files are configured as follows:
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <! --author: Deniro Lee--> <modelVersion>4.0.0</modelVersion> <! </description> <groupId>com.xxx.cc</groupId> <artifactId>app-config</artifactId> < version > 1.0.0 < / version >, < name > app - config < / name > <! Package type --> <packaging>jar</packaging> <! Cc </groupId> <artifactId>ms-app</artifactId> <version>1.0-SNAPSHOT</version> </parent> <! --> <dependencies> <! --spring-cloud-config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>Copy the code
Spring-cloud-config-server is the configuration center server.
The version of Spring Cloud is named after the London Underground station 1. The corresponding relationship between it and the Spring Boot version is as follows 2:
Release Train | Boot Version |
---|---|
2020.0.x aka Ilford | X 2.4. |
Hoxton | 2.2.x, 2.3.x (Starting with SR5) |
Greenwich | X 2.1. |
Finchley | X 2.0. |
Edgware | X 1.5. |
Dalston | X 1.5. |
Dalston, Edgware, and Finchley versions of Spring Cloud are no longer officially maintained.
(3) Configure annotations
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class AppConfigApplication { public static void main(String[] args) { SpringApplication.run(AppConfigApplication.class, args); }}Copy the code
This adds a line annotation @enableconFigServer to Spring Boot’s Application startup class.
(4) Configure applicaton.yml
Spring: Cloud: config: server: native: Classpath :/shared # Load profiles: active: native using file systemsCopy the code
-
Server. port Specifies the port number of the service configuration center.
-
Spring.profiles. active=native indicates that the file system is used to load configuration information. It needs to cooperate with spring. Cloud. Config. Server native. SearchLocations attribute to take effect. The searchLocations property indicates the path where the configuration file is located, which can be the classpath path or file system path 3.
-
The searchLocations attribute supports multiple paths, such as [classpath:/, classpath:/config, file:./, file:./config]. Classpath: indicates the classpath path. File: indicates the file system path.
Note: The file system-based configuration pattern is easy to get started and easy to test. But in a production environment, we must ensure that the file system is reliable and shared by all configuration center instances.
(5) Environment configuration mapping process
The command rules of the configuration file are as follows: Application name. environment name. yml. The requested URL is translated by the configuration center into the corresponding profile name. For example, app-admin/dev is converted to app-admin-dev.yml. The default configuration file is the application name.yml.
Assume that the app-admin application uses the Oracle database by default and the MySQL database in the development environment.
App-admin. yml defines the following contents:
spring:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.Oracle10gDialect
Copy the code
App-admin-dev. yml defines the following contents:
spring:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
Copy the code
Configuration center starts, the browser to http://localhost:8881/app-admin/dev, you can get a JSON form of app – admin – dev. The contents of the yml.
You can see that profiles is dev in the response content, along with the content in app-admin.yml.
When the Spring framework performs property resolution, it looks for the value of the property in the default configuration and overrides the default value of 1 with the value of the specific environment (if present).
1 John Carnell. Spring Micro service field (asynchronous books) (Kindle location 1570-1666). Posts and Telecommunications Press. Kindle version 2 Spring Cloud OVERVIEW 3 File System Backend