From b9e243d93c1e73ec3f9078468e1abcbce840d426 Mon Sep 17 00:00:00 2001 From: "lingxiao.wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Thu, 2 Mar 2023 19:47:01 +0800 Subject: [PATCH] feature:add User-Agent:polaris for healthyCheck api. (#859) Co-authored-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../registry/PolarisServiceRegistry.java | 7 ++++- .../cloud/polaris/util/OkHttpUtil.java | 4 +++ .../registry/PolarisServiceRegistryTest.java | 30 +++++++++++++++++++ .../cloud/polaris/util/OkHttpUtilTest.java | 5 ++-- 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98846b63e..8b696927b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [docs:Add license checker GitHub Action.](https://github.com/Tencent/spring-cloud-tencent/pull/839) - [refactor:move loadbalancer to discovery module.](https://github.com/Tencent/spring-cloud-tencent/pull/844) - [fix:fix the error capture of rate limit exception.](https://github.com/Tencent/spring-cloud-tencent/pull/854) +- [feature:add User-Agent:polaris for healthyCheck api.](https://github.com/Tencent/spring-cloud-tencent/pull/859) - [feat:enable stat reporting as default.](https://github.com/Tencent/spring-cloud-tencent/pull/862) - [optimize:optimize ServiceRuleManager.](https://github.com/Tencent/spring-cloud-tencent/pull/869) - [refactor:update to junit 5.](https://github.com/Tencent/spring-cloud-tencent/pull/865) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 10c29d0b9..22fa2f93d 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -18,6 +18,8 @@ package com.tencent.cloud.polaris.registry; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -45,6 +47,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.http.HttpHeaders; import static java.util.concurrent.TimeUnit.SECONDS; import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; @@ -224,7 +227,9 @@ public class PolarisServiceRegistry implements ServiceRegistry headers = new HashMap<>(1); + headers.put(HttpHeaders.USER_AGENT, "polaris"); + if (!OkHttpUtil.get(healthCheckUrl, headers)) { LOGGER.error("backend service health check failed. health check endpoint = {}", healthCheckEndpoint); return; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java index 1839a3101..f8580ee19 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java @@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -60,6 +61,9 @@ public final class OkHttpUtil { conn.setRequestMethod("GET"); conn.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(2)); conn.setReadTimeout((int) TimeUnit.SECONDS.toMillis(2)); + if (!CollectionUtils.isEmpty(headers)) { + headers.forEach(conn::setRequestProperty); + } BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuffer buffer = new StringBuffer(); String str; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java index c0c740fa3..0fbdf050c 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java @@ -61,6 +61,19 @@ public class PolarisServiceRegistryTest { .withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST) .withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx"); + private final WebApplicationContextRunner contextRunner2 = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + PolarisContextAutoConfiguration.class, + PolarisPropertiesConfiguration.class, + PolarisDiscoveryClientConfiguration.class, + PolarisDiscoveryAutoConfiguration.class)) + .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) + .withPropertyValues("server.port=" + PORT) + .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") + .withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST) + .withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx") + .withPropertyValues("spring.cloud.polaris.discovery.health-check-url=/test"); + @BeforeAll static void beforeAll() throws Exception { namingServer = NamingServer.startNamingServer(10081); @@ -111,6 +124,23 @@ public class PolarisServiceRegistryTest { }); } + @Test + void testHeartbeat() { + this.contextRunner2.run(context -> { + PolarisServiceRegistry registry = context.getBean(PolarisServiceRegistry.class); + PolarisRegistration registration = Mockito.mock(PolarisRegistration.class); + when(registration.getHost()).thenReturn("127.0.0.1"); + when(registration.getPort()).thenReturn(8080); + when(registration.getServiceId()).thenReturn(SERVICE_PROVIDER); + + assertThatCode(() -> registry.register(registration)).doesNotThrowAnyException(); + + Thread.sleep(6000); + + assertThatCode(() -> registry.deregister(registration)).doesNotThrowAnyException(); + }); + } + @Configuration @EnableAutoConfiguration static class PolarisPropertiesConfiguration { diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/util/OkHttpUtilTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/util/OkHttpUtilTest.java index 2bf269bb5..7590b8b1e 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/util/OkHttpUtilTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/util/OkHttpUtilTest.java @@ -26,6 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; import static org.assertj.core.api.Assertions.assertThat; @@ -53,8 +54,8 @@ public class OkHttpUtilTest { @RestController static class TestApplication { @GetMapping("/test") - public String test() { - return "test"; + public String test(@RequestHeader("key") String key) { + return key; } } }