From 9ca77727bfae9e8ce09eba0011e2a85ff6dae1d2 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Tue, 15 Aug 2023 17:41:27 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AE=B9=E5=99=A8=E5=8C=96=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E6=94=AF=E6=8C=81=E6=97=A0=E6=8D=9F=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=20#2338?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. k8s环境下支持停止接收流量后延迟一段时间关闭Spring容器,用于无损更新/扩缩容; 2. 基础镜像增加网络调试相关工具。 --- ...licationAvailabilityAutoConfiguration.java | 15 ++++++++++++ .../main/resources/META-INF/spring.factories | 2 ++ .../JobApplicationAvailabilityBean.java | 23 +++++++++++++++++++ ...licationAvailabilityAutoConfiguration.java | 16 +++++++++++++ .../main/resources/META-INF/spring.factories | 3 ++- .../analysis/JobAnalysisBootApplication.java | 4 +++- .../job/backup/JobBackupBootApplication.java | 4 +++- .../job/config/JobConfigBootApplication.java | 4 +++- .../crontab/JobCrontabBootApplication.java | 4 +++- .../execute/JobExecuteBootApplication.java | 4 +++- .../JobFileGatewayBootApplication.java | 4 +++- .../gateway/JobGatewayBootApplication.java | 4 +++- .../bk/job/logsvr/JobLogBootApplication.java | 4 +++- .../job/manage/JobManageBootApplication.java | 4 +++- .../images/backend/backend.Dockerfile | 2 +- .../kubernetes/images/backend/jdk.Dockerfile | 4 ++-- .../kubernetes/images/backend/os.Dockerfile | 4 +++- 17 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 src/backend/commons/common-consul/src/main/java/com/tencent/bk/job/common/consul/config/JobApplicationAvailabilityAutoConfiguration.java create mode 100644 src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/availability/JobApplicationAvailabilityBean.java create mode 100644 src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/config/JobApplicationAvailabilityAutoConfiguration.java diff --git a/src/backend/commons/common-consul/src/main/java/com/tencent/bk/job/common/consul/config/JobApplicationAvailabilityAutoConfiguration.java b/src/backend/commons/common-consul/src/main/java/com/tencent/bk/job/common/consul/config/JobApplicationAvailabilityAutoConfiguration.java new file mode 100644 index 0000000000..d27074aada --- /dev/null +++ b/src/backend/commons/common-consul/src/main/java/com/tencent/bk/job/common/consul/config/JobApplicationAvailabilityAutoConfiguration.java @@ -0,0 +1,15 @@ +package com.tencent.bk.job.common.consul.config; + +import org.springframework.boot.availability.ApplicationAvailabilityBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +public class JobApplicationAvailabilityAutoConfiguration { + + @Bean + public ApplicationAvailabilityBean applicationAvailability() { + return new ApplicationAvailabilityBean(); + } + +} diff --git a/src/backend/commons/common-consul/src/main/resources/META-INF/spring.factories b/src/backend/commons/common-consul/src/main/resources/META-INF/spring.factories index e69de29bb2..e042a78bea 100644 --- a/src/backend/commons/common-consul/src/main/resources/META-INF/spring.factories +++ b/src/backend/commons/common-consul/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.tencent.bk.job.common.consul.config.JobApplicationAvailabilityAutoConfiguration diff --git a/src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/availability/JobApplicationAvailabilityBean.java b/src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/availability/JobApplicationAvailabilityBean.java new file mode 100644 index 0000000000..c6d6e9048e --- /dev/null +++ b/src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/availability/JobApplicationAvailabilityBean.java @@ -0,0 +1,23 @@ +package com.tencent.bk.job.common.k8s.availability; + +import com.tencent.bk.job.common.util.ThreadUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.availability.ApplicationAvailabilityBean; +import org.springframework.boot.availability.AvailabilityChangeEvent; +import org.springframework.boot.availability.ReadinessState; + +@Slf4j +public class JobApplicationAvailabilityBean extends ApplicationAvailabilityBean { + @Override + public void onApplicationEvent(AvailabilityChangeEvent event) { + super.onApplicationEvent(event); + if (ReadinessState.REFUSING_TRAFFIC == event.getState()) { + // SpringCloud负载均衡缓存默认为35s,等待调用方缓存刷新后再真正关闭Spring容器 + int waitSeconds = 35; + while (waitSeconds > 0) { + ThreadUtils.sleep(1000); + log.info("wait for GracefulShutdown, {}s left", waitSeconds--); + } + } + } +} diff --git a/src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/config/JobApplicationAvailabilityAutoConfiguration.java b/src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/config/JobApplicationAvailabilityAutoConfiguration.java new file mode 100644 index 0000000000..78b9534e33 --- /dev/null +++ b/src/backend/commons/common-k8s/src/main/java/com/tencent/bk/job/common/k8s/config/JobApplicationAvailabilityAutoConfiguration.java @@ -0,0 +1,16 @@ +package com.tencent.bk.job.common.k8s.config; + +import com.tencent.bk.job.common.k8s.availability.JobApplicationAvailabilityBean; +import org.springframework.boot.availability.ApplicationAvailabilityBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +public class JobApplicationAvailabilityAutoConfiguration { + + @Bean + public ApplicationAvailabilityBean applicationAvailability() { + return new JobApplicationAvailabilityBean(); + } + +} diff --git a/src/backend/commons/common-k8s/src/main/resources/META-INF/spring.factories b/src/backend/commons/common-k8s/src/main/resources/META-INF/spring.factories index 7a3edcd59c..de59831050 100644 --- a/src/backend/commons/common-k8s/src/main/resources/META-INF/spring.factories +++ b/src/backend/commons/common-k8s/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.tencent.bk.job.common.k8s.config.K8sServiceInfoServiceAutoConfig +com.tencent.bk.job.common.k8s.config.K8sServiceInfoServiceAutoConfig,\ +com.tencent.bk.job.common.k8s.config.JobApplicationAvailabilityAutoConfiguration diff --git a/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/JobAnalysisBootApplication.java b/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/JobAnalysisBootApplication.java index 6b350c53e5..f8ae6d1f41 100644 --- a/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/JobAnalysisBootApplication.java +++ b/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/JobAnalysisBootApplication.java @@ -27,6 +27,7 @@ import com.tencent.bk.job.common.config.FeatureToggleConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; @@ -34,7 +35,8 @@ import org.springframework.context.annotation.DependsOn; import org.springframework.scheduling.annotation.EnableScheduling; -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class, + ApplicationAvailabilityAutoConfiguration.class}) @EnableCaching @EnableFeignClients @EnableScheduling diff --git a/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/JobBackupBootApplication.java b/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/JobBackupBootApplication.java index 55d0de443d..e4ee2e79f7 100644 --- a/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/JobBackupBootApplication.java +++ b/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/JobBackupBootApplication.java @@ -27,6 +27,7 @@ import com.tencent.bk.job.common.config.FeatureToggleConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; @@ -37,7 +38,8 @@ /** * @since 21/7/2020 10:55 */ -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class, + ApplicationAvailabilityAutoConfiguration.class}) @EnableCaching @EnableFeignClients @EnableScheduling diff --git a/src/backend/job-config/src/main/java/com/tencent/bk/job/config/JobConfigBootApplication.java b/src/backend/job-config/src/main/java/com/tencent/bk/job/config/JobConfigBootApplication.java index 5c145a8df8..8309a6b3ff 100644 --- a/src/backend/job-config/src/main/java/com/tencent/bk/job/config/JobConfigBootApplication.java +++ b/src/backend/job-config/src/main/java/com/tencent/bk/job/config/JobConfigBootApplication.java @@ -26,12 +26,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.cloud.config.server.EnableConfigServer; /** * @date 2019/09/20 */ -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job") +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = + {ApplicationAvailabilityAutoConfiguration.class}) @EnableConfigServer public class JobConfigBootApplication { diff --git a/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/JobCrontabBootApplication.java b/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/JobCrontabBootApplication.java index 0d5ca00673..84a8357093 100644 --- a/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/JobCrontabBootApplication.java +++ b/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/JobCrontabBootApplication.java @@ -28,6 +28,7 @@ import com.tencent.bk.job.crontab.config.JobQuartzProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.openfeign.EnableFeignClients; @@ -35,7 +36,8 @@ /** * @date 2019/09/20 */ -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class, + ApplicationAvailabilityAutoConfiguration.class}) @EnableFeignClients @EnableConfigurationProperties({FeatureToggleConfig.class, JobQuartzProperties.class}) public class JobCrontabBootApplication { diff --git a/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/JobExecuteBootApplication.java b/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/JobExecuteBootApplication.java index 065e9f81f5..3864a43650 100644 --- a/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/JobExecuteBootApplication.java +++ b/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/JobExecuteBootApplication.java @@ -27,13 +27,15 @@ import com.tencent.bk.job.common.config.FeatureToggleConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class, + ApplicationAvailabilityAutoConfiguration.class}) @EnableDiscoveryClient @EnableFeignClients @EnableConfigurationProperties({FeatureToggleConfig.class}) diff --git a/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/JobFileGatewayBootApplication.java b/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/JobFileGatewayBootApplication.java index be284ada6f..09abc1ac5f 100644 --- a/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/JobFileGatewayBootApplication.java +++ b/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/JobFileGatewayBootApplication.java @@ -28,6 +28,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; @@ -35,7 +36,8 @@ import org.springframework.scheduling.annotation.EnableScheduling; @Slf4j -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class, + ApplicationAvailabilityAutoConfiguration.class}) @EnableCaching @EnableFeignClients @EnableScheduling diff --git a/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/JobGatewayBootApplication.java b/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/JobGatewayBootApplication.java index 3317859597..dd74f8e644 100644 --- a/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/JobGatewayBootApplication.java +++ b/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/JobGatewayBootApplication.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; @@ -43,7 +44,8 @@ /** * @date 2019/09/20 */ -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job") +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = + {ApplicationAvailabilityAutoConfiguration.class}) @EnableDiscoveryClient @Slf4j @EnableConfigurationProperties({FeatureToggleConfig.class}) diff --git a/src/backend/job-logsvr/boot-job-logsvr/src/main/java/com/tencent/bk/job/logsvr/JobLogBootApplication.java b/src/backend/job-logsvr/boot-job-logsvr/src/main/java/com/tencent/bk/job/logsvr/JobLogBootApplication.java index c1bb5cd5f9..1886969180 100644 --- a/src/backend/job-logsvr/boot-job-logsvr/src/main/java/com/tencent/bk/job/logsvr/JobLogBootApplication.java +++ b/src/backend/job-logsvr/boot-job-logsvr/src/main/java/com/tencent/bk/job/logsvr/JobLogBootApplication.java @@ -27,13 +27,15 @@ import com.tencent.bk.job.common.config.FeatureToggleConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @date 2019/09/20 */ -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job") +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = + {ApplicationAvailabilityAutoConfiguration.class}) @EnableFeignClients @EnableConfigurationProperties({FeatureToggleConfig.class}) public class JobLogBootApplication { diff --git a/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/JobManageBootApplication.java b/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/JobManageBootApplication.java index 08f4a2e0ce..24cfae7f25 100644 --- a/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/JobManageBootApplication.java +++ b/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/JobManageBootApplication.java @@ -27,13 +27,15 @@ import com.tencent.bk.job.common.config.FeatureToggleConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.scheduling.annotation.EnableScheduling; -@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = "com.tencent.bk.job", exclude = {RedisAutoConfiguration.class, + ApplicationAvailabilityAutoConfiguration.class}) @EnableConfigurationProperties({FeatureToggleConfig.class}) @EnableCaching @EnableFeignClients diff --git a/support-files/kubernetes/images/backend/backend.Dockerfile b/support-files/kubernetes/images/backend/backend.Dockerfile index 9c5199f0c8..b73fa152f6 100644 --- a/support-files/kubernetes/images/backend/backend.Dockerfile +++ b/support-files/kubernetes/images/backend/backend.Dockerfile @@ -1,4 +1,4 @@ -FROM bkjob/jdk:0.0.2 +FROM bkjob/jdk:0.0.3 LABEL maintainer="Tencent BlueKing Job" diff --git a/support-files/kubernetes/images/backend/jdk.Dockerfile b/support-files/kubernetes/images/backend/jdk.Dockerfile index 29c787b0ea..2a4790a18c 100644 --- a/support-files/kubernetes/images/backend/jdk.Dockerfile +++ b/support-files/kubernetes/images/backend/jdk.Dockerfile @@ -1,4 +1,4 @@ -FROM bkjob/os:0.0.1 +FROM bkjob/os:0.0.2 LABEL maintainer="Tencent BlueKing Job" @@ -10,4 +10,4 @@ RUN mkdir -p /data && \ rm -f TencentKona8.0.3.b2_jdk_linux-x86_64_8u262.tar.gz ENV JAVA_HOME=/data/TencentKona-8.0.3-262 ENV PATH=${JAVA_HOME}/bin:$PATH -ENV CLASSPATH=.:${JAVA_HOME}/lib \ No newline at end of file +ENV CLASSPATH=.:${JAVA_HOME}/lib diff --git a/support-files/kubernetes/images/backend/os.Dockerfile b/support-files/kubernetes/images/backend/os.Dockerfile index 1d165b19ed..3a9a106dd8 100644 --- a/support-files/kubernetes/images/backend/os.Dockerfile +++ b/support-files/kubernetes/images/backend/os.Dockerfile @@ -14,4 +14,6 @@ RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\ RUN yum install -y vim && \ yum install -y less && \ yum install -y wget && \ - yum install -y lrzsz \ No newline at end of file + yum install -y lrzsz && \ + yum install -y net-tools.x86_64 && \ + yum install -y bind-utils