您好,登錄后才能下訂單哦!
原文發(fā)布于:http://www.gufeng.tech/ 谷風(fēng)的個人主頁
SpringCloud的ConfigServer默認(rèn)是持久化使用的是git。git有它天然的優(yōu)勢,比如多版本管理、分支管理、提交審核策略等等,但是如果相對其中存儲的數(shù)據(jù)做細(xì)粒度的權(quán)限控制,就力不從心了。當(dāng)然,也可以改變使用方式以適應(yīng)這種特點,但是今天我們要做的是將持久化從git遷移到MySQL上。
ConfigServer有個接口:org.springframework.cloud.config.server.environment.EnvironmentRepository,這個接口的實現(xiàn)類就是ConfigServer的用來查詢配置信息的,方法簽名如下:
Environment findOne(String application, String profile, String label);
我們可以實現(xiàn)這個接口,在方法實現(xiàn)中查詢MySQL,由此看來,我們已經(jīng)成功一半了,另一半就是解決如何把數(shù)據(jù)存到MySQL中。我們還是先解決查詢的問題,我們實現(xiàn)該方法內(nèi)容如下:
public class DatabasesEnvironmentRepository implements EnvironmentRepository { @Autowired private ConfigService configService; @Override public Environment findOne(String application, String profile, String label) { if (StringUtils.isEmpty(application) || StringUtils.isEmpty(profile)) return null; ConfigItem configItem = configService.findConfig(application, profile, label); if (configItem != null) { Environment environment = new Environment(application, StringUtils.commaDelimitedListToStringArray(profile), label, configItem.getVersion()); Map map = new HashMap<>(); for (ConfigProperty configProperty : configItem.getConfigProperties()) { map.put(configProperty.getKey(), configProperty.getValue()); } environment.add(new PropertySource(application + "_" + profile + "_" + label, map)); return environment; } return new Environment(application, StringUtils.commaDelimitedListToStringArray(profile)); } }
接下來我們看一下ConfigService類的內(nèi)容:
@Servicepublic class ConfigService { @Autowired private ConfigDAO configDAO; public ConfigItem findConfig(String application, String profile, String label) { ConfigItem configItem = configDAO.findConfig(application, profile, label); if (null == configItem) { return null; } List configProperties = configDAO.findConfigProperties(configItem.getId()); configItem.setConfigProperties(configProperties); return configItem; } }
最后我們看一下ConfigDAO的實現(xiàn):
@Mapperpublic interface ConfigDAO { @Select("select * from config_item where application = #{application} and profile = #{profile} and label = #{label}") List findConfigProperties(@Param("application") String application, @Param("profile") String profile, @Param("label") String label); }
這里我們使用的是MyBatis的注解方式,關(guān)于MyBatis的注解使用詳細(xì)內(nèi)容請查閱相關(guān)文檔。
我們首先看下數(shù)據(jù)源的配置:
@Configurationpublic class DataSourceConfiguration { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Value("${jdbc.maxActive}") private int maxActive; @Value("${jdbc.maxIdel}") private int maxIdel; @Value("${jdbc.maxWait}") private long maxWait; @Bean public BasicDataSource dataSource(){ BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setMaxTotal(maxActive); dataSource.setMaxIdle(maxIdel); dataSource.setMaxWaitMillis(maxWait); dataSource.setValidationQuery("SELECT 1"); dataSource.setTestOnBorrow(true); return dataSource; } }
接下來看一下MyBatis的配置信息(當(dāng)然,也可以使用SpringJDBC來實現(xiàn)):
@Configuration @EnableTransactionManagement//支持事務(wù)
public class MyBatisConfig implements TransactionManagementConfigurer { @Autowired private DataSource dataSource; @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); try { return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
最后看一下ConfigProperty中都有哪些內(nèi)容呢?至少包括以下內(nèi)容:application, profile, label, key, value,其它內(nèi)容可以根據(jù)實際需要增減。
這里提供兩種思路供參考:
一:在ConfigPropertity對應(yīng)的表里存儲當(dāng)前使用的配置及歷史配置,通過版本(或者狀態(tài)位)加以區(qū)分,使用狀態(tài)位的好處是整體存儲數(shù)量會少一下,使用版本的好處是一下就能夠查到某個歷史版本的數(shù)據(jù)而不需要經(jīng)過分析;
二:分兩張表,一掌存儲當(dāng)前生效正在使用的配置信息,另一張表用來存儲歷史配置信息,每次有變化時都同時寫入兩張表,歷史表采用追加的方式,當(dāng)前表采用更新的方式。
以上就是把ConfigServer的持久化存儲從git改到MySQL的一種做法。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。