"7 Days of Learning Spring Cloud Series" To create a configuration Management Server and implementation of distributed configuration management applications.
The items covered in this article are:
- Open Source project: Http://git.oschina.net/zhou666/spring-cloud-7simple
- Cloud-config-repo: folder where the configuration file resides
- Cloud-simple-service: A database application that uses MyBatis
Distributed configuration management should be the first step in distributed systems and microservices applications. Imagine if you have dozens of services or applications that need to be configured, and each service is divided into different dimensions, such as development, testing, production, and so on, the workload is quite large and error-prone. If the configuration information of each application can be managed centrally, using a set of mechanisms or systems to manage, it will greatly improve the production efficiency of the system development, but also improve the system development environment and production environment running consistency.
In traditional development, we often need to develop "Configuration Management Server", you can use Redis, LDAP, zookeeper, DB, etc. to hold unified configuration information, and then develop a management interface for management. The traditional approach is fine, compared to the configuration management solution provided by spring Cloud, which needs to be developed on its own, which simply uses ready-made components. Of course, it is important to note that the Spring Configuration Management module is implemented by the spring boot core, so a lot of work is done to configure some of the startup parameters externally , which is difficult in traditional scenarios because it involves the problem of rewriting third-party components, It's a lot harder. For example, the Web application of the binding port, the traditional application can only be changed in the Tomcat configuration file, and spring cloud can be put to remote, similar to the database connection, security framework configuration and so on.
To use the spring cloud distributed profile is generally divided into 3 large steps, preferably you need to create a repository for the configuration file, and then create a profile server that transforms the profile information into rest interface data and then creates an app service. The service demonstrates the use of distributed configuration file information.
1) Create a configuration file to store the repository
Spring cloud uses git or SVN to store the configuration file and Git is used by default, so you'll need to install git or use GitHub or Git.oschina on the Internet, and it's recommended to use Git.oschina. This example uses Git.oschina to create a git project, which is the project mentioned at the beginning of the article, where you create a folder Cloud-config-repo to hold the configuration file. Then create two configuration files:
- Cloud-config-dev.properties
- Cloud-config-test.properties
These two files correspond to the configuration information required by the development environment and the test environment, with the following configuration information:
Mysqldb.datasource.url=jdbc\:mysql\://10.0.12.170\:3306/test?useunicode\=true&characterencoding\=utf-8
Mysqldb.datasource.username=csst
Mysqldb.datasource.password=csst
Logging.level.org.springframework.web:DEBUG
Configuration information provides database connection parameters, and so on, because the database is used in later application services.
2) Create a spring cloud configuration server
Once the profile warehouse has been created, you will need to create a configuration Management Server, as described earlier, which simply transforms the configuration file into a rest interface service and does nothing else. The functionality of this server is also provided by spring Cloud, so we just need to introduce the relevant jar package and set it up a little bit. To create the service app, you need to prefer to create an empty MAVEN project:
Then add a class to this project named: Configserverapplication, the code is as follows:
@SpringBootApplication
@EnableConfigServer
public class Configserverapplication {
public static void Main (string[] args) {
Springapplication.run (Configserverapplication.class, args);
}
}
As you can see, we only need to activate the app as a profile server with @enableconfigserver. This has been done since the app started, which reads the remote configuration file and translates it into the rest interface service.
Of course, you need to configure the remote profile read path in application.properties:
server.port=8888
Spring.cloud.config.server.git.uri=https://git.oschina.net/zhou666/spring-cloud-7simple.git
Spring.cloud.config.server.git.searchpaths=cloud-config-repo
Where Server.port is configuring the current web App binding 8888 port, Git.uri the GIT project path where the configuration file resides, searchpaths means that the configuration file under that folder will be searched (our configuration file is placed in the spring-cloud-7simple of this project Clou D-config-repo folder).
Finally, you need to increase the dependency on the configuration server in the Pom file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Since then, the configuration file server has been established, can be started directly, the service port is 8888, the application only need to bind to the server's URI and port number to get configuration information.
3) Create a service to use the remote configuration
Now that you can create a service that uses this remote configuration, you can define a simple custom message in the remote configuration, such as:
My.message=helloword
Then use the Spring boot HelloWorld app we mentioned earlier to read this information. Of course, confined to the space we directly use a more complex service to demonstrate the use of this configuration Manager, the service needs to use the database access, the database access layer we use mybaits, the data table only one, the DDL is as follows:
CREATE TABLE ' user ' (
' ID ' varchar (+) not NULL DEFAULT ' ',
' username ' varchar DEFAULT NULL,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8;
After creating the data table, go back to our app service:
The service uses Datasourceproperties to encapsulate the MyBatis load configuration information. To get the remote configuration information, you need to set the configuration Management Server address, which is set in:
Bootstrap.properties
The configuration file information is as follows:
spring.cloud.config.uri=http://127.0.0.1:${config.port:8888}
Spring.cloud.config.name=cloud-config
Spring.cloud.config.profile=${config.profile:dev}
Where Config.uri specifies the address of the remote load configuration information, which is the address of the configuration Management Server we just established, bound port 8888, where config.port : 8888, indicating that if the Config.port parameter is provided at the command line, we use this port, otherwise we will use port 8888. Config.name represents the profile name, view the configuration file that we created earlier, is this name:
Cloud-config-dev.properties
Can be divided into two parts: {application}-{profile}.properties
So we configured config.name for Cloud-config,config.profile as Dev, where Dev represents the development profile, and a configuration file in the configuration file repository that has a test environment, switching the configuration file only needs to change dev to test, and this parameter can also be started The command line is passed in, such as:
Java-jar Cloud-simple-service-1.0.0.jar--config.profile =test
The app loads the configuration information in the test environment at this point.
Using spring cloud for distributed configuration management