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
* <code>{application}-{profile}.properties</code>, 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: 8041
spring:
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<ConfigInfo> 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));
}
}
代碼組織截圖如下: