当前位置: 首页 > news >正文

php网站开发参考文献wordpress发布pdf

php网站开发参考文献,wordpress发布pdf,网站做接口到app 价格,免费自助建站网站建设免费信息发布对于springboot配置自动刷新,原理也很简单,就是在启动过程中用一个BeanPostProcessor去收集需要自动刷新的字段,然后在springboot启动后开启轮询任务即可。 不过需要对之前的代码再次做修改,因为springboot的配置注入value("…

对于springboot配置自动刷新,原理也很简单,就是在启动过程中用一个BeanPostProcessor去收集需要自动刷新的字段,然后在springboot启动后开启轮询任务即可。
不过需要对之前的代码再次做修改,因为springboot的配置注入@value("${}"),允许多个${}和嵌套,所以不能确定简单的确定用到了那个配置,本文为了简单就把所有的配置都认为需要动态刷新,实际用的时候可以在application.yml中配置需要动态刷新的配置id列表。代码在https://gitee.com/summer-cat001/config-center。其中设计到的原理都在之前的一篇文章中,感兴趣可以去看看springboot配置注入增强(二)属性注入的原理_springboot bean属性增强-CSDN博客

新增注解

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigRefresh {
}

加上这个注解的字段并且字段上有@value注解就会自动刷新

收集自动刷新的字段

这里会收集自动刷新的字段,并加到ConfigCenterClient的refreshFieldValueList中。长轮询会从这里取数据进行对比,如果发生变化就更新bean中的字段

@Slf4j
public class ConfigRefreshAnnotationBeanPostProcessor implements ApplicationRunner, BeanPostProcessor, BeanFactoryAware, EnvironmentAware {private Environment environment;private ConfigurableBeanFactory beanFactory;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {if (!(beanFactory instanceof ConfigurableBeanFactory)) {log.warn("ConfigurableBeanFactory requires a ConfigurableListableBeanFactory");return;}this.beanFactory = (ConfigurableBeanFactory) beanFactory;}@Overridepublic Object postProcessBeforeInitialization(Object bean, final String beanName) throws BeansException {if (beanFactory != null) {ReflectionUtils.doWithFields(bean.getClass(), field -> {try {ConfigRefresh configRefresh = AnnotationUtils.getAnnotation(field, ConfigRefresh.class);if (configRefresh == null) {return;}Value valueAnnotation = AnnotationUtils.getAnnotation(field, Value.class);if (valueAnnotation == null) {return;}String value = valueAnnotation.value();String relValue = beanFactory.resolveEmbeddedValue(value);ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.addRefreshFieldValue(bean, field, relValue);} catch (Exception e) {log.error("set bean field fail,beanName:{},fieldName:{}", bean.getClass().getName(), field.getName(), e);}});}return bean;}@Overridepublic void setEnvironment(Environment environment) {this.environment = environment;}@Overridepublic void run(ApplicationArguments args) {ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.startSpringBootLongPolling((ConfigurableEnvironment) environment, beanFactory);}
}

把该bean注入到springboot中,即在spring.factories中加入自动注入

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.config.center.autoconfigure.ConfigAutoConfiguration

这是一个ImportSelector会自动注入返回的类

@Import(ConfigAutoConfiguration.class)
public class ConfigAutoConfiguration implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{ConfigRefreshAnnotationBeanPostProcessor.class.getName()};}
}

启动长轮询

springboot启动完成后会发一个ApplicationRunner事件,我们只要在实现这个接口的bean中启动即可

@Overridepublic void run(ApplicationArguments args) {ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.startSpringBootLongPolling((ConfigurableEnvironment) environment, beanFactory);}
    public void startSpringBootLongPolling(ConfigurableEnvironment environment, ConfigurableBeanFactory beanFactory) {if (configMap.isEmpty() || refreshFieldValueList.isEmpty()) {log.info("configMap.size:{} refreshFieldValueList.size:{}", configMap.size(), refreshFieldValueList.size());return;}MutablePropertySources propertySources = environment.getPropertySources();MapPropertySource configCenter = (MapPropertySource) propertySources.get(PROPERTY_SOURCE_NAME);if (configCenter == null) {log.warn("configCenter is null");return;}Map<String, Object> source = configCenter.getSource();Thread thread = new Thread(() -> {while (!Thread.interrupted()) {try {Map<String, Integer> configIdMap = configMap.values().stream().collect(Collectors.toMap(c -> c.getId() + "", ConfigBO::getVersion));HttpRespBO httpRespBO = HttpUtil.httpPostJson(url + "/config/change/get/long", JSON.toJSONString(configIdMap), 30000);List<ConfigVO> configList = httpResp2ConfigVOList(httpRespBO);if (configList.isEmpty()) {continue;}configList.forEach(configVO -> {Map<String, Object> result = new HashMap<>();DataTransUtil.buildFlattenedMap(result, configVO.getConfigData(), "");ConfigBO configBO = this.configMap.get(configVO.getId());configBO.setVersion(configVO.getVersion());List<ConfigDataBO> configDataList = configBO.getConfigDataList();Map<String, ConfigDataBO> configDataMap = configDataList.stream().collect(Collectors.toMap(ConfigDataBO::getKey, Function.identity()));result.forEach((key, value) -> {ConfigDataBO configDataBO = configDataMap.get(key);if (configDataBO == null) {configDataList.add(new ConfigDataBO(key, value.toString()));} else {configDataBO.setValue(value.toString());source.put(key, value);}});});refreshFieldValueList.forEach(refreshFieldBO -> {try {Field field = refreshFieldBO.getField();Value valueAnnotation = AnnotationUtils.getAnnotation(field, Value.class);if (valueAnnotation == null) {return;}String value = valueAnnotation.value();String relValue = beanFactory.resolveEmbeddedValue(value);if(relValue.equals(refreshFieldBO.getValue())){return;}field.setAccessible(true);field.set(refreshFieldBO.getBean(), relValue);} catch (Exception e) {log.error("startSpringBootLongPolling set Field error", e);}});} catch (Exception e) {log.error("startSpringBootLongPolling error", e);}}});thread.setName("startSpringBootLongPolling");thread.setDaemon(true);thread.start();}

效果

@Value

@Data
@Component
public class ConfigTest {@ConfigRefresh@Value("${user.name}")private String name;}
    @Autowiredprivate ConfigTest configTest;@Testpublic void configTest() throws InterruptedException {while (true) {System.out.println(configTest.getName());Thread.sleep(1000);}}

@ConfigurationProperties

增加同时有@ConfigurationProperties和@ConfigRefresh的收集

ConfigRefresh configRefresh = AnnotationUtils.findAnnotation(bean.getClass(), ConfigRefresh.class);if (configRefresh != null) {ConfigurationProperties configurationProperties = AnnotationUtils.findAnnotation(bean.getClass(), ConfigurationProperties.class);if (configurationProperties != null) {ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.addRefreshBeanList(bean);}}

在长轮询的返回中对@ConfigurationProperties重新绑定

refreshBeanList.forEach(refreshBean -> {ConfigurationProperties configurationProperties = AnnotationUtils.findAnnotation(refreshBean.getClass(), ConfigurationProperties.class);if (configurationProperties == null) {log.warn("refreshBeanList refreshBean configurationProperties is null, class:{}", refreshBean.getClass());return;}Binder binder = Binder.get(environment);binder.bind(configurationProperties.prefix(), Bindable.ofInstance(refreshBean));});

完整代码

@Slf4j
public class ConfigRefreshAnnotationBeanPostProcessor implements ApplicationRunner, BeanPostProcessor, BeanFactoryAware, EnvironmentAware {private Environment environment;private ConfigurableBeanFactory beanFactory;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {if (!(beanFactory instanceof ConfigurableBeanFactory)) {log.warn("ConfigurableBeanFactory requires a ConfigurableListableBeanFactory");return;}this.beanFactory = (ConfigurableBeanFactory) beanFactory;}@Overridepublic Object postProcessBeforeInitialization(Object bean, final String beanName) throws BeansException {if (beanFactory != null) {ReflectionUtils.doWithFields(bean.getClass(), field -> {try {ConfigRefresh configRefresh = AnnotationUtils.getAnnotation(field, ConfigRefresh.class);if (configRefresh == null) {return;}Value valueAnnotation = AnnotationUtils.getAnnotation(field, Value.class);if (valueAnnotation == null) {return;}String value = valueAnnotation.value();String relValue = beanFactory.resolveEmbeddedValue(value);ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.addRefreshFieldValue(bean, field, relValue);} catch (Exception e) {log.error("set bean field fail,beanName:{},fieldName:{}", bean.getClass().getName(), field.getName(), e);}});ConfigRefresh configRefresh = AnnotationUtils.findAnnotation(bean.getClass(), ConfigRefresh.class);if (configRefresh != null) {ConfigurationProperties configurationProperties = AnnotationUtils.findAnnotation(bean.getClass(), ConfigurationProperties.class);if (configurationProperties != null) {ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.addRefreshBeanList(bean);}}}return bean;}@Overridepublic void setEnvironment(Environment environment) {this.environment = environment;}@Overridepublic void run(ApplicationArguments args) {ConfigCenterClient configCenterClient = ConfigCenterClient.getInstance(null);configCenterClient.startSpringBootLongPolling((ConfigurableEnvironment) environment, beanFactory);}
}
 public void startSpringBootLongPolling(ConfigurableEnvironment environment, ConfigurableBeanFactory beanFactory) {if (configMap.isEmpty() || refreshFieldValueList.isEmpty()) {log.info("configMap.size:{} refreshFieldValueList.size:{}", configMap.size(), refreshFieldValueList.size());return;}MutablePropertySources propertySources = environment.getPropertySources();MapPropertySource configCenter = (MapPropertySource) propertySources.get(PROPERTY_SOURCE_NAME);if (configCenter == null) {log.warn("configCenter is null");return;}Map<String, Object> source = configCenter.getSource();Thread thread = new Thread(() -> {while (!Thread.interrupted()) {try {Map<String, Integer> configIdMap = configMap.values().stream().collect(Collectors.toMap(c -> c.getId() + "", ConfigBO::getVersion));HttpRespBO httpRespBO = HttpUtil.httpPostJson(url + "/config/change/get/long", JSON.toJSONString(configIdMap), 30000);List<ConfigVO> configList = httpResp2ConfigVOList(httpRespBO);if (configList.isEmpty()) {continue;}configList.forEach(configVO -> {Map<String, Object> result = new HashMap<>();DataTransUtil.buildFlattenedMap(result, configVO.getConfigData(), "");ConfigBO configBO = this.configMap.get(configVO.getId());configBO.setVersion(configVO.getVersion());List<ConfigDataBO> configDataList = configBO.getConfigDataList();Map<String, ConfigDataBO> configDataMap = configDataList.stream().collect(Collectors.toMap(ConfigDataBO::getKey, Function.identity()));result.forEach((key, value) -> {ConfigDataBO configDataBO = configDataMap.get(key);if (configDataBO == null) {configDataList.add(new ConfigDataBO(key, value.toString()));} else {configDataBO.setValue(value.toString());source.put(key, value);}});});refreshFieldValueList.forEach(refreshFieldBO -> {try {Field field = refreshFieldBO.getField();Value valueAnnotation = AnnotationUtils.getAnnotation(field, Value.class);if (valueAnnotation == null) {return;}String value = valueAnnotation.value();String relValue = beanFactory.resolveEmbeddedValue(value);if (relValue.equals(refreshFieldBO.getValue())) {return;}field.setAccessible(true);field.set(refreshFieldBO.getBean(), relValue);} catch (Exception e) {log.error("startSpringBootLongPolling set Field error", e);}});refreshBeanList.forEach(refreshBean -> {ConfigurationProperties configurationProperties = AnnotationUtils.findAnnotation(refreshBean.getClass(), ConfigurationProperties.class);if (configurationProperties == null) {log.warn("refreshBeanList refreshBean configurationProperties is null, class:{}", refreshBean.getClass());return;}Binder binder = Binder.get(environment);binder.bind(configurationProperties.prefix(), Bindable.ofInstance(refreshBean));});} catch (Exception e) {log.error("startSpringBootLongPolling error", e);}}});thread.setName("startSpringBootLongPolling");thread.setDaemon(true);thread.start();}

效果

@Component
@ConfigRefresh
@ConfigurationProperties(prefix = "user")
public class ConfigTest2 {private String name;private int age;private List<String> education;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public List<String> getEducation() {return education;}public void setEducation(List<String> education) {this.education = education;}
}
   @Autowiredprivate ConfigTest2 configTest2;@Testpublic void configTest() throws InterruptedException {while (true) {System.out.println(configTest2.getName() + "-" + configTest2.getAge() + "-" + configTest2.getEducation());Thread.sleep(1000);}}

http://www.sczhlp.com/news/131201/

相关文章:

  • 如何创建一个网站用来存放东西三角网站建设
  • 网站首页结构图安全认证的机票网站成就怎么做
  • 网站怎样做 文件签收wordpress博客做seo
  • 太原网站建设公司大全中国十大做网站公司排名
  • 网站 wordpress六安市网站建设
  • 合肥政务新区建设局网站江苏住房与城乡建设部网站
  • 北京专业建设网站公司哪家好wordpress 新建媒体库
  • 哈尔滨模板建站服务商建站服务的网络公司有哪些
  • 做相册本哪个网站好用推动房地产发展新模式
  • 坂田网站建设推广公司网建设门户网站
  • 哪家做公司网站用word文档做网站
  • 衡水网站建设在哪里网站建设数据库模板
  • 主要测试的测试用例
  • 详细介绍Seata的AT模式分布式事务
  • 清爽网站模板外 网站 公司
  • 科讯cms制作网站完整教程物流企业网站建设方案
  • 舆情网站直接打开的软件网站备案流程图片
  • VMware VeloCloud 漏洞分析:未授权远程代码执行全链条攻破
  • 做校招的网站有哪些网站标题优化
  • 村建站属于哪个部门电子商务网站开发这书不出版了吗
  • 网站建设及推广人员企业组织架构
  • 建设网站的基本流程是什么网页设计与网站建设有区别吗
  • 网站名称需要备案吗广州网站营销
  • 专业建材网站建设网站建设开题报告
  • 网站建设 提案 框架爱站网seo工具
  • 教育网站建设案例搭建是什么意思
  • 天津网站建设zymxart7一12岁手工
  • 保洁公司在哪个网站做推广比较好一个公司为什么要做网站
  • 网站优化任务龙岗住房和建设局网站官网
  • 网站排名优化手机wordpress自动发布