From 494fd876c8cb13d96c9b95be03a98705ab0c93e1 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Mon, 14 Nov 2022 22:16:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?perf:=20job-gateway=E4=BB=8EESB=E8=8E=B7?= =?UTF-8?q?=E5=8F=96JWT=E5=85=AC=E9=92=A5=E5=A2=9E=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E8=AF=95=20#786?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/EsbJwtServiceImpl.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java b/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java index d02e105cb0..0f7f415926 100644 --- a/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java +++ b/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java @@ -26,6 +26,7 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.tencent.bk.job.common.util.ThreadUtils; import com.tencent.bk.job.gateway.model.esb.EsbJwtInfo; import com.tencent.bk.job.gateway.service.EsbJwtPublicKeyService; import com.tencent.bk.job.gateway.service.EsbJwtService; @@ -59,22 +60,48 @@ public class EsbJwtServiceImpl implements EsbJwtService { .maximumSize(99999).expireAfterWrite(30, TimeUnit.SECONDS).build(); @Autowired - public EsbJwtServiceImpl(EsbJwtPublicKeyService esbJwtPublicKeyService){ + public EsbJwtServiceImpl(EsbJwtPublicKeyService esbJwtPublicKeyService) { this.esbJwtPublicKeyService = esbJwtPublicKeyService; - getAndCachePublicKey(); + getPublicKeyOrRetryInBackground(); } - private void getAndCachePublicKey() { + private void getPublicKeyOrRetryInBackground() { + boolean publicKeyGotten = tryToGetAndCachePublicKeyOnce(); + if (!publicKeyGotten) { + Thread esbPublicKeyGetter = new Thread(() -> { + boolean keyGotten; + int retryCount = 0; + int sleepMillsOnce = 5000; + // 最多重试一整天 + int maxRetryCount = 24 * 3600 / 5; + do { + log.warn("esbJwtPublicKey not gotten, retry {} after 5s", ++retryCount); + ThreadUtils.sleep(sleepMillsOnce); + keyGotten = tryToGetAndCachePublicKeyOnce(); + } while (!keyGotten && retryCount <= maxRetryCount); + if (!keyGotten) { + log.error("esbJwtPublicKey not gotten after {} retry, plz check esb", maxRetryCount); + } + }); + esbPublicKeyGetter.setDaemon(true); + esbPublicKeyGetter.setName("esbPublicKeyGetter"); + esbPublicKeyGetter.start(); + } + } + + private boolean tryToGetAndCachePublicKeyOnce() { try { String esbJwtPublicKey = esbJwtPublicKeyService.getEsbJWTPublicKey(); if (StringUtils.isEmpty(esbJwtPublicKey)) { log.error("Esb jwt public key is not configured!"); - return; + return false; } this.publicKey = buildPublicKey(esbJwtPublicKey); + return true; } catch (Throwable e) { // Catch all exception log.error("Build esb jwt public key caught error!", e); + return false; } } From 88a1b7fa60c0fd94aaad71dbd31fc1f3b2e16f73 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Mon, 14 Nov 2022 22:23:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?perf:=20job-gateway=E4=BB=8EESB=E8=8E=B7?= =?UTF-8?q?=E5=8F=96JWT=E5=85=AC=E9=92=A5=E5=A2=9E=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E8=AF=95=20#786?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/EsbJwtServiceImpl.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java b/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java index 0f7f415926..88e4d12b59 100644 --- a/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java +++ b/src/backend/job-gateway/src/main/java/com/tencent/bk/job/gateway/service/impl/EsbJwtServiceImpl.java @@ -67,26 +67,27 @@ public EsbJwtServiceImpl(EsbJwtPublicKeyService esbJwtPublicKeyService) { private void getPublicKeyOrRetryInBackground() { boolean publicKeyGotten = tryToGetAndCachePublicKeyOnce(); - if (!publicKeyGotten) { - Thread esbPublicKeyGetter = new Thread(() -> { - boolean keyGotten; - int retryCount = 0; - int sleepMillsOnce = 5000; - // 最多重试一整天 - int maxRetryCount = 24 * 3600 / 5; - do { - log.warn("esbJwtPublicKey not gotten, retry {} after 5s", ++retryCount); - ThreadUtils.sleep(sleepMillsOnce); - keyGotten = tryToGetAndCachePublicKeyOnce(); - } while (!keyGotten && retryCount <= maxRetryCount); - if (!keyGotten) { - log.error("esbJwtPublicKey not gotten after {} retry, plz check esb", maxRetryCount); - } - }); - esbPublicKeyGetter.setDaemon(true); - esbPublicKeyGetter.setName("esbPublicKeyGetter"); - esbPublicKeyGetter.start(); + if (publicKeyGotten) { + return; } + Thread esbPublicKeyGetter = new Thread(() -> { + boolean keyGotten; + int retryCount = 0; + int sleepMillsOnce = 5000; + // 最多重试3天 + int maxRetryCount = 3 * 24 * 3600 / 5; + do { + log.warn("esbJwtPublicKey not gotten, retry {} after 5s", ++retryCount); + ThreadUtils.sleep(sleepMillsOnce); + keyGotten = tryToGetAndCachePublicKeyOnce(); + } while (!keyGotten && retryCount <= maxRetryCount); + if (!keyGotten) { + log.error("esbJwtPublicKey not gotten after {} retry (3 days), plz check esb", maxRetryCount); + } + }); + esbPublicKeyGetter.setDaemon(true); + esbPublicKeyGetter.setName("esbPublicKeyGetter"); + esbPublicKeyGetter.start(); } private boolean tryToGetAndCachePublicKeyOnce() {