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

青浦营销型网站建设Wordpress 淘宝客 页面

青浦营销型网站建设,Wordpress 淘宝客 页面,免费python在线正常网站,合肥如何做百度的网站推广文章目录 Core什么是配置中心#xff1f;以及如何实现一个配置中心#xff1f;SpringBoot如何实现配置的管控#xff1f;SpringCloud项目是如何对bootstrap配置文件进行加载的#xff1f;Nacos是如何实现配置文件的读取加载的#xff1f;开发配置中心前必须了解的前置知识… 文章目录 Core什么是配置中心以及如何实现一个配置中心SpringBoot如何实现配置的管控SpringCloud项目是如何对bootstrap配置文件进行加载的Nacos是如何实现配置文件的读取加载的开发配置中心前必须了解的前置知识配置中心Server和Client端代码的编写配置中心Core核心功能代码的编写配置中心源码优化---本地缓存与读写锁 网关项目源码 RPC项目源码 配置中心项目源码 Core Core模块是我们项目最核心最重要的模块当别人需要使用我们的配置中心的时候只需要引入Core模块在项目启动的时候就会自动连接我们的配置中心获取配置并刷新本地的配置。 接下来我们来看看Core模块是如何实现的。 这里按照我们的前置知识可以知道我们自上到下需要完成如下几件事情我们在复习一下 bootstrap配置的获取配置中心的连接与配置的获取Locator的实现加载配置中心的配置 所以我写了一个启动类也是按照完成这三个事情的顺序对Bean进行加载。 Configuration(proxyBeanMethods false) ConditionalOnProperty(name spring.cloud.blossom.config.enabled, matchIfMissing true) public class BlossomConfigBootstrapConfiguration {/*** 项目对配置中心配置* return*/BeanConditionalOnMissingBeanpublic BlossomConfigProperties blossomConfigProperties(){return new BlossomConfigProperties();}/*** 配置中心管理器* param blossomConfigProperties* return*/BeanConditionalOnMissingBeanpublic BlossomConfigManager blossomConfigManager(BlossomConfigProperties blossomConfigProperties) {return new BlossomConfigManager(blossomConfigProperties);}Beanpublic BlossomConfigChangePublisher blossomConfigChangePublisher(ApplicationEventPublisher applicationEventPublisher){return new BlossomConfigChangePublisher(applicationEventPublisher);}/*** 配置中心配置加载器* param blossomConfigManager* return*/Beanpublic BlossomPropertySourceLocator blossomPropertySourceLocator(BlossomConfigManager blossomConfigManager) {return new BlossomPropertySourceLocator(blossomConfigManager);}Beanpublic BlossomConfigChangeEventSubscriber blossomConfigChangeEventSubscriber(BlossomConfigManager manager,BlossomConfigChangePublisher publisher){return new BlossomConfigChangeEventSubscriber(manager.getConfigService(),publisher);}}代码实现起来其实非常非常简单。 一切的难点都是在于代码的编写顺序。 这里我们跳过对boostrap配置文件的获取的代码在前面我们已经提到过了如果你是SpringCloud项目直接引入spring-cloud-starter-boostrap依赖就可以直接帮助你完成对bootstrap配置文件的解析如果你非要手写那么你就按照前面的方式编写一个事件监听器来读取你的配置文件。这里我追求效率就不手写了。 那么我们的配置文件就很快的完成了加载。 然后很简单的我们依赖Client模块提供的配置中心的创建工程来创建Core模块的配置中心。 public class BlossomConfigFactory {/*** 用于创建ConfigService配置中心* param properties 配置中心的创建需要用到配置文件* return* throws BlossomException*/public static ConfigService createConfigService(Properties properties) throws BlossomException {try {Class? configServiceClass Class.forName(blossom.project.config.client.BlossomConfigService);Constructor constructor configServiceClass.getConstructor(Properties.class);ConfigService configService (ConfigService) constructor.newInstance(properties);return configService;} catch (Throwable e) {throw new BlossomException(BlossomException.REFLECT_CREATE_ERROR,e.getMessage(), e);}}}到此为止我们其实就完成了配置的加载和配置中心的创建。 那么接下来要做的就是初始化配置中心的配置拉取配置中心的配置。 并加载到本地作为PropertySource返回。 也是一样我们重写Locator方法。 package blossom.project.config.core;import blossom.project.config.client.ConfigService; import blossom.project.config.common.constants.BlossomConstants; import blossom.project.config.common.enums.ConfigType; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource;import java.util.List; import java.util.Objects; import java.util.Properties; import java.util.Set;import static blossom.project.config.common.constants.BlossomConstants.DOT; import static blossom.project.config.common.constants.BlossomConstants.SEPARATOR;/*** author: ZhangBlossom* date: 2023/12/28 17:35* contact: QQ:4602197553* contact: WX:qczjhczs0114* blog: https://blog.csdn.net/Zhangsama1* github: https://github.com/ZhangBlossom* BlossomPropertySourceLocator类* 在编写这个类之前应该先将ConfigService实现类编写完毕* 然后在这个类里面得到ConfigService之后* 调用里面的方法获取到配置中心的配置之后* 将配置加载到本地 同时考虑编写一套缓存*/ Slf4j Order(0) public class BlossomPropertySourceLocator implements PropertySourceLocator {private BlossomConfigManager manager;private BlossomConfigProperties properties;//使用builder的方式得到来自各种地方的BlossomPropertySource//最后将BlossomPropertySource放入到CompositePropertySource即可private BlossomPropertySourceBuilder blossomPropertySourceBuilder;public BlossomPropertySourceLocator(BlossomConfigManager manager) {this.manager manager;this.properties manager.getProperties();}Overridepublic PropertySource? locate(Environment environment) {this.properties.setEnvironment(environment);ConfigService configService manager.getConfigService();if (Objects.isNull(configService)) {log.warn(No instance of ConfigService was found,can not load config from ConfigService);return null;}this.blossomPropertySourceBuilder new BlossomPropertySourceBuilder(configService, properties);CompositePropertySource ps new CompositePropertySource(BlossomConstants.BLOSSOM_PROPERTY_SOURCE_NAME);loadApplicationConfig(ps);loadConfigLists(ps);return ps;}/*** 加载项目所有配置* param ps*/private void loadConfigLists(CompositePropertySource ps) {ListBlossomConfigProperties.BlossomConfig configLists this.properties.getConfigLists();configLists.forEach(config - {loadConfigIfPresent(ps, config.getConfigId(), config.getGroup(), this.properties.getFileExtension());});}/*** 在项目项目原生配置* param ps*/private void loadApplicationConfig(CompositePropertySource ps) {Environment env this.properties.getEnvironment();String applicationName env.getProperty(spring.application.name);String group this.properties.getGroup();String fileExtension this.properties.getFileExtension();for (String profile : env.getActiveProfiles()) {//blossom-core-dev.yamlString configId applicationName SEPARATOR profile DOT this.properties.getFileExtension();loadConfigIfPresent(ps, configId, group, fileExtension);}}private void loadConfigIfPresent(CompositePropertySource ps, String configId, String group, String fileExtension) {if (StringUtils.isBlank(configId)) {return;}if (StringUtils.isBlank(group)) {return;}Boolean validType ConfigType.isValidType(fileExtension);//the file extension is unvalid;if (!validType) {return;}this.loadBlossomConfig(ps, configId, group, fileExtension);}private void loadBlossomConfig(CompositePropertySource ps, String configId, String group, String fileExtension) {//1:从配置中心获取配置 并且封装为BlossomPropertySourceBlossomPropertySource blossomPropertySource this.blossomPropertySourceBuilder.buildBlossomPropertySource(configId, group, fileExtension);//2将配置转换为PropertySource ---能得到Properties类型即可//3:将配置添加到CompositePropertySourceps.addFirstPropertySource(blossomPropertySource);}}然后在代码中按照我们所说的完成对配置中心配置的加载和获取。 下面的代码才是真正的连接到配置中心并且对配置中心的配置进行拉取以及解析的地方。 那么到此我们就已经完成了项目启动的时候的初始化属性的配置了。 接下来我们要思考一下如何在项目的配置文件发生变更的时候能刷新本地的配置呢 package blossom.project.config.core;import blossom.project.config.client.ConfigService; import blossom.project.config.common.exception.BlossomException; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.core.env.PropertySource;import java.util.Collections; import java.util.Date; import java.util.List;/*** author: ZhangBlossom* date: 2023/12/29 20:16* contact: QQ:4602197553* contact: WX:qczjhczs0114* blog: https://blog.csdn.net/Zhangsama1* github: https://github.com/ZhangBlossom* BlossomPropertySourceBuilder类*/ Data Slf4j public class BlossomPropertySourceBuilder {private ConfigService configService;private BlossomConfigProperties properties;public BlossomPropertySourceBuilder(ConfigService configService, BlossomConfigProperties properties) {this.configService configService;this.properties properties;}/*** 当前方法会完成BlossomPropertySource的构建** param configId* param group* param fileExtension* return*/BlossomPropertySource buildBlossomPropertySource(String configId, String group, String fileExtension) {//从配置中心得到配置并且封装为List类型的PropertySourceListPropertySource? propertySources loadBlossomConfigData(configId, group, fileExtension);//将List转换为最后我们需要的BlossomPropertySourceBlossomPropertySource blossomPropertySource new BlossomPropertySource(group, configId, new Date(),propertySources);return blossomPropertySource;}/*** 当前方法完成对配置中心配置的加载和解析* 并且最终返回PropertySource集合** param configId* param group* param fileExtension* return*/private ListPropertySource? loadBlossomConfigData(String configId, String group, String fileExtension) {ListPropertySource? propertySources Collections.emptyList();try {//得到配置文件的内容String configData this.configService.getConfig(configId, group, fileExtension);if (StringUtils.isBlank(configData)) {log.warn(the data from ConfigService is empty, configId: {}, group:{}, configId, group);return Collections.emptyList();}//在spring中想要将配置解析为PropertySource可以用自带的解析器--只提供了yaml和properties//也就是说json/xml等其他格式需要自己实现propertySources BlossomConfigDataHandler.getInstance().parseConfigData(configId, configData, fileExtension);} catch (BlossomException e) {log.error(get the config data from ConfigService failed, configId: {}, group:{},Exception:{}, configId,group, e);} catch (Exception e) {log.error(parse the config data failed. Exception:{}, e);}return propertySources;}}我们知道其实对于配置变更事件两种实现方法一种push一种pull。 push就是让Server端主动的通知Client实现起来相比pull更加复杂因此这里我们选择使用pull的方式也就是让CLient主动的去Server端拉取配置变更。 那么这里就会用到我们上面所说的长轮询了以及我们的事件发布机制。 因为我们得让Client这个普通的Java项目能通知道Core模块这个Spring项目并且让我们轻松的利用到Spring项目中提供的强大的事件监听机制。 重点就是Client模块我说到的Publish接口。 在Core模块中 Bean public BlossomConfigChangeEventSubscriber blossomConfigChangeEventSubscriber(BlossomConfigManager manager,BlossomConfigChangePublisher publisher){return new BlossomConfigChangeEventSubscriber(manager.getConfigService(),publisher); }有如下的一个类这个类的作用就是在Core模块启动的时候主动的根据配置中心的信息去发起一个长轮询监听请求。 Slf4j public class BlossomConfigChangeEventSubscriber {private ConfigService configService;private Properties properties;private Publish publish;public BlossomConfigChangeEventSubscriber(ConfigService configService, BlossomConfigChangePublisher publisher) {this.configService configService;this.properties configService.getProperties();this.publish publisher;}PostConstructpublic void listen() {//得到当前项目所有生效的配置ListBlossomConfigProperties.BlossomConfig configLists (ListBlossomConfigProperties.BlossomConfig) this.properties.get(BlossomConfigPropertiesKeyConstants.CONFIG_LISTS);if (configLists.isEmpty()) {return;}//对这些配置进行遍历为他们添加监听器//使得这些配置发生变更之后我能监听到对应的事件 从而对这些事件进行处理configLists.stream().forEach(config - {this.configService.subscribeConfigChangeEvent(config.getGroup(),config.getConfigId(),this.publish);});}}而这里的subscribeConfigChangeEvent就是Client端实现的代码。 那么这里我们想要让Core项目能知道配置变更那么我们只要确保我们的Core模块提供的Publish实现能发送事件即可。 public class BlossomConfigChangePublisher extends AbstractConfigChangePublish {private final ApplicationEventPublisher applicationEventPublisher;public BlossomConfigChangePublisher(ApplicationEventPublisher applicationEventPublisher) {this.applicationEventPublisher applicationEventPublisher;}Overridepublic void publishRemoveEvent(String key) {BlossomConfigChangeEvent event new BlossomConfigChangeEvent(this, key);applicationEventPublisher.publishEvent(event);}Overridepublic void publishPublishEvent(String key, ConfigCache configCache) {BlossomConfigChangeEvent event new BlossomConfigChangeEvent(this, key, configCache);applicationEventPublisher.publishEvent(event);} }这样子一旦我们的Core模块能监听到事件那么如何刷新Value注解对应的值其实就简单了我们在文章开篇就已经讲解了。 这里我们来看看监听器的实现即可。 package blossom.project.config.core;import blossom.project.config.common.exception.BlossomException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct; import java.io.IOException; import java.util.Collections; import java.util.List;import static blossom.project.config.common.constants.BlossomConstants.SEPARATOR;/*** author: ZhangBlossom* date: 2023/12/30 22:49* contact: QQ:4602197553* contact: WX:qczjhczs0114* blog: https://blog.csdn.net/Zhangsama1* github: https://github.com/ZhangBlossom*/ Slf4j Component public class BlossomConfigChangeListener implements ApplicationListenerBlossomConfigChangeEvent {private static final String REFRESH_SCOPE RefreshScope;Autowiredprivate ConfigurableApplicationContext applicationContext;private BeanDefinitionRegistry beanDefinitionRegistry;Autowiredprivate Environment environment;PostConstructpublic void init() {ScopeRegistry scopeRegistry applicationContext.getBean(ScopeRegistry.class);this.beanDefinitionRegistry scopeRegistry.getBeanDefinitionRegistry();}Overridepublic void onApplicationEvent(BlossomConfigChangeEvent event) {// 处理配置更改事件if (event.getConfigCache() ! null) {// 处理发布事件System.out.println(Config published: event.getKey());doPublishEvent(event);} else {// 处理删除事件System.out.println(Config removed: event.getKey());doRemoveEvent(event);}}/*** 处理配置变更事件** param event*/private void doPublishEvent(BlossomConfigChangeEvent event) {//1根据event中的key 找到对应的配置String key event.getKey();//2根据新的content信息解析完毕之后重新添加到Environemnt中String content event.getConfigCache().getContent();//得到文件解析格式String type event.getConfigCache().getType();ListPropertySource? newPropertySources Collections.emptyList();//得到配置文件的内容if (StringUtils.isBlank(content)) {log.warn(the data from ConfigService is empty, key:{}, key);return;}//在spring中想要将配置解析为PropertySource可以用自带的解析器--只提供了yaml和properties//也就是说json/xml等其他格式需要自己实现String configId parseKey(key);try {newPropertySources BlossomConfigDataHandler.getInstance().parseConfigData(configId, content, type);} catch (IOException e) {throw new RuntimeException(e);}// 将新的PropertySource添加到Environment中for (PropertySource? propertySource : newPropertySources) {((ConfigurableEnvironment) environment).getPropertySources().addFirst(propertySource);}// 触发环境变更事件以刷新Value注解的值applicationContext.publishEvent(new EnvironmentChangeEvent(applicationContext, Collections.singleton(event.getKey())));// 刷新带有RefreshScope注解的BeanrefreshScopedBeans();}/*** 刷新Value注解的值*/private void refreshScopedBeans() {String[] beanDefinitionNames applicationContext.getBeanDefinitionNames();for (String beanDefinitionName : beanDefinitionNames) {BeanDefinition beanDefinition beanDefinitionRegistry.getBeanDefinition(beanDefinitionName);if (REFRESH_SCOPE.equalsIgnoreCase(beanDefinition.getScope())) {applicationContext.getBeanFactory().destroyScopedBean(beanDefinitionName);applicationContext.getBean(beanDefinitionName);}}}/*** 处理配置删除事件** param event*/private void doRemoveEvent(BlossomConfigChangeEvent event) {//1删除对应的Environment//2: 不刷新Value}/*** 根据key返回configid* param key* return*/private String parseKey(String key){return key.substring(key.lastIndexOf(SEPARATOR));}}在上面的Listener代码中我们就顺利的完成了Core模块对Client模块的整合完成了变更事件的监听以及变更事件的处理。 代码比较好理解不做过多的解释了。 其实完成上面的代码之后一个非常简易的配置中心就做完了上面的代码已经可以完成配置的加载和变更了。 如果代码只是写到这里那么这个项目也只是类似于一个Demo帮助我们了解Spring与配置中心的关系。 但是亮点并不多只能说帮助你和面试官聊天的时候聊到这一块有一些说辞。 所以我打算在上面的版本中进行一下简单的优化用上一些”花里胡哨“的功能。 什么是配置中心以及如何实现一个配置中心 SpringBoot如何实现配置的管控 SpringCloud项目是如何对bootstrap配置文件进行加载的 Nacos是如何实现配置文件的读取加载的 开发配置中心前必须了解的前置知识 配置中心Server和Client端代码的编写 配置中心Core核心功能代码的编写 配置中心源码优化—本地缓存与读写锁
http://www.zqtcl.cn/news/236268/

相关文章:

  • 网站建设使用虚拟主机的优点与缺点马蹄室内设计论坛
  • 像wordpress一样的网站wordpress提示数据库出错
  • 优化网站首页新项目首码对接平台
  • 一个完整的网站建设过程wordpress企业中文模板下载
  • 我做微信淘宝客网站有哪些在公司做网站是什么职位
  • 湖南网站建设公司 干净磐石网络网站开发技术可行性分析怎么写
  • 石家庄制作网站网络推广与优化
  • 建站平台免代码可以自己设计图案的软件
  • 中山网站开发公司事业单位 网站备案
  • wordpress 做用户登录seo快速推广窍门大公开
  • php网站开发试卷做外贸一般在哪个网站
  • 有哪些网站的搜索引擎网站设计需要需要用
  • 网站建设有哪些需求徐州百度运营中心
  • 怎么做电脑网站后台杭州网站制作公司排名
  • 济南手机网站定制价格wordpress前端登陆 投稿
  • 建设企业网站的原因网站开发项目经验和教训
  • 京东网站的建设与发展现状分析手机与pc网站同步模板
  • 枣阳网站建设吧yw最新域名备案查询
  • 西安知名网站建设公司什么网站可以分享wordpress
  • ugc网站开发2022恢复线下教学通知
  • 网站界面尺寸珠海网站建设制作哪家专业
  • 上海闸北城市建设有限公司网站想学设计没有基础怎么办
  • 免费微网站哪个好用在线网页代理浏览免费
  • 广州建设专业网站寻找做项目的网站
  • 湛江网站建设方案服务卖wordpress主题
  • 扬州做网站的网络公司wordpress设置登录背景图片
  • 有哪些好的网站建设移动网站建设解决方案
  • 江苏网站建设公司排名高校门户网站建设问题
  • 网站怎么加关键词视频网站程序
  • 建设网站需要的人员及资金建设一个网站的具体流程