Spring Cloud config中,使用数据库存储配置信息

主要内容

  1. 在springcloud config中,使用数据库存储配置信息。

    系统默认采用git的方式,此处我们介绍使用jdbc的方式存储配置信息

准备数据库

  1. 数据库我们使用mysql。
  2. 新建库 p-config-server
  3. 创建配置需要的表,并初始化一些配置信息

    CREATE TABLE `properties` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `application` varchar(64) COLLATE utf8_bin NOT NULL DEFAULT '', `profile` varchar(64) COLLATE utf8_bin NOT NULL DEFAULT '', `label` varchar(64) COLLATE utf8_bin NOT NULL DEFAULT '', `key` varchar(128) COLLATE utf8_bin NOT NULL DEFAULT '', `value` varchar(4096) COLLATE utf8_bin NOT NULL DEFAULT '', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;INSERT INTO `properties` VALUES ('1', 'p-config-client', 'dev', 'master', 'key-1', 'value-1');INSERT INTO `properties` VALUES ('2', 'p-config-client', 'dev', 'master', 'key-2', 'value-2');INSERT INTO `properties` VALUES ('3', 'p-config-client', 'dev1', 'master', 'key-1', 'value-3');INSERT INTO `properties` VALUES ('4', 'p-config-client', 'dev1', 'master', 'key-4', 'value-4');INSERT INTO `properties` VALUES ('5', 'p-config-client', 'dev1', 'master', 'key-5', 'value-5');

创建eureka注册中心

  1. 创建springcloud项目p-eureka

    groupId: com.msartifactId: p-eureka

  2. pom.xml内容

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ms</groupId> <artifactId>p-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>p-eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
  3. application.properties改为application.yml

    server: port: 7001spring: application: name: p-eurekaeureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:7001/eureka/
  4. PEurekaApplication内容

    package com.ms;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class PEurekaApplication { public static void main(String[] args) { SpringApplication.run(PEurekaApplication.class, args); }}

    注意:

    • @EnableEurekaServer:表示这是一个eureka注册中心
  5. 运行PEurekaApplication

  6. 访问http://localhost:7001/

    创建配置中心(config server)

  7. 创建springcloud项目p-config-server

    groupId: com.msartifactId: p-config-server

  8. pom.xml内容

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ms</groupId> <artifactId>p-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>p-config-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
  9. application.yml 内容

    server: port: 8080eureka: client: service-url: defaultZone: http://localhost:7001/eureka/spring: application: name: p-config-server cloud: config: enabled: true profile: jdbc server: jdbc: sql: SELECT `key`, `value` from PROPERTIES where application=? and profile=? and label=? datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/config-server?characterEncoding=UTF-8 username: root password: root123 type: com.zaxxer.hikari.HikariDataSource profiles: active: jdbc

    注意

    • 数据连接地址、用户名、密码,这些值根据自己的机器进行修改
  10. PConfigServerApplication内容

    package com.ms;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableConfigServer@EnableEurekaClientpublic class PConfigServerApplication { public static void main(String[] args) { SpringApplication.run(PConfigServerApplication.class, args); }}

    注意:

    • @EnableConfigServer:表示这是一个注册中心应用
    • @EnableEurekaClient:将当前服务注册到eureka中,可以被其他应用发现,然后使用
  11. 运行PConfigServerApplication

  12. 访问:http://localhost:8080/master/p-config-client-dev,dev1.yml 结果如下:

    key-1: value-3key-2: value-2key-4: value-4key-5: value-5

创建客户端

  1. 创建springcloud项目p-config-client

    groupId: com.msartifactId: p-config-client

  2. pom.xml内容

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ms</groupId> <artifactId>p-config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>p-config-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
  3. application.properties改为bootstrap.yml 内容

    注意:此处必须使用bootstrap名称

    server: port: 8081spring: application: name: p-config-client cloud: config: discovery: enabled: true service-id: p-config-server name: ${spring.application.name} profile: dev,dev1 label: master override-system-properties: falseeureka: client: service-url: defaultZone: http://localhost:7001/eureka/
    • config.name对应数据库properties表中的application字段

    • config.profile对应表中的profile字段,config.profile如果包含多个值,之间用英文逗号隔开

    • config.label对应表中的label字段,我们可以使用label来区分环境(dev【开发环境】、test【测试环境】、prod【线上环境】)

  4. PConfigClientApplication内容

    package com.ms;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class PConfigClientApplication { public static void main(String[] args) { SpringApplication.run(PConfigClientApplication.class, args); }}

    注意:

    • @EnableEurekaClient:当前应用会访问eureka注册中心,发现config service
  5. 创建DemoController

    package com.ms;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@Slf4jpublic class DemoController { @Autowired private Environment environment; @RequestMapping("/{key}") public String index(@PathVariable("key") String key) { return this.environment.getProperty(key); }}
  6. 运行PConfigClientApplication

  7. 访问 http://localhost:8081/key-1 结果如下:

    value-3

总结

  1. 使用数据库存储配置信息还是比较靠谱的,相对于git来说会更好一些
  2. 代码已上传至git,获取源码方式:微信公众号javacode2018,发送:sccj

相关文章