12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
mysql数据库 时间:2024-12-25 09:58:14
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
原文发布于:http://www.gufeng.tech/ 谷风的个人主页1.背景 SpringCloud的ConfigServer默认是持久化使用的是git。git有它天然的优势,比如多版本管理、分支管理、提交审核策略
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
原文发布于:http://www.gufeng.tech/ 谷风的个人主页
1.背景SpringCloud的ConfigServer默认是持久化使用的是git。git有它天然的优势,比如多版本管理、分支管理、提交审核策略等等,但是如果相对其中存储的数据做细粒度的权限控制,就力不从心了。当然,也可以改变使用方式以适应这种特点,但是今天我们要做的是将持久化从git迁移到MySQL上。
2.查询配置信息ConfigServer有个接口:org.springframework.cloud.config.server.environment.EnvironmentRepository,这个接口的实现类就是ConfigServer的用来查询配置信息的,方法签名如下:
Environment findOne(String application, String profile, String label);
我们可以实现这个接口,在方法实现中查询MySQL,由此看来,我们已经成功一半了,另一半就是解决如何把数据存到MySQL中。我们还是先解决查询的问题,我们实现该方法内容如下:
publicclassDatabasesEnvironmentRepositoryimplementsEnvironmentRepository{@AutowiredprivateConfigServiceconfigService;@OverridepublicEnvironmentfindOne(Stringapplication,Stringprofile,Stringlabel){if(StringUtils.isEmpty(application)||StringUtils.isEmpty(profile))returnnull;ConfigItemconfigItem=configService.findConfig(application,profile,label);if(configItem!=null){Environmentenvironment=newEnvironment(application,StringUtils.commaDelimitedListToStringArray(profile),label,configItem.getVersion());Mapmap=newHashMap<>();for(ConfigPropertyconfigProperty:configItem.getConfigProperties()){map.put(configProperty.getKey(),configProperty.getValue());}environment.add(newPropertySource(application+"_"+profile+"_"+label,map));returnenvironment;}returnnewEnvironment(application,StringUtils.commaDelimitedListToStringArray(profile));}}
接下来我们看一下ConfigService类的内容:
@ServicepublicclassConfigService{@AutowiredprivateConfigDAOconfigDAO;publicConfigItemfindConfig(Stringapplication,Stringprofile,Stringlabel){ConfigItemconfigItem=configDAO.findConfig(application,profile,label);if(null==configItem){returnnull;}ListconfigProperties=configDAO.findConfigProperties(configItem.getId());configItem.setConfigProperties(configProperties);returnconfigItem;}}
最后我们看一下ConfigDAO的实现:
@MapperpublicinterfaceConfigDAO{@Select("select*fromconfig_itemwhereapplication=#{application}andprofile=#{profile}andlabel=#{label}")ListfindConfigProperties(@Param("application")Stringapplication,@Param("profile")Stringprofile,@Param("label")Stringlabel);}
这里我们使用的是MyBatis的注解方式,关于MyBatis的注解使用详细内容请查阅相关文档。
3.数据库相关功能我们首先看下数据源的配置:
@ConfigurationpublicclassDataSourceConfiguration{@Value("${jdbc.driver}")privateStringdriver;@Value("${jdbc.url}")privateStringurl;@Value("${jdbc.username}")privateStringusername;@Value("${jdbc.password}")privateStringpassword;@Value("${jdbc.maxActive}")privateintmaxActive;@Value("${jdbc.maxIdel}")privateintmaxIdel;@Value("${jdbc.maxWait}")privatelongmaxWait;@BeanpublicBasicDataSourcedataSource(){BasicDataSourcedataSource=newBasicDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);dataSource.setMaxTotal(maxActive);dataSource.setMaxIdle(maxIdel);dataSource.setMaxWaitMillis(maxWait);dataSource.setValidationQuery("SELECT1");dataSource.setTestOnBorrow(true);returndataSource;}}
接下来看一下MyBatis的配置信息(当然,也可以使用SpringJDBC来实现):
@Configuration@EnableTransactionManagement//支持事务
publicclassMyBatisConfigimplementsTransactionManagementConfigurer{@AutowiredprivateDataSourcedataSource;@OverridepublicPlatformTransactionManagerannotationDrivenTransactionManager(){returnnewDataSourceTransactionManager(dataSource);}@Bean(name="sqlSessionFactory")publicSqlSessionFactorysqlSessionFactoryBean(){SqlSessionFactoryBeanbean=newSqlSessionFactoryBean();bean.setDataSource(dataSource);try{returnbean.getObject();}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}@BeanpublicSqlSessionTemplatesqlSessionTemplate(SqlSessionFactorysqlSessionFactory){returnnewSqlSessionTemplate(sqlSessionFactory);}}
最后看一下ConfigProperty中都有哪些内容呢?至少包括以下内容:application, profile, label, key, value,其它内容可以根据实际需要增减。
这里提供两种思路供参考:
一:在ConfigPropertity对应的表里存储当前使用的配置及历史配置,通过版本(或者状态位)加以区分,使用状态位的好处是整体存储数量会少一下,使用版本的好处是一下就能够查到某个历史版本的数据而不需要经过分析;
二:分两张表,一掌存储当前生效正在使用的配置信息,另一张表用来存储历史配置信息,每次有变化时都同时写入两张表,历史表采用追加的方式,当前表采用更新的方式。
以上就是把ConfigServer的持久化存储从git改到MySQL的一种做法。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19