From cfea9906dba238bc8da4a0d1080e13266f45698d Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Wed, 14 Aug 2024 18:01:40 +0800 Subject: [PATCH] fix: fix lossless deregister failed when no health-check configured (#1385) Co-authored-by: andrew shan <45474304+andrewshan@users.noreply.github.com> --- CHANGELOG.md | 3 +- .../src/main/resources/bootstrap.yml | 8 +- .../src/main/resources/bootstrap.yml | 14 +-- .../context/PolarisSDKContextManager.java | 119 +++++++++--------- 4 files changed, 70 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8f226fc9..6f820ba57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,4 +11,5 @@ - [fix: fix the lossless provider override in multiple registries scenario](https://github.com/Tencent/spring-cloud-tencent/pull/1380) - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1381) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1382) -- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1383) \ No newline at end of file +- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1383) +- [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1385) \ No newline at end of file diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml index cdd07b831..4e8c2a8ce 100644 --- a/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml @@ -20,8 +20,9 @@ spring: port: 28084 lossless: enabled: true - healthCheckPath: /lossless/callee/health - healthCheckInterval: 5000 + port: 28084 + #healthCheckPath: /actuator/health + #healthCheckInterval: 5000 lossless: healthy: delay: @@ -32,5 +33,4 @@ management: exposure: include: - polaris-discovery - - polaris-ratelimit - - polaris-config \ No newline at end of file + - health \ No newline at end of file diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml index 1487942e9..39d7f2c51 100644 --- a/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml @@ -6,23 +6,15 @@ spring: cloud: nacos: discovery: - server-addr: 9.134.5.52:8848 + server-addr: 127.0.0.1:8848 enabled: true - namespace: "test1" polaris: lossless: enabled: true - healthCheckPath: /lossless/nacos/callee/health + port: 28085 + healthCheckPath: /actuator/health healthCheckInterval: 5000 lossless: healthy: delay: second: 20 -management: - endpoints: - web: - exposure: - include: - - polaris-discovery - - polaris-ratelimit - - polaris-config \ No newline at end of file diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java index d84a000b0..743444a49 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java @@ -158,57 +158,7 @@ public class PolarisSDKContextManager { } public void init() { - if (null == serviceSdkContext) { - try { - // init SDKContext - serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList, - () -> environment.getProperty("spring.cloud.client.ip-address"), - () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); - serviceSdkContext.init(); - - // init ProviderAPI - providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(serviceSdkContext); - - // init losslessAPI - losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(serviceSdkContext); - - // init ConsumerAPI - consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(serviceSdkContext); - - // init RouterAPI - routerAPI = RouterAPIFactory.createRouterAPIByContext(serviceSdkContext); - - // init CircuitBreakAPI - circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(serviceSdkContext); - - // init LimitAPI - limitAPI = LimitAPIFactory.createLimitAPIByContext(serviceSdkContext); - - // init AssemblyAPI - assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(serviceSdkContext); - - // add shutdown hook - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - long startTimestamp = System.currentTimeMillis(); - long delay = 0; - while (true) { - if (!isRegistered || delay >= 60000) { - innerDestroy(); - break; - } - else { - delay = System.currentTimeMillis() - startTimestamp; - } - } - })); - LOG.info("create Polaris SDK context successfully. properties: {}", properties); - } - catch (Throwable throwable) { - LOG.error("create Polaris SDK context failed. properties: {}", properties, throwable); - throw throwable; - } - } - + initService(); initConfig(); } @@ -225,37 +175,37 @@ public class PolarisSDKContextManager { } public SDKContext getSDKContext() { - init(); + initService(); return serviceSdkContext; } public ProviderAPI getProviderAPI() { - init(); + initService(); return providerAPI; } public LosslessAPI getLosslessAPI() { - init(); + initService(); return losslessAPI; } public ConsumerAPI getConsumerAPI() { - init(); + initService(); return consumerAPI; } public RouterAPI getRouterAPI() { - init(); + initService(); return routerAPI; } public CircuitBreakAPI getCircuitBreakAPI() { - init(); + initService(); return circuitBreakAPI; } public LimitAPI getLimitAPI() { - init(); + initService(); return limitAPI; } @@ -268,6 +218,59 @@ public class PolarisSDKContextManager { return configSDKContext; } + public void initService() { + if (null == serviceSdkContext) { + try { + // init SDKContext + serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList, + () -> environment.getProperty("spring.cloud.client.ip-address"), + () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); + serviceSdkContext.init(); + + // init ProviderAPI + providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(serviceSdkContext); + + // init losslessAPI + losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(serviceSdkContext); + + // init ConsumerAPI + consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(serviceSdkContext); + + // init RouterAPI + routerAPI = RouterAPIFactory.createRouterAPIByContext(serviceSdkContext); + + // init CircuitBreakAPI + circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(serviceSdkContext); + + // init LimitAPI + limitAPI = LimitAPIFactory.createLimitAPIByContext(serviceSdkContext); + + // init AssemblyAPI + assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(serviceSdkContext); + + // add shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + long startTimestamp = System.currentTimeMillis(); + long delay = 0; + while (true) { + if (!isRegistered || delay >= 60000) { + innerDestroy(); + break; + } + else { + delay = System.currentTimeMillis() - startTimestamp; + } + } + })); + LOG.info("create Polaris SDK context successfully. properties: {}", properties); + } + catch (Throwable throwable) { + LOG.error("create Polaris SDK context failed. properties: {}", properties, throwable); + throw throwable; + } + } + } + public void initConfig() { // get modifiers for configuration. List configModifierList = new ArrayList<>();