设为首页 收藏本站
查看: 833|回复: 0

[经验分享] 将SpringCloud ConfigServer持久化存储改为MySQL

[复制链接]

尚未签到

发表于 2018-10-9 10:35:41 | 显示全部楼层 |阅读模式
  原文发布于: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中。我们还是先解决查询的问题,我们实现该方法内容如下:
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的注解使用详细内容请查阅相关文档。
3.数据库相关功能
        我们首先看下数据源的配置:
@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的一种做法。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-619483-1-1.html 上篇帖子: mysql的主从复制以及读写分离 下篇帖子: MySQL常用字符函数简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表