Spring Cloud Config:基于JDBC搭建配置中心

为什么使用配置中心

每个微服务都有各种环境的配置,例如开发、测试、预生产、生产环境,通过配置中心可以集中进行管理与维护,同时还可以保证配置信息的安全性。

配置中心的特点

Spring Cloud Config分为服务端与客户端两部分:

  • 服务端:也称为分布式配置中心,是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息的接口。
  • 客户端:是微服务架构中各微服务应用,启动时先从配置中心加载配置信息。
    在这里插入图片描述
config支持的存储方式
  • Git
  • FileSystem
  • Vault
  • JDBC
  • Redis
  • Aws S3
  • CredHub

本文搭建基于数据库存储的config配置中心

1、config server 服务端搭建

Application启动类增加注解 @EnableConfigServer

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerDemoApplication.class, args);
    }
}

pom引入maven依赖

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.3.0.RELEASE</version>
   <relativePath/>
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>

<dependencies>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>

     <!--配置中心server依赖-->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
     </dependency>

     <!--数据库驱动-->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.22</version>
     </dependency>

     <!--spring jdbc-->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
         <version>2.4.0</version>
     </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>

application.yml配置文件

spring:
  application:
    name: config-server-demo
  profiles:
    active: jdbc
  datasource:
    url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  cloud:
    config:
      server:
        jdbc:
          sql: SELECT `KEY`,`VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
      label: master
server:
  port: 7070
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8090/eureka/
  instance:
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

配置服务端用到的properties表的sql创建语句

CREATE TABLE `properties` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `key` varchar(50) DEFAULT NULL,
  `value` varchar(500) DEFAULT NULL,
  `application` varchar(50) DEFAULT NULL,
  `profile` varchar(50) DEFAULT NULL,
  `label` varchar(50) DEFAULT NULL,
  `remark` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

启动项目如下图所示,jdbc表示配置中心数据源是数据库
在这里插入图片描述
以http访问配置资源的几种方式

# 访问配置资源
http://localhost:7070/config-client-demo/prod
# 以properties方式访问配置资源
http://localhost:7070/config-client-demo-prod.properties
# 以YAML方式访问配置资源
http://localhost:7070/config-client-demo-prod.yml

# 可以用以下几种方式访问资源
# {application}为spring.application.name设置的值
# {profiel}为spring.profile.active设置的值
# {label}为版本标签(通常指在有版本控制的情况下)
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
2、 config client 客户端搭建

pom引入maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>

<dependencies>
   <!--web依赖-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <!--config 客户端依赖-->
   <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>
</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>

bootstrap.yml 配置文件

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8090/eureka/
  instance:
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
server:
  port: 5050
spring:
  application:
    name: config-client-demo
  cloud:
    config:
      discovery:
        # 开启配置中心
        enabled: true
        # 配置服务的名称
        service-id: config-server-demo
  profiles:
    active: dev

新建一个controller控制器用于访问表中的配置属性

@RestController
public class ConfigClientController {

    @Value("${test.userName}")
    private String testUserName;

    @GetMapping("getTestUserName")
    public String getTestUserName(){
        return testUserName;
    }
}

启动项目出现英文提示 Fetching config from server表示从配置服务拉取配置信息
在这里插入图片描述
通过浏览器可以正常访问获取用户名配置属性内容如下图:
在这里插入图片描述
至此,基于JDBC搭建配置中心完成


版权声明:本文为weixin_43845422原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。