Spring boot2.0 springcloud Finchley搭建基于jdbc的Config配置中心

本文介绍如何使用 Spring Cloud Config 搭建基于 MySQL 的配置中心,包括服务端与客户端的具体实现步骤,适用于无法使用 Git 存储配置的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springcloud config默认支持git模式,根据项目需求有时不能使用git形式,于是搭建了基于jdbc mysql的配置中心。

总线配置:https://blog.youkuaiyun.com/qq_27289875/article/details/82597208

版本springBoot 2.0.4 ,spring cloud Finchley 

一、server端

1.引入pom文件

<dependency>
     <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> 
<dependency>
    <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency> 
<dependency>
     <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>  

2.在数据库里创建表

CREATE TABLE `properties` (
	`APPLICATION` VARCHAR(32) NOT NULL COLLATE 'utf8mb4_bin',
	`PROFILE` VARCHAR(32) NOT NULL COLLATE 'utf8mb4_bin',
	`LABEL` VARCHAR(32) NOT NULL COLLATE 'utf8mb4_bin',
	`KEY` VARCHAR(100) NOT NULL COLLATE 'utf8mb4_bin',
	`VALUE` LONGTEXT NULL COLLATE 'utf8mb4_bin',
	PRIMARY KEY (`APPLICATION`, `PROFILE`, `LABEL`, `KEY`)
)
COLLATE='utf8mb4_bin'
ENGINE=MyISAM

3.配置properties文件

  

server:
  port: 7900

spring:
  application:
    name: SERVICE-CONFIG
  profiles:
    active: jdbc   #jdbc方式读取配置信息
  cloud:
    config:
      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?characterEncoding=UTF-8
    username: root
    password: password
    
eureka:
  client:
     serviceUrl.defaultZone: http://localhost:1111/eureka/,http://localhost:1112/eureka/
 

在这里,属性配置的“:”和“值”之间必须要有空格。

sql中的key,value都是mysql的关键字,用 `  (英文输入法状态下ESC下面的键)引起来。

4.启动类上添加@EnableConfigServer注解。

至此服务端搭配完成。

二、client端配置

 1.引入pom文件

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency> 

2.配置文件:这里客户端的config配置必须在bootstrap.yml中配置. 参考官方文档:

A Spring Cloud application operates by creating a “bootstrap” context, which is a parent context for the main application. 
It is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. 
The two contexts share an Environment, which is the source of external properties for any Spring application. 
By default, bootstrap properties (not bootstrap.properties but properties that are loaded during the bootstrap phase) are added with high precedence, 
so they cannot be overridden by local configuration.

 

大意是说bootstrap.yml有更高的优先级被先加载并不会被本地配置文件覆盖,用于加载外部资源。可以用于加载配置参数作为本项目的启动参数,比如把端口号配置在数据库里,项目启动时从配置中心取端口号并发布相应的端口号上。

bootstrap.yml配置:

通过注册中心去找配置中心:

spring:
  application:
    name: client-name
  cloud: 
    config:
       discovery:
         enabled: true # 通过服务发现的方式去找配置中心
         service-id: SERVICE-CONFIG 
       name: a    # 对应配置中心文件的${application}部分
       profile: b # 对应配置中心文件的${profile}部分
       label: c   

也可以直接通过ip去找配置中心:

spring:
  application:
    name: client-name
  cloud:
    config:
      name: a
      profile: b
      label: c
      uri: http://localhost:7900

3.获取参数:

可以通过@Value("${key}")获取,也可以通过@Autowired一个environment获取配置参数,这个environment是springboot包下的。

至此客户端搭载完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值