分布式apollo简介
Apollo(阿波罗)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。
本文主要介绍如何使用apollo与springboot实现动态刷新配置,如果之前不了解apollo可以查看如下文档
https://github.com/ctripcorp/apollo
学习了解一下apollo,再来查看本文
正文
apollo与spring实现动态刷新配置本文主要演示2种刷新,一种基于普通字段刷新、一种基于bean上使用了@ConfigurationProperties刷新
1、普通字段刷新
a、pom.xml配置
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.6.0</version> </dependency>
b、客户端配置AppId,Apollo Meta Server
此配置有多种方法,本示例直接在application.yml配置,配置内容如下
app:
id: ${spring.application.name}
apollo:
meta: http://192.168.88.128:8080,http://192.168.88.129:8080
bootstrap:
enabled: true
eagerLoad:
enabled: true
c、项目中启动类上加上@EnableApolloConfig注解,形如下
@SpringBootApplication @EnableApolloConfig(value = {"application","user.properties","product.properties","order.properties"}) public class ApolloApplication { public static void main(String[] args) { SpringApplication.run(ApolloApplication.class, args); } }
@EnableApolloConfig不一定要加在启动类上,加在被spring管理的类上即可
d、在需刷新的字段上配置@Value注解,形如
@Value("${hello}") private String hello;
通过以上三步就可以实现普通字段的动态刷新
2.bean使用@ConfigurationProperties动态刷新
bean使用@ConfigurationProperties注解目前还不支持自动刷新,得编写一定的代码实现刷新。目前官方提供2种刷新方案
a、基于RefreshScope实现刷新
1、pom.xml要额外引入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> <version>2.0.3.RELEASE</version> </dependency>
2、bean上使用@RefreshScope注解
@Component @ConfigurationProperties(prefix = "product") @Data @AllArgsConstructor @NoArgsConstructor @Builder @RefreshScope public class Product { private Long id; private String productName; private BigDecimal price; }
3、利用RefreshScope搭配@ApolloConfigChangeListener监听实现bean的动态刷新,其代码实现如下
@ApolloConfigChangeListener(value="product.properties",interestedKeyPrefixes = {"product."}) private void refresh(ConfigChangeEvent changeEvent){ refreshScope.refresh("product"); PrintChangeKeyUtils.printChange(changeEvent); }
b、基于EnvironmentChangeEvent实现刷新
利用spring的事件驱动配合@ApolloConfigChangeListener监听实现bean的动态刷新,其代码如下
@Component @Slf4j public class UserPropertiesRefresh implements ApplicationContextAware { private ApplicationContext applicationContext; @ApolloConfigChangeListener(value="user.properties",interestedKeyPrefixes = {"user."}) private void refresh(ConfigChangeEvent changeEvent){ applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); PrintChangeKeyUtils.printChange(changeEvent); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
c、当bean上有@ConditionalOnProperty如何实现刷新
当bean上有@ConditionalOnProperty注解时,上述的两种方案可以说失效了,因为@ConditionalOnProperty是一个条件注解,当不满足条件注解时,bean是没法注册到spring容器中的。如果我们要实现此种情况的下的动态刷新,我们就得自己手动注册或者销毁bean了。其实现流程如下
1、当满足条件注解时,则手动创建bean,然后配合@ApolloConfigChangeListener监听该bean的属性变化。当该bean属性有变化时,手动把属性注入bean。同时刷新依赖该bean的其他bean
2、当不满足条件注解时,则手动从spring容器中移除bean,同时刷新依赖该bean的其他bean
其刷新核心代码如下
public class OrderPropertiesRefresh implements ApplicationContextAware { private ApplicationContext applicationContext; @ApolloConfig(value = "order.properties") private Config config; @ApolloConfigChangeListener(value="order.properties",interestedKeyPrefixes = {"order."},interestedKeys = {"model.isShowOrder"}) private void refresh(ConfigChangeEvent changeEvent){ for (String basePackage : listBasePackages()) { Set<Class> conditionalClasses = ClassScannerUtils.scan(basePackage, ConditionalOnProperty.class); if(!CollectionUtils.isEmpty(conditionalClasses)){ for (Class conditionalClass : conditionalClasses) { ConditionalOnProperty conditionalOnProperty = (ConditionalOnProperty) conditionalClass.getAnnotation(ConditionalOnProperty.class); String[] conditionalOnPropertyKeys = conditionalOnProperty.name(); String beanChangeCondition = this.getChangeKey(changeEvent,conditionalOnPropertyKeys); String conditionalOnPropertyValue = conditionalOnProperty.havingValue(); boolean isChangeBean = this.changeBean(conditionalClass, beanChangeCondition, conditionalOnPropertyValue); if(!isChangeBean){ // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); } } } } PrintChangeKeyUtils.printChange(changeEvent); printAllBeans(); } /** * 根据条件对bean进行注册或者移除 * @param conditionalClass * @param beanChangeCondition bean发生改变的条件 * @param conditionalOnPropertyValue */ private boolean changeBean(Class conditionalClass, String beanChangeCondition, String conditionalOnPropertyValue) { boolean isNeedRegisterBeanIfKeyChange = this.isNeedRegisterBeanIfKeyChange(beanChangeCondition,conditionalOnPropertyValue); boolean isNeedRemoveBeanIfKeyChange = this.isNeedRemoveBeanIfKeyChange(beanChangeCondition,conditionalOnPropertyValue); String beanName = StringUtils.uncapitalize(conditionalClass.getSimpleName()); if(isNeedRegisterBeanIfKeyChange){ boolean isAlreadyRegisterBean = this.isExistBean(beanName); if(!isAlreadyRegisterBean){ this.registerBean(beanName,conditionalClass); return true; } }else if(isNeedRemoveBeanIfKeyChange){ this.unregisterBean(beanName); return true; } return false; } /** * bean注册 * @param beanName * @param beanClass */ public void registerBean(String beanName,Class beanClass) { log.info("registerBean->beanName:{},beanClass:{}",beanName,beanClass); BeanDefinitionBuilder beanDefinitionBurinilder = BeanDefinitionBuilder.genericBeanDefinition(beanClass); BeanDefinition beanDefinition = beanDefinitionBurinilder.getBeanDefinition(); setBeanField(beanClass, beanDefinition); getBeanDefinitionRegistry().registerBeanDefinition(beanName,beanDefinition); } /** * 设置bean字段值 * @param beanClass * @param beanDefinition */ private void setBeanField(Class beanClass, BeanDefinition beanDefinition) { ConfigurationProperties configurationProperties = (ConfigurationProperties) beanClass.getAnnotation(ConfigurationProperties.class); if(ObjectUtils.isNotEmpty(configurationProperties)){ String prefix = configurationProperties.prefix(); for (String propertyName : config.getPropertyNames()) { String fieldPrefix = prefix + "."; if(propertyName.startsWith(fieldPrefix)){ String fieldName = propertyName.substring(fieldPrefix.length()); String fieldVal = config.getProperty(propertyName,null); log.info("setBeanField-->fieldName:{},fieldVal:{}",fieldName,fieldVal); beanDefinition.getPropertyValues().add(fieldName,fieldVal); } } } } /** * bean移除 * @param beanName */ public void unregisterBean(String beanName){ log.info("unregisterBean->beanName:{}",beanName); getBeanDefinitionRegistry().removeBeanDefinition(beanName); } public <T> T getBean(String name) { return (T) applicationContext.getBean(name); } public <T> T getBean(Class<T> clz) { return (T) applicationContext.getBean(clz); } public boolean isExistBean(String beanName){ return applicationContext.containsBean(beanName); } public boolean isExistBean(Class clz){ try { Object bean = applicationContext.getBean(clz); return true; } catch (BeansException e) { // log.error(e.getMessage(),e); } return false; } private boolean isNeedRegisterBeanIfKeyChange(String changeKey,String conditionalOnPropertyValue){ if(StringUtils.isEmpty(changeKey)){ return false; } String apolloConfigValue = config.getProperty(changeKey,null); return conditionalOnPropertyValue.equals(apolloConfigValue); } private boolean isNeedRemoveBeanIfKeyChange(String changeKey,String conditionalOnPropertyValue){ if(!StringUtils.isEmpty(changeKey)){ String apolloConfigValue = config.getProperty(changeKey,null); return !conditionalOnPropertyValue.equals(apolloConfigValue); } return false; } private boolean isChangeKey(ConfigChangeEvent changeEvent,String conditionalOnPropertyKey){ Set<String> changeKeys = changeEvent.changedKeys(); if(!CollectionUtils.isEmpty(changeKeys) && changeKeys.contains(conditionalOnPropertyKey)){ return true; } return false; } private String getChangeKey(ConfigChangeEvent changeEvent, String[] conditionalOnPropertyKeys){ if(ArrayUtils.isEmpty(conditionalOnPropertyKeys)){ return null; } String changeKey = null; for (String conditionalOnPropertyKey : conditionalOnPropertyKeys) { if(isChangeKey(changeEvent,conditionalOnPropertyKey)){ changeKey = conditionalOnPropertyKey; break; } } return changeKey; } private BeanDefinitionRegistry getBeanDefinitionRegistry(){ ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) applicationContext; BeanDefinitionRegistry beanDefinitionRegistry = (DefaultListableBeanFactory) configurableContext.getBeanFactory(); return beanDefinitionRegistry; } private List<String> listBasePackages(){ ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) applicationContext; return AutoConfigurationPackages.get(configurableContext.getBeanFactory()); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public void printAllBeans() { String[] beans = applicationContext.getBeanDefinitionNames(); Arrays.sort(beans); for (String beanName : beans) { Class<?> beanType = applicationContext.getType(beanName); System.out.println(beanType); } } }
如果条件注解的值也是配置在apollo上,可能会出现依赖条件注解的bean的其他bean,在项目拉取apollo配置时,就已经注入spring容器中,此时就算条件注解满足条件,则引用该条件注解bean的其他bean,也会拿不到条件注解bean。此时有2种方法解决,一种是在依赖条件注解bean的其他bean注入之前,先手动注册条件注解bean到spring容器中,其核心代码如下
@Component @Slf4j public class RefreshBeanFactory implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { Config config = ConfigService.getConfig("order.properties"); List<String> basePackages = AutoConfigurationPackages.get(configurableListableBeanFactory); for (String basePackage : basePackages) { Set<Class> conditionalClasses = ClassScannerUtils.scan(basePackage, ConditionalOnProperty.class); if(!CollectionUtils.isEmpty(conditionalClasses)){ for (Class conditionalClass : conditionalClasses) { ConditionalOnProperty conditionalOnProperty = (ConditionalOnProperty) conditionalClass.getAnnotation(ConditionalOnProperty.class); String[] conditionalOnPropertyKeys = conditionalOnProperty.name(); String beanConditionKey = this.getConditionalOnPropertyKey(config,conditionalOnPropertyKeys); String conditionalOnPropertyValue = conditionalOnProperty.havingValue(); this.registerBeanIfMatchCondition((DefaultListableBeanFactory)configurableListableBeanFactory,config,conditionalClass,beanConditionKey,conditionalOnPropertyValue); } } } } private void registerBeanIfMatchCondition(DefaultListableBeanFactory beanFactory,Config config,Class conditionalClass, String beanConditionKey, String conditionalOnPropertyValue) { boolean isNeedRegisterBean = this.isNeedRegisterBean(config,beanConditionKey,conditionalOnPropertyValue); String beanName = StringUtils.uncapitalize(conditionalClass.getSimpleName()); if(isNeedRegisterBean){ this.registerBean(config,beanFactory,beanName,conditionalClass); } } public void registerBean(Config config,DefaultListableBeanFactory beanFactory, String beanName, Class beanClass) { log.info("registerBean->beanName:{},beanClass:{}",beanName,beanClass); BeanDefinitionBuilder beanDefinitionBurinilder = BeanDefinitionBuilder.genericBeanDefinition(beanClass); BeanDefinition beanDefinition = beanDefinitionBurinilder.getBeanDefinition(); setBeanField(config,beanClass, beanDefinition); beanFactory.registerBeanDefinition(beanName,beanDefinition); } private void setBeanField(Config config,Class beanClass, BeanDefinition beanDefinition) { ConfigurationProperties configurationProperties = (ConfigurationProperties) beanClass.getAnnotation(ConfigurationProperties.class); if(ObjectUtils.isNotEmpty(configurationProperties)){ String prefix = configurationProperties.prefix(); for (String propertyName : config.getPropertyNames()) { String fieldPrefix = prefix + "."; if(propertyName.startsWith(fieldPrefix)){ String fieldName = propertyName.substring(fieldPrefix.length()); String fieldVal = config.getProperty(propertyName,null); log.info("setBeanField-->fieldName:{},fieldVal:{}",fieldName,fieldVal); beanDefinition.getPropertyValues().add(fieldName,fieldVal); } } } } public boolean isNeedRegisterBean(Config config,String beanConditionKey,String conditionalOnPropertyValue){ if(StringUtils.isEmpty(beanConditionKey)){ return false; } String apolloConfigValue = config.getProperty(beanConditionKey,null); return conditionalOnPropertyValue.equals(apolloConfigValue); } private String getConditionalOnPropertyKey(Config config, String[] conditionalOnPropertyKeys){ if(ArrayUtils.isEmpty(conditionalOnPropertyKeys)){ return null; } String changeKey = null; for (String conditionalOnPropertyKey : conditionalOnPropertyKeys) { if(isConditionalOnPropertyKey(config,conditionalOnPropertyKey)){ changeKey = conditionalOnPropertyKey; break; } } return changeKey; } private boolean isConditionalOnPropertyKey(Config config,String conditionalOnPropertyKey){ Set<String> propertyNames = config.getPropertyNames(); if(!CollectionUtils.isEmpty(propertyNames) && propertyNames.contains(conditionalOnPropertyKey)){ return true; } return false; } }
其次利用懒加载的思想,在使用条件注解bean时,使用形如下方法
Order order = (Order) SpringContextUtils.getBean("order");
总结
本文主要介绍了常用的动态刷新,但本文的代码示例实现的功能不局限于此,本文的代码还实现如何通过自定义注解与apollo整合来实现一些业务操作,同时也实现了基于hystrix注解与apollo整合,实现基于线程隔离的动态熔断,感兴趣的朋友可以复制文末链接到浏览器,进行查看
apollo基本上是能满足我们日常的业务开发要求,但是对于一些需求,比如动态刷新线上数据库资源啥,我们还是得做一定的量的改造,好在携程也提供了apollo-use-cases,在里面可以找到常用的使用场景以及示例代码,其链接如下
https://github.com/ctripcorp/apollo-use-cases
感兴趣的朋友,可以查看下。
demo链接
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-apollo
到此这篇关于apollo与springboot集成实现动态刷新配置的文章就介绍到这了,更多相关apollo与springboot集成动态刷新配置内容请搜索小牛知识库以前的文章或继续浏览下面的相关文章希望大家以后多多支持小牛知识库!
本文向大家介绍spring boot 配置动态刷新实现详解,包括了spring boot 配置动态刷新实现详解的使用技巧和注意事项,需要的朋友参考一下 本文测试使用的spring cloud版本为: Dalston.SR1 很多朋友只知道spring cloud config可以刷新远程git的配置到内存中, 却不知道spring cloud config的客户端可以脱离服务端使用, 更不知道sp
本文向大家介绍springmvc与mybatis集成配置实例详解,包括了springmvc与mybatis集成配置实例详解的使用技巧和注意事项,需要的朋友参考一下 简单之美,springmvc,mybatis就是一个很好的简单集成方案,能够满足一般的项目需求。闲暇时间把项目配置文件共享出来,供大家参看: 1.首先我们来看下依赖的pom: spring 选用的是4.1.4的版本,根据系统需要我们可以
本文向大家介绍Springboot集成fastDFS配置过程解析,包括了Springboot集成fastDFS配置过程解析的使用技巧和注意事项,需要的朋友参考一下 配置文件 配置fdfs_client.conf配置文件 # 连接超时时间,针对socket套接字函数connect,默认为30秒 connect_timeout=30000 # 网络通讯超时时间,默认是60秒 network_timeo
本文向大家介绍Spring Boot实战教程之自动配置详解,包括了Spring Boot实战教程之自动配置详解的使用技巧和注意事项,需要的朋友参考一下 前言 大家应该都有所了解,随着Ruby、Groovy等动态语言的流行,相比较之下Java的开发显得格外笨重。繁多的配置、低下的开发效率、复杂的部署流程以及第三方技术集成难度大等问题一直被人们所诟病。随着Spring家族中的新星Spring Boot
我从“dynconfigflows”-目录中加载xml配置文件,然后动态创建IntegrationFlow。 每个xml文件一个IntegrationFlow。如果配置文件已编辑,则应更新IntegrationFlow。IntegrationFlow的注册id被设置为配置文件名。 ,因此当前的流应该平稳地停止(没有更多的输入消息和当前处理的消息应该完成)。在此之后,应该删除它,并注册流的更新版本。
本文向大家介绍详解SpringBoot+Dubbo集成ELK实战,包括了详解SpringBoot+Dubbo集成ELK实战的使用技巧和注意事项,需要的朋友参考一下 前言 一直以来,日志始终伴随着我们的开发和运维过程。当系统出现了Bug,往往就是通过Xshell连接到服务器,定位到日志文件,一点点排查问题来源。 随着互联网的快速发展,我们的系统越来越庞大。依赖肉眼分析日志文件来排查问题的方式渐渐凸显