spring cloud config使用mysql存储配置文件
1.结构图
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版本不行。