SpringCloud系列教程(8)-- Config数据库JDBC配置

###config的数据库配置
config支持多种数据配置,包含git、svn、vault、 jdbc, 这里我推荐使用JDBC的方式,因为之前的GIT SVN 对于服务比较少的系统,可能比较容易维护,如果服务比较多,没有一个后台管理系统来维护,就太复杂了,在我的水平上理想的架构是这样的:
描述

###config数据库配置

1.查看spring-cloud-config-server.jar中的源码,找到EnvironmentRepositoryConfiguration.java,这个是server的注入类
源码
源码

阅读源码,我们可以知道,config默认支持git模式,但是同时也提供了svn、vault、 jdbc,三种配置模式,那我们怎么激活呢?

spring.profiles.active=jdbc

同时,我们查看JdbcEnvironmentRepository.java
源码2

发现,是通过JdbcTemplate 来读取的的数据库,同时,JdbcEnvironmentRepository
也提供了默认的数据库查询SQL:

SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?

同时 sql提供了 set方法,说明SQL 是可以自定义的,好了,那我们就按照源码的样子来配置吧.

2.这里我们用mysql,新建数据库test,在test库下面新建表properties ,建表语句如下:

CREATE TABLE `properties` (
  `id` int(11) NOT NULL,
  `key` varchar(50) DEFAULT NULL,
  `value` varchar(500) DEFAULT NULL,
  `application` varchar(50) DEFAULT NULL,
  `profile` varchar(50) DEFAULT NULL,
  `lable` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

3.新建cloud-config-jdbc-server项目,引入依赖配置

<!-- mysql 依赖包 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.38</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

4.配置 application.properties,这里我们重写SQL,因为KEY为mysql关键字,在mysql需要使用反引号来表示其为字段,配置如下:

server.port=8082
spring.application.name=cloud-config-jdbc-server
#数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.cloud.config.label=master
spring.cloud.config.server.jdbc=true
spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
spring.profiles.active
=jdbc

5.给启动类加上@EnableConfigServer 注解

@EnableConfigServer
@SpringBootApplication
public class CloudConfigJdbcServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(CloudConfigJdbcServerApplication.class, args);
	}
}

6.我们在数据库中新增一条数据
数据

7,启动项目,访问http://127.0.0.1:8082/cloud-client/test/master
结果
结果访问到数据库的值。

8.当然后面也可以改造重写该类,比如,可以在数据库层再加一层缓存,这样性能就更高了。

###戳我下载示例代码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值