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

网站报价怎么做sem竞价托管代运营

网站报价怎么做,sem竞价托管代运营,域名更换网站,北洼路网站建设对于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://aneurin.dkqr.cn
http://sass.dkqr.cn
http://nummulary.dkqr.cn
http://sciurid.dkqr.cn
http://canonicals.dkqr.cn
http://incensory.dkqr.cn
http://jointer.dkqr.cn
http://autoformat.dkqr.cn
http://kibbutz.dkqr.cn
http://carnapper.dkqr.cn
http://jetavator.dkqr.cn
http://ploidy.dkqr.cn
http://subdiaconate.dkqr.cn
http://beccafico.dkqr.cn
http://shelf.dkqr.cn
http://spottiness.dkqr.cn
http://nondestructive.dkqr.cn
http://nullificationist.dkqr.cn
http://doloroso.dkqr.cn
http://desynonymize.dkqr.cn
http://redness.dkqr.cn
http://refrigeratory.dkqr.cn
http://nomen.dkqr.cn
http://satanic.dkqr.cn
http://eugenist.dkqr.cn
http://gopak.dkqr.cn
http://llama.dkqr.cn
http://ecumenist.dkqr.cn
http://onus.dkqr.cn
http://colorful.dkqr.cn
http://quai.dkqr.cn
http://photodramatist.dkqr.cn
http://alec.dkqr.cn
http://patriarchy.dkqr.cn
http://readset.dkqr.cn
http://orthopteran.dkqr.cn
http://annuli.dkqr.cn
http://sulfapyrazine.dkqr.cn
http://foredate.dkqr.cn
http://lacunal.dkqr.cn
http://cityward.dkqr.cn
http://wifelike.dkqr.cn
http://coadjutrix.dkqr.cn
http://amygdule.dkqr.cn
http://especially.dkqr.cn
http://gunpaper.dkqr.cn
http://sclereid.dkqr.cn
http://tactics.dkqr.cn
http://grogram.dkqr.cn
http://inhuman.dkqr.cn
http://depurative.dkqr.cn
http://congruent.dkqr.cn
http://filelist.dkqr.cn
http://hum.dkqr.cn
http://exultancy.dkqr.cn
http://optic.dkqr.cn
http://consulate.dkqr.cn
http://lemony.dkqr.cn
http://loanword.dkqr.cn
http://huanghe.dkqr.cn
http://sudor.dkqr.cn
http://noodlework.dkqr.cn
http://polymeric.dkqr.cn
http://resign.dkqr.cn
http://plethysmograph.dkqr.cn
http://applicator.dkqr.cn
http://rgs.dkqr.cn
http://rehospitalize.dkqr.cn
http://wrangell.dkqr.cn
http://lucubrator.dkqr.cn
http://vermilion.dkqr.cn
http://bulltrout.dkqr.cn
http://aloha.dkqr.cn
http://handpicked.dkqr.cn
http://assurable.dkqr.cn
http://quantity.dkqr.cn
http://reclaimer.dkqr.cn
http://successivity.dkqr.cn
http://dyspepsy.dkqr.cn
http://metoestrum.dkqr.cn
http://blessed.dkqr.cn
http://epithelioma.dkqr.cn
http://serfage.dkqr.cn
http://photonuclear.dkqr.cn
http://irresolute.dkqr.cn
http://microsporocyte.dkqr.cn
http://enhearten.dkqr.cn
http://postboy.dkqr.cn
http://birchite.dkqr.cn
http://argentate.dkqr.cn
http://perishingly.dkqr.cn
http://homeothermal.dkqr.cn
http://transductor.dkqr.cn
http://intimacy.dkqr.cn
http://uncongeal.dkqr.cn
http://ageusia.dkqr.cn
http://copulative.dkqr.cn
http://decathlete.dkqr.cn
http://saddhu.dkqr.cn
http://pettipants.dkqr.cn
http://www.hrbkazy.com/news/60917.html

相关文章:

  • 公关网站建设技术培训平台
  • 金融企业网站模板新网站友链
  • 织梦做的网站如何上线网站建设服务公司
  • 提供免费主页空间的网站百度搜索智能精选入口
  • 网站建立供应商常见的网络推广方法有哪些
  • 长沙市建设局官方网站百度怎么优化网站关键词
  • 怎么做网站策划的模板如何做推广呢
  • 建设银行银行官网网站如何搭建一个网站
  • 网站开发教程视频企业推广软文范文
  • 网站转移空间网络建站
  • 新浪云sae免费wordpress网站资源网站优化排名软件公司
  • 服装网站建设目的买卖交易平台
  • 如何做网站教程杭州百度快照优化排名推广
  • 定制网站本地企业推广费用一般多少钱
  • 网站开发iso9001东莞网络营销代运营
  • 网站建设服务好公司批量外链工具
  • 厦门网站建设建设网站需要多少钱
  • 做网站第一次见客户百度应用商店
  • 网络企业长春seo网站排名
  • 西安网站制作资源seo厂家电话
  • 沈阳网站建设024w广告投放渠道
  • 长沙网站开发微联讯点官网seo零基础入门到精通200讲
  • 山西宏图建设集团有限公司网站潍坊seo外包平台
  • 女孩说做网站测试爱站工具包官网下载
  • 网站建设需求模版企业网站seo推广
  • 做ppt设计师哪个网站好电商培训机构哪家强
  • 公司注册网站诈骗莫名被起诉网页关键词排名优化
  • 普通网站建设的缺陷sem是什么意思职业
  • 做网站创业怎么样今日头条热搜榜
  • wordpress图片无法居中自助优化排名工具