feature:add User-Agent:polaris for healthyCheck api.

pull/859/head
lingxiao-wu 3 years ago
parent 2d88b7f799
commit ae429230e4

@ -18,6 +18,8 @@
package com.tencent.cloud.polaris.registry;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -40,6 +42,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;
@ -200,7 +203,9 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
String healthCheckUrl = String.format("http://%s:%s%s", heartbeatRequest.getHost(),
heartbeatRequest.getPort(), healthCheckEndpoint);
if (!OkHttpUtil.get(healthCheckUrl, null)) {
Map<String, String> 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;

@ -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;

@ -26,6 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
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;
}
}
}

Loading…
Cancel
Save