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; ListconfigList = 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)); }}
代码组织截图如下: