Spring Cloud组件Config Server 使用数据库作为配置库

Spring Cloud的配置服务器config server一般使用git,svn版本管理系统或者本地文件系统作为配置库。Edgware版本新增数据库作为配置库,这边总结了项目实施的具体过程与遇到的一些问题。

  • 项目结构如下 - parent -- zuul -- eureka -- config server -- service-a -- service-b
  • pom依赖 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java

org.springframework.boot spring-boot-starter-actuator

  • 数据库新增配置数据表 properties,使用配置项+配置值+应用名称+配置环境+分支作为联合主键: DROP TABLE IF EXISTS `properties`; CREATE TABLE `properties` ( `akey` varchar(128) NOT NULL, `avalue` varchar(512) NOT NULL, `application` varchar(64) NOT NULL COMMENT '应用名称', `profile` varchar(64) NOT NULL COMMENT '配置环境', `label` varchar(64) NOT NULL COMMENT '分支', PRIMARY KEY (`akey`,`avalue`,`application`,`profile`,`label`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • config server模块application.properties文件 spring.application.name=config server.port={port} # management.security.enabled=false spring.profiles.active=jdbc spring.cloud.config.server.jdbc.sql=select `aKey`, `avalue` from config_properties where\(application=? or application = 'common') and profile=? and label = ? # datasource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://{ip}:{port}/{db} spring.datasource.username:{username} spring.datasource.password:{password} # eureka.client.serviceUrl.defaultZone={eureka uri}
  • service-a 模块bootstrap.properties文件 spring.application.name=service-a server.port={port} spring.cloud.config.uri={config server uri} spring.cloud.config.profile={profile} spring.cloud.config.label={label} management.security.enabled=false
  • 配置加载说明 select `aKey`, `avalue` from config_properties where (application=? or \ application = 'common') and profile=? and label = ? 项目数据库配置表中application=common的为公共配置,存放数据源,redis连接等公共配置,所以配置加载sql语句定义为取common和微服务模块自定义配置的合集; 微服务模块要覆盖某个配置,比如common中有ftp.path=a的配置,目前测试在service-a中配置ftp.path=b,重新加载后可以生效;没有查看实现原理,建议微服务模块不要与公共配置有重复配置项。
  • 常用操作 重载配置,需配合Bean类上的@RefreshScope注解使用,原理:销毁带@RefreshScope注解的Bean然后重新使用该Bean时再创建,根据原理需要注意加@RefreshScope注解的位置; http://{service-a ip}:{service-a port}/refresh 查看当前配置: http://{service-a ip}:{service-a port}/env

转载于:https://my.oschina.net/diagram/blog/1852677

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值