javaconfig配置mysql_spring cloud config使用mysql存储配置文件

本文介绍了如何在Spring Cloud Config中使用MySQL作为配置文件的存储后端。通过配置pom.xml、application.properties以及数据库相关脚本来展示具体实现步骤,并强调必须使用Edgware版本,因为Dalston版本不支持此功能。
摘要由CSDN通过智能技术生成

spring cloud config使用mysql存储配置文件

1.结构图

4172d79b8df01c88afc210ca1d63ec91.png

2.pom.xml:

4.0.0

com.didispace

config-server-db

1.0.0

jar

config-server-db

Spring Cloud In Action

org.springframework.boot

spring-boot-starter-parent

1.5.11.RELEASE

UTF-8

1.8

org.springframework.cloud

spring-cloud-config-server

org.springframework.boot

spring-boot-starter-jdbc

org.flywaydb

flyway-core

5.0.3

mysql

mysql-connector-java

5.1.21

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

Edgware.SR3

pom

import

org.springframework.boot

spring-boot-maven-plugin

3.application.properties:

spring.application.name=config-server-db

server.port=10020spring.profiles.active=jdbc

spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?spring.datasource.url=jdbc:mysql://localhost:3306/config-server-db

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

flyway.locations=/schema

4.V1_Base_version.sql:

CREATE TABLE `properties` (

`id`int(11) NOT NULL,

`key` varchar(50) NOT NULL,

`value` varchar(500) NOT NULL,

`application` varchar(50) NOT NULL,

`profile` varchar(50) NOT NULL,

`label` varchar(50) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

5.启动项:

@EnableConfigServer

@SpringBootApplicationpublic classConfigServerBootstrap {public static voidmain(String[] args) {

ApplicationContext context= SpringApplication.run(ConfigServerBootstrap.class);

JdbcTemplate jdbcTemplate= context.getBean(JdbcTemplate.class);

jdbcTemplate.execute("delete from properties");

jdbcTemplate.execute("INSERT INTO properties VALUES(1, 'com.didispace.message', 'test-stage-master', 'config-client', 'stage', 'master')");

jdbcTemplate.execute("INSERT INTO properties VALUES(2, 'com.didispace.message', 'test-online-master', 'config-client', 'online', 'master')");

jdbcTemplate.execute("INSERT INTO properties VALUES(3, 'com.didispace.message', 'test-online-develop', 'config-client', 'online', 'develop')");

jdbcTemplate.execute("INSERT INTO properties VALUES(4, 'com.didispace.message', 'hello-online-master', 'hello-service', 'online', 'master')");

jdbcTemplate.execute("INSERT INTO properties VALUES(5, 'com.didispace.message', 'hello-online-develop', 'hello-service', 'online', 'develop')");

}

}

只需要建立一个

config-server-db数据库就行,表不用自己创建,

flyway会自动帮你创建好数据库表。直接运行项目就行,访问方式和使用git获取配置文件的方式一样,返回的数据类型也是一样的。

值得注意的是配置中心想要用mysql存储配置文件,必须是

Edgware版本才行,

Dalston版本不行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值