Distributed configuration management using spring cloud

  Create a configuration management server and implement a distributed configuration management application in "Learning Spring Cloud Series in 7 Days".

  Projects covered in this article:

   Distributed configuration management should be the first step in the application of distributed systems and microservices. 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, etc., the workload is quite large, and it is also prone to errors. If the configuration information of each application can be centrally managed and managed by a set of mechanisms or systems, the production efficiency of system development will be greatly improved, and the consistency of system development environment and production environment will also be improved.

  

  In traditional development, we often need to develop a "configuration management server" by ourselves. You can use redis, ldap, zookeeper, db, etc. to store unified configuration information, and then develop a management interface for management. There is nothing wrong with the traditional approach. Compared with the configuration management solution provided by spring cloud, the former needs to be developed by itself, while the latter can simply use off-the-shelf components. Of course, there is also a very important point. Since the spring configuration management module is implemented by the core of spring boot, a lot of work has been done, and some startup parameters can be configured externally , which is difficult to achieve in traditional solutions. Because it involves the problem of rewriting third-party components, it is very difficult. For example, the binding port of web applications, traditional applications can only be changed in the tomcat configuration file, but spring cloud can be placed remotely, similar to database connection, security framework configuration, etc.

  To use the spring cloud distributed configuration file is generally divided into three major steps. First, you need to create a warehouse to store the configuration file, and then create a configuration file server, which converts the configuration file information into rest interface data, and then creates a An application service that demonstrates the use of distributed profile information.

  1) Create a configuration file storage warehouse

  Spring cloud uses git or svn to store configuration files. By default, git is used, so you need to install git private server or directly use github or git.oschina on the Internet. Here, git.oschina is recommended. The example in this article uses git.oschina. After the git project is created, which is the project mentioned at the beginning of the article, a folder cloud-config-repo is created in this project to store the configuration files. 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 respectively. The configuration information is as follows:

  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

  The configuration information provides database connection parameters, etc., because the database is used in the subsequent application services.

  2) Create spring cloud configuration server

  After the configuration file warehouse is created, it is necessary to create a configuration management server. As mentioned above, the server only converts the configuration file into a rest interface service and does not use it for other purposes. The function of this server is also provided by spring cloud, so we only need to introduce the relevant jar package and set it up a little. To create the service application, you need to first create an empty maven project:

        

  然后在这个工程中增加一个类,命名为:ConfigServerApplication,代码如下:

  @SpringBootApplication

  @EnableConfigServer

  public class ConfigServerApplication {

           public static void main(String[] args) {

            SpringApplication.run(ConfigServerApplication.class, args);

           }

  }

  可以看到,我们只需要用@EnableConfigServer激活该应用为配置文件服务器即可。如此以来该应用启动后就会完成前面提到的功能,即:读取远程配置文件,转换为rest接口服务。

当然,需要配置远程配置文件读取路径,在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

  其中server.port是配置当前web应用绑定8888端口,git.uri指定配置文件所在的git工程路径,searchPaths表示将搜索该文件夹下的配置文件(我们的配置文件放在spring-cloud-7simple这个工程的cloud-config-repo文件夹下)。

  最后,还需要在pom文件中增加配置服务器的相关依赖:

  <dependency>

                     <groupId>org.springframework.cloud</groupId>

                     <artifactId>spring-cloud-config-server</artifactId>

  </dependency>

  如此以来,配置文件服务器就建立好了,可以直接启动了,服务端口是8888,应用只需要绑定改服务器的uri和端口号就可以拿到配置信息了。

  3)  创建一个服务使用该远程配置

  现在可以创建一个服务使用该远程配置了,你可以在远程配置中定义一个简单的自定义信息,比如:

  my.message=helloword

  然后使用前面我们提到的spring boot helloworld应用来读取这个信息。当然,限于篇幅我们直接使用比较复杂的一个服务来演示这个配置管理器的使用,这个服务需要用到数据库访问,数据库访问层我们使用的是mybaits,数据表只有一个,DDL如下:

  CREATE TABLE `user` (

       `id` varchar(50) NOT NULL DEFAULT '',

       `username` varchar(50) DEFAULT NULL,

       PRIMARY KEY (`id`)

  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  创建好数据表后,回到我们的应用服务:

 

  该服务使用DataSourceProperties封装了mybatis加载配置信息。要拿到远程配置信息,需要设置配置管理服务器地址,该配置设置在:

  bootstrap.properties

  该配置文件信息如下:

  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}

  其中config.uri指定远程加载配置信息的地址,就是前面我们刚建立的配置管理服务器的地址,绑定端口8888,其中config.port:8888,表示如果在命令行提供了config.port参数,我们就用这个端口,否则就用8888端口。config.name表示配置文件名称,查看我们前面创建配置文件,是这个名称:

  cloud-config-dev.properties

  可以分成两部分: {application}- {profile}.properties

  所以我们配置config.name为cloud-config,config.profile为dev,其中dev表示开发配置文件,配置文件仓库里还有一个测试环境的配置文件,切换该配置文件只需要将dev改为test即可,当然这个参数也可以由启动时命令行传入,如:

  java -jar cloud-simple-service-1.0.0.jar --config.profile =test

  此时应用就会加载测试环境下的配置信息。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326393815&siteId=291194637