原文發布于:http://www.gufeng.tech/ 谷風的個人主頁
SpringCloud的ConfigServer默認是持久化使用的是git。git有它天然的優勢,比如多版本管理、分支管理、提交審核策略等等,但是如果相對其中存儲的數據做細粒度的權限控制,就力不從心了。當然,也可以改變使用方式以適應這種特點,但是今天我們要做的是將持久化從git遷移到MySQL上。
ConfigServer有個接口:org.springframework.cloud.config.server.environment.EnvironmentRepository,這個接口的實現類就是ConfigServer的用來查詢配置信息的,方法簽名如下:
Environment findOne(String application, String profile, String label);
我們可以實現這個接口,在方法實現中查詢MySQL,由此看來,我們已經成功一半了,另一半就是解決如何把數據存到MySQL中。我們還是先解決查詢的問題,我們實現該方法內容如下:
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類的內容:
@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的實現:
@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的注解方式,關于MyBatis的注解使用詳細內容請查閱相關文檔。
我們首先看下數據源的配置:
@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的配置信息(當然,也可以使用SpringJDBC來實現):
@Configuration @EnableTransactionManagement//支持事務
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中都有哪些內容呢?至少包括以下內容:application, profile, label, key, value,其它內容可以根據實際需要增減。
這里提供兩種思路供參考:
一:在ConfigPropertity對應的表里存儲當前使用的配置及歷史配置,通過版本(或者狀態位)加以區分,使用狀態位的好處是整體存儲數量會少一下,使用版本的好處是一下就能夠查到某個歷史版本的數據而不需要經過分析;
二:分兩張表,一掌存儲當前生效正在使用的配置信息,另一張表用來存儲歷史配置信息,每次有變化時都同時寫入兩張表,歷史表采用追加的方式,當前表采用更新的方式。
以上就是把ConfigServer的持久化存儲從git改到MySQL的一種做法。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。