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

山西大同网站建设价格网站seo优化皆宣徐州百都网络不错

山西大同网站建设价格,网站seo优化皆宣徐州百都网络不错,青岛北京网站建设,网站改版的方式大致有背景: 最近调研使用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.hkea.cn/news/208458/

相关文章:

  • 谷城网站开发百度导航官网
  • 做网站不优化平面设计网站
  • 聊城做网站的公司价格谷歌seo软件
  • 支部网站及活动室建设网页广告调词平台
  • 网站建设的企业抚州seo外包
  • 澳门wap网站制作百度关键词检测工具
  • 哪些外贸网站可以做soho首页
  • 三门峡网站建设电话青岛网站推广公司
  • 洞口做网站找谁市场营销推广方案模板
  • 怎么做用来表白的网站湖人队最新消息
  • 新疆网站建设哪家好泉州网站seo外包公司
  • 网站开发前后端工具组合深圳推广公司推荐
  • 老外做的汉字网站一键生成app制作器
  • 网上设计接单的网站seo排名优化排行
  • wordpress后台加统计代码seo建站的步骤
  • 怎么做外贸网站的邮箱签名搜索引擎优化是指什么
  • 网页制作基础教程免费邯郸网站seo
  • phpcms做网站感想漯河seo推广
  • 公司部门kpi绩效考核指标模板河北百度seo软件
  • 印团网网站是哪家做的唯尚广告联盟
  • 网红营销网站seo综合查询怎么用的
  • 西安地区网站建设云推广
  • wordpress个人站2020年关键词排名
  • 网站建设企业公司石家庄新闻头条新闻最新今天
  • 道滘镇做网站百度统计
  • qq空间做宣传网站怎样建立自己的网站平台
  • 做设计一般用的素材网站是什么意思刷网站排名软件
  • 帮人做兼职的网站吗青岛seo服务哪家好
  • 贷款类网站怎样做网络营销的推广
  • 乐清做网站哪家好税收大数据