In Spring Cloud, a framework of Spring Cloud Config was developed to build a configuration center, and configuration servers and multiple configuration warehouse implementations were provided.
- Introducing maven dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
2. Start to use @EnableConfigServer
@SpringBootApplication @EnableConfigServer public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
The @EnableConfigServer is used to enable the Spring Cloud Config service. With this annotation, the configuration server can convert the stored configuration information into RESTful interface data for each business microservice to use in a distributed environment.
There are many implementations of configuration warehouses in Spring Cloud Config, the most common of which are local file system-based and Git-based.
3. Configuration scheme based on local file system
When building a configuration warehouse using a local profile schema, a typical project engineering structure is illustrated below:
Create a springconfig folder in the src/main/resources directory and deviceservice and userservice folders under that folder, which must match the names of their microservices.
application.yml configuration content
server: port: 9090 #Local file based spring: profiles: active: native cloud: config: server: native: search-locations: classpath:springconfig/,classpath:springconfig/deviceservice,classpath:springconfig/userservice
The userservice.yml configuration information is as follows:
spring: jpa: database: MYSQL datasource: platform: mysql url: jdbc:mysql://127.0.0.1:3306/config_server username: root password: default driver-class-name: com.mysql.jdbc.Driver
Spring Cloud Config provides a powerful integration gateway where the configuration server automatically converts the configuration file information stored in the local file system into RESTful-style interface data. When we start the configuration service, it can be accessed through the HTTP interface
localhost:9090/userservice/default
Because we visited http://localhost:8888/userservice/default The endpoint, which is equivalent to getting the configuration information in the userservice.yml file, so the "profiles" value here is "default", which means our profile's Profile is the default environment.
4. Based on third-party warehouse configuration
The configuration is as follows:
server: port: 9090 #git-based warehouse spring: cloud: config: discovery: enabled: true server: encrypt: enabled: false git: uri: https://github.com/gitname/springcloud-demo/config-repository search-paths: deviceservice,userservice username: your_username password: pass
The above is a demo based on github
2. Use of Spring Cloud Config Client
- Introducing Spring Cloud Config Client dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
2. Configuration obtains the test configuration under the userservice service name:
spring: application: name: userservice profiles: active: test cloud: config: enabled: true uri: http://localhost:9090 config: import: optional:configserver:http://localhost:9090
http://localhost:9090/userservice/test
{ "name": "userservice", "profiles": [ "test" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "class path resource [springconfig/userservice/userservice-test.yml]", "source": { "spring.jpa.database": "MYSQL", "spring.datasource.platform": "mysql", "spring.datasource.url": "jdbc:mysql://127.0.0.1:3306/config_server", "spring.datasource.username": "root", "spring.datasource.password": "test", "spring.datasource.driver-class-name": "com.mysql.jdbc.Driver" } }, { "name": "class path resource [springconfig/userservice/userservice.yml]", "source": { "spring.jpa.database": "MYSQL", "spring.datasource.platform": "mysql", "spring.datasource.url": "jdbc:mysql://127.0.0.1:3306/config_server", "spring.datasource.username": "root", "spring.datasource.password": "default", "spring.datasource.driver-class-name": "com.mysql.jdbc.Driver" } } ] }
3. Get Configuration
@Configuration @ConfigurationProperties(prefix = "spring.datasource") public class DataSourceConfig { private String platform; private String url; private String username; private String password; private String driverClassName; //getter and setter }
4. Use configuration information obtained from the Configuration Center