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

做网站哪里买空间好百度竞价推广流程

做网站哪里买空间好,百度竞价推广流程,网站收录没图片,青岛网站建设和优化背景: 最近调研使用k8s的ConfigMap来作为springboot项目的配置中心,需要实现热更新机制,避免pod重启影响业务。 ConfigMap作为挂载卷使用的时候可以更新pod中的配置内容,但是业务应用需要能监听并处理这些变更。我在测试的时候已…

背景:

最近调研使用k8s的ConfigMap来作为springboot项目的配置中心,需要实现热更新机制,避免pod重启影响业务。

ConfigMap作为挂载卷使用的时候可以更新pod中的配置内容,但是业务应用需要能监听并处理这些变更。我在测试的时候已经可以看到pod中的ConfigMap配置更新了,但是业务应用始终没有刷新配置。参考网上各位大神的关于spring-cloud-starter-kubernetes-config的配置,一直未能实现业务配置热更新,k8s在v1.19之后已经改为其他方式了,其他开源方案过于复杂,遂改换思路,简单点,就用java最原始的文件变更监听来手动刷新配置。

相关环境:

macOS: bigsur 11.7.8
docker desktop: 4.22.0 
docker engine: 24.0.5
kubernetes: 1.27.2
openjdk: 17.0.2
spring-boot:2.7.10
spring-cloud-context:3.1.1

实现:

1、引入依赖

主要依赖common-io对文件的监听和springcloud刷新上下文

<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.16.0</version>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-context</artifactId><version>3.1.1</version>
</dependency>

完整依赖如下 

<modelVersion>4.0.0</modelVersion>
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.10</version>
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
<properties><java.version>17</java.version>
</properties>
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator-autoconfigure</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.16.0</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-context</artifactId><version>3.1.1</version></dependency>
</dependencies>

2、文件监听器

在项目启动后开启监听

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;import java.io.File;@Slf4j
@Service
public class ConfigMapFileMonitor implements CommandLineRunner {@Autowiredprivate ConfigMapFileListener configFileListener;@Overridepublic void run(String... args) throws Exception {log.info("启动configMap文件监听...");// configMap挂载路径mountPathString fileDir = "/app/config/";FileAlterationMonitor monitor = new FileAlterationMonitor(1000);FileAlterationObserver observer = new FileAlterationObserver(new File(fileDir));observer.addListener(configFileListener);monitor.addObserver(observer);monitor.start();log.info("configMap文件监听开始...");}}

3、文件变更处理逻辑

主要用到Springcloud的ContextRefresher.refresh()方法,可能有的配置不需要更新,这里就需要根据实际业务逻辑来决定要更新哪些配置了。

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.stereotype.Service;import java.io.File;
import java.util.concurrent.Executors;@Slf4j
@Service
public class ConfigMapFileListener extends FileAlterationListenerAdaptor {@Qualifier("configDataContextRefresher")@Autowiredprivate ContextRefresher contextRefresher;@Overridepublic void onFileChange(File file) {log.info("configMap文件变更,异步刷新上下文...");Executors.newSingleThreadExecutor().execute(() -> {contextRefresher.refresh();log.info("异步刷新上下文完成。");});}
}

4、配置类

需要更新的配置使用配置类加@RefreshScope注解,@Value的方式无法直接更新

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;@Data
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DatabaseConfig {private String url;private String username;private String password;
}

类似以下方式的配置无法直接更新,可能需要增加一些逻辑,自行处理吧。。。

@Value("${dfs.console.server}")
private String dfsConsoleServer;

补充下,刷新配置不是修改后立即执行的,是有时间间隔的,可以配置,自行研究吧

其他

如果在运行过程中遇到如下错误,需要在k8s中添加相应权限:

o.s.cloud.kubernetes.StandardPodUtils    : Failed to get pod with name:[sdk-test-7b9dd4f586-69vql]. You should look into this if things aren't working as you expect. Are you missing serviceaccount permissions?
io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://10.96.0.1/api/v1/namespaces/default/pods/sdk-test-7b9dd4f586-69vql. Message: Forbidden!Configured service account doesn't have access. Service account may have been revoked. pods "sdk-test-7b9dd4f586-69vql" is forbidden: User "system:serviceaccount:default:default" cannot get resource "pods" in API group "" in the namespace "default".at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:570) ~[kubernetes-client-4.13.2.jar:na]

 使用如下代码可以监听到配置变更事件,可以针对具体业务看看有没有用吧。

@EventListener
public void envListener(EnvironmentChangeEvent event) {System.out.println("conf change: " + event);
}

参考资料:

spring-cloud-kubernetes 实战 二 configmap_spring-cloud-starter-kubernetes-config github-CSDN博客

spring-cloud-kubernetes自动同步k8s的configmap更新_fabric8 更新configmap-CSDN博客

Spring boot实现更改配置文件后自动更新配置-CSDN博客

SpringBoot基础篇配置信息之配置刷新-腾讯云开发者社区-腾讯云 (tencent.com)

http://www.hengruixuexiao.com/news/33708.html

相关文章:

  • 专业模板网站制作建网站的软件有哪些
  • wordpress 分类子类seo点击工具
  • 网站是什么的集合高清网站推广免费下载
  • python爬虫windows优化大师绿色版
  • 网站建设遵循原则关键词有哪些关联词
  • 加盟手机网站源码南宁优化网站网络服务
  • 网站服务器在哪网页设计参考网站
  • 提供网站制作公司万能搜索引擎网站
  • 在自己网站做支付可以吗直销怎么做才最快成功
  • 做核酸收费seo如何快速排名百度首页
  • 做农宿的网站昆明seo外包
  • 做的网站必须备案自己的网站怎么在百度上面推广
  • 商城网站如何提高收录郑州seo
  • 网站logo优化免费卖货平台
  • 电子商务网站建设 课后答案友情链接分析
  • 如何网站做外贸生意印度疫情最新消息
  • eclipse做企业网站汽油价格最新调整最新消息
  • 合肥餐饮网站建设seo效果最好的是
  • 网站和新媒体建设审批制度单页站好做seo吗
  • 宝应网站建设二级子域名ip地址查询
  • 收费下载的wordpress网站做网站好的网站建设公司
  • 公众号免费模板seo高效优化
  • 网站漂浮广告怎么做国内免费发布产品的平台
  • 网站做销售是斤么工作网络营销的真实案例分析
  • 淘宝网站怎么做的好坏创意设计
  • wordpress建站多用户搜索引擎调词平台
  • 盗取dede系统做的网站模板拓客引流推广
  • 衡水网站建设格公司强化防疫指导
  • 深圳制作网站软件搜索网站排行榜
  • 加强局网站建设it培训班出来工作有人要么