博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
功能实现:spring cloud config配置中心自定义存储方式
阅读量:6857 次
发布时间:2019-06-26

本文共 3978 字,大约阅读时间需要 13 分钟。

  hot3.png

Spring Cloud Config配置中心可以使用git,svn以及数据库方式实现配置存储等等,分别在配置文件中对应spring.profiles.active定义入口实现EnvironmentRepository接口。

比方说spring.cloud.config.server=jdbc的时候,通过JdbcEnvironmentRepository实现接口,spring.cloud.config.server=svn,通过SvnKitEnvironmentRepository实现接口。具体可以参考这两个实现。

JdbcEnvironmentRepository部分代码

/** * Subversion-backed {@link EnvironmentRepository}. * * @author Michael Prankl * @author Roy Clarkson */@ConfigurationProperties("spring.cloud.config.server.svn")public class SvnKitEnvironmentRepository extends AbstractScmEnvironmentRepository		implements EnvironmentRepository, InitializingBean {	private static Log logger = LogFactory.getLog(SvnKitEnvironmentRepository.class);	private static final String DEFAULT_LABEL = "trunk";

SvnKitEnvironmentRepository部分代码

/** * An {@link EnvironmentRepository} that picks up data from a relational database. The * database should have a table called "PROPERTIES" with columns "APPLICATION", "PROFILE", * "LABEL" (with the usual {@link Environment} meaning), plus "KEY" and "VALUE" for the * key and value pairs in {@link Properties} style. Property values behave in the same way * as they would if they came from Spring Boot properties files named * {application}-{profile}.properties, including all the encryption and * decryption, which will be applied as post-processing steps (i.e. not in this repository * directly). *  * @author Dave Syer * */@ConfigurationProperties("spring.cloud.config.server.jdbc")public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered {	private static final String DEFAULT_SQL = "SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?";	private int order = Ordered.LOWEST_PRECEDENCE - 10;	private final JdbcTemplate jdbc;	private String sql = DEFAULT_SQL;	private final PropertiesResultSetExtractor extractor = new PropertiesResultSetExtractor();

从上面可以清楚看到,如何实现自定义的实现类了

比如除了前文提及到框架实现的jdbc方式,我想通过mybatis-jdbc实现存储提取方式(甚至通过Redis缓存等)。自定义spring.profiles.active=mybatis。

application.yml

server:  port: 8041spring:  application:    name: config-server-mysql    # 项目名称尽量用小写  profiles:     active: mybatis###################################################################################################### mysql 属性配置  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://10.79.158.43:3306/frdm    username: frdm_app    password: frdm_app*1########################################################################################################################################################################################################### mybatis mapper xml 配置mybatis:  # mybatis.type-aliases-package:指定domain类的基包,即指定其在*Mapper.xml文件中可以使用简名来代替全类名  type-aliases-package:  mapper-locations: classpath:mybatis/mapper/*.xml  config-location: classpath:mybatis/mybatis-config.xml

实现类代码,需要实现findone函数,里面具体实现这里只是个参考,可根据情况自行实现。通过application、profile和label三个参数将key和value放到map变量中,再将map放到environment变量中,return environment即可。

@profile作为入口,本文通过mybatis进入,也可以通过其他自定义,例如redis进入

@Configuration@Profile("mybatis")public class ConfigEnvironmentRepository implements EnvironmentRepository {	@Resource	private IConfigService service;		@Override	public Environment findOne(String application, String profile, String label) {		if (StringUtils.isEmpty(application) || StringUtils.isEmpty(profile)) return null;		List
configList = service.find(application, profile, label); if(configList != null && configList.size()>0){ Environment environment = new Environment(application, StringUtils.commaDelimitedListToStringArray(profile),label,"version", "state"); Map map = new HashMap<>(); for(ConfigInfo configInfo:configList){ map.put(configInfo.getKey(), configInfo.getValue()); } environment.add(new PropertySource(application + "_" + profile + "_" + label, map)); return environment; } return new Environment(application,StringUtils.commaDelimitedListToStringArray(profile)); }}

代码组织截图如下:

151810_mFLj_3750151.png

 

 

转载于:https://my.oschina.net/gore/blog/1609658

你可能感兴趣的文章
Office WPS如何在页眉页脚添加一条横线
查看>>
站在 Android 开发的角度,聊聊 Airbnb 的 Lottie!!!
查看>>
数组去重Demo引出的思考
查看>>
javascript怎么禁用浏览器后退按钮
查看>>
AtomicLong可以被原子地读取和写入的底层long值的操作
查看>>
Android studio 将 Module 打包成 Jar 包
查看>>
coffee script
查看>>
正则表达式大全
查看>>
SVN switch 用法详解
查看>>
Javascript文件下载顺序问题
查看>>
程序员第一定律:关于技能与收入
查看>>
网络通讯合并数据发送的重要性和实现原理
查看>>
Jquery getJSON 实现跨域请求 --- callback
查看>>
<转载>构造函数与拷贝构造函数
查看>>
[转]K近邻算法
查看>>
表单元素01
查看>>
React Native项目Xcode打包发布iOS问题
查看>>
JPress v1.0-rc2 发布,新增微信小程序 SDK
查看>>
Confluence 6 为搜索引擎隐藏外部链接
查看>>
Python Mysql 数据库操作
查看>>