In the previous article, "Spring Cloud Building MicroServices Architecture: Distributed Configuration Center," We introduced the Spring Cloud Server Configuration Center using git to configure the information store. This ingenious use of Git's own mechanisms and other rich git server-side offerings has allowed spring Cloud servers to circumvent many of the complex management-related implementations of Configuration storage and management, giving them the basic ability to configure central storage configurations and read configurations , and the higher-level management mechanism, because it does not have universal applicability, so Spring Cloud server does not have to implement this part of the content, but through the GIT service-side products to provide a part of the implementation, if you need more complex features can also be implemented and defined. Even so, the scenario for Spring Cloud server to use Git to store configurations by default has long been controversial. So, this article will introduce a new way of configuring Spring Cloud config from the Edgware version: Using database storage configuration information.
Building the configuration Center service side
The first step: Create a basic spring boot project that introduces several major dependencies in Pom.xml:
spring-cloud-config-server
: The base dependency of the configuration center
spring-boot-starter-jdbc
: You need to load the JDBC dependency because you need to access the database
mysql-connector-java
: Connection pack for MySQL database
flyway-core
: This content is not mandatory and is primarily used to manage the schema (if you don't understand it, you can read this article)
<parent> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter-parent</artifactId> <version>1.5.11.RELEASE</version> <relativePath/></parent><dependencies> <dependency> <groupId>Org.springframework.cloud</groupId> <artifactId>Spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>Org.flywaydb</groupId> <artifactId>Flyway-core</artifactId> <version>5.0.3</version> </dependency> <dependency> <groupId>Mysql</groupId> <artifactId>Mysql-connector-java</artifactId> <version>5.1.21</version> </dependency></dependencies><dependencyManagement> <dependencies> <dependency> <groupId>Org.springframework.cloud</groupId> <artifactId>Spring-cloud-dependencies</artifactId> <version>Edgware.sr3</version> <type>Pom</type> <scope>Import</scope> </dependency> </dependencies></dependencyManagement>
Step two: Prepare the schema to create the file. resources
under Create schema
directory, and add the V1__Base_version.sql
file, the specific content is as follows:
CREATE TABLE' Properties ' (' ID ')int( One) not NULL, ' key 'varchar( -) not NULL, ' value 'varchar( -) not NULL, ' Application 'varchar( -) not NULL, ' profile 'varchar( -) not NULL, ' label 'varchar( -) not NULL,PRIMARY KEY(' ID ')) Engine=innodbDEFAULTCharset=utf8;
The script is automatically executed by Flyway when the program is run
Step Three: Create the application main class, as follows:
@EnableConfigServer@SpringBootApplication Public classConfigserverbootstrap { Public Static void Main(string[] args) {ApplicationContext context = springapplication.Run(Configserverbootstrap.class);//test data, used only for testing purposes in this documentJdbcTemplate JdbcTemplate = context.Getbean(JdbcTemplate.class); JdbcTemplate.Execute("Delete from properties"); JdbcTemplate.Execute(INSERT into Properties VALUES (1, ' com.didispace.message ', ' test-stage-master ', ' config-client ', ' stage ', ' Master ' )"); JdbcTemplate.Execute(INSERT into Properties VALUES (2, ' com.didispace.message ', ' test-online-master ', ' config-client ', ' online ', ' Master ') "); JdbcTemplate.Execute(INSERT into Properties VALUES (3, ' com.didispace.message ', ' test-online-develop ', ' config-client ', ' online ', ' Develop ') "); JdbcTemplate.Execute(INSERT into Properties VALUES (4, ' com.didispace.message ', ' hello-online-master ', ' hello-service ', ' online ', ' Master ') "); JdbcTemplate.Execute(INSERT into Properties VALUES (5, ' com.didispace.message ', ' hello-online-develop ', ' hello-service ', ' online ', ' Develop ') "); }}
Some test data are added here to facilitate subsequent configuration read validation.
The fourth step: Configuration application.properties
, the specific content is as follows:
spring.application.name=config-server-dbserver.port=10020spring.profiles.active=jdbcspring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?spring.datasource.url=jdbc:mysql://localhost:3306/config-server-dbspring.datasource.username=rootspring.datasource.password=spring.datasource.driver-class-name=com.mysql.jdbc.Driverflyway.locations=/schema
Here are some of the main configurations:
spring.profiles.active=jdbc
: The way to switch the storage implementation of the configuration center to JDBC must be set
spring.cloud.config.server.jdbc.sql
: Non-mandatory, here because of the use of MySQL data source, key
value
is reserved keywords, the original implementation statement will be error, so you need to rewrite this query statement (if the stored table structure design differs from the content prepared above, you can also use this property configuration to modify the configuration of the acquisition logic)
spring.datasource.*
: Data source configuration for storing configuration information, MySQL is used here, and developers modify it according to their actual situation.
flyway.locations
: Flyway loading schema to create SQL location
Server-side configuration verification
Having completed the previous section, we have built a configuration center that stores the configuration content via data cool, and we can try to read the configuration by configuring the endpoint exposed by the hub.
The first step: Start the configuration center built above first.
Step Two: Verify the configuration information gets:
curl http://localhost:10020/config-client/stage/
, get the config-client
stage
configuration content of the information service environment, according to the above data preparation, we will get the following return content:
{ "Name": "Config-client", "Profiles": [ "Stage" ], "Label": NULL, "Version": NULL, "State": NULL, "Propertysources": [ { "Name": "Config-client-stage", "Source": { "Com.didispace.message": "Test-stage-master" } } ]}
curl http://localhost:10020/hello-service/stage/develop
, obtain information hello-service
service, stage
Environment, develop
label configuration content, according to the above data preparation, we will get the following return content:
{ "Name": "Hello-service", "Profiles": [ "Online" ], "Label": "Develop", "Version": NULL, "State": NULL, "Propertysources": [ { "Name": "Hello-service-online", "Source": { "Com.didispace.message": "Hello-online-develop" } } ]}
Learn more about how to access the Spring Cloud Config build configuration center to get configuration information
, you can look at the previous article: "Spring Cloud Building MicroServices Architecture: Distributed Configuration Center", this article does not do a detailed introduction.
Summarize
In this paper, we mainly introduce the use of JDBC storage in Spring Cloud config in the Edgware version, and there are actually a lot of space that can be optimized, such as: Index optimization, query statement optimization, if you need further custom management, It is also necessary to optimize the table structure.
Finally, Amway is a Spring Cloud config-based configuration management project: Https://github.com/dyc87112/spring-cloud-config-admin, in the midst of intense development, Look forward to it!
Examples of this article
Readers can choose from the following two warehouses to view config-server-db
and two items according to their preferences config-client
:
- github:https://github.com/dyc87112/springcloud-learning/
- gitee:https://gitee.com/didispace/springcloud-learning/
If you are interested in these, welcome to star, follow, collection, forwarding to give support!
Spring Cloud config uses database storage configuration content