一 代码位置
https://github.com/cakin24/spring-cloud-code/tree/master/ch12-3
二 数据库脚本
CREATE TABLE `PROPERTIES` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`KEY` TEXT DEFAULT NULL,
`VALUE` TEXT DEFAULT NULL,
`APPLICATION` TEXT DEFAULT NULL,
`PROFILE` TEXT DEFAULT NULL,
`LABLE` TEXT DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('3', 'cn.springcloud.book.config', 'I am the mysql configuration file from dev environment.', 'config-info', 'dev', 'master');
INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('4', 'cn.springcloud.book.config', 'I am the mysql configuration file from test environment.', 'config-info', 'test', 'master');
INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('5', 'cn.springcloud.book.config', 'I am the mysql configuration file from prod environment.', 'config-info', 'prod', 'master');
三 测试
1 启动config-server-db
2 启动config-client-db
3 浏览器输入 http://localhost:9096/configConsumer/getConfigInfo
匹配到的配置数据是
INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('3', 'cn.springcloud.book.config', 'I am the mysql configuration file from dev environment.', 'config-info', 'dev', 'master');
匹配到这条数据是由下面SQL语句的where条件所决定。
spring:
application:
name: ch12-3-config-server-db
cloud:
config:
server:
jdbc:
sql: SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE application =? AND profile =? AND lable =?