diff --git a/CHANGELOG.md b/CHANGELOG.md index fab6bb7fd..e87c79fcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,3 +23,4 @@ - [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/805) - [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/812) - [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/820) +- [fix:fix router parameter not checking bug.](https://github.com/Tencent/spring-cloud-tencent/pull/828) diff --git a/pom.xml b/pom.xml index f079fbc46..c381102f0 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.9.0-Hoxton.SR12-SNAPSHOT + 1.9.0-Hoxton.SR12 Hoxton.SR12 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 eef636736..1839a3101 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 @@ -17,15 +17,18 @@ package com.tencent.cloud.polaris.util; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Map; -import java.util.Objects; +import java.util.concurrent.TimeUnit; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + /** * okhttp util. * @@ -38,53 +41,46 @@ public final class OkHttpUtil { */ public final static Logger LOGGER = LoggerFactory.getLogger(OkHttpUtil.class); - /** - * client. - */ - private final static OkHttpClient HTTP_CLIENT = new OkHttpClient(); - private OkHttpUtil() { } /** * get request. - * @param url url + * @param path path * @param headers headers * @return response */ - public static boolean get(String url, Map headers) { + public static boolean get(String path, Map headers) { + HttpURLConnection conn = null; try { - Request.Builder builder = new Request.Builder(); - buildHeader(builder, headers); - Request request = builder.url(url).build(); - Response response = HTTP_CLIENT.newCall(request).execute(); + URL url = new java.net.URL(path); + conn = (HttpURLConnection) url.openConnection(); - if (response.isSuccessful() && Objects.nonNull(response.body())) { - String result = response.body().string(); - LOGGER.debug("exec get request, url: {} success, response data: {}", url, result); + conn.setRequestMethod("GET"); + conn.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(2)); + conn.setReadTimeout((int) TimeUnit.SECONDS.toMillis(2)); + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + StringBuffer buffer = new StringBuffer(); + String str; + while ((str = reader.readLine()) != null) { + buffer.append(str); + } + String responseBody = buffer.toString(); + if (conn.getResponseCode() == 200 && StringUtils.hasText(responseBody)) { + LOGGER.debug("exec get request, url: {} success, response data: {}", url, responseBody); return true; } } catch (Exception e) { - LOGGER.error("exec get request, url: {} failed!", url, e); + LOGGER.error("exec get request, url: {} failed!", path, e); + return false; } - return false; - } - - /** - * build header. - * @param builder builder - * @param headers headers - */ - private static void buildHeader(Request.Builder builder, - Map headers) { - if (Objects.nonNull(headers) && headers.size() > 0) { - headers.forEach((k, v) -> { - if (Objects.nonNull(k) && Objects.nonNull(v)) { - builder.addHeader(k, v); - } - }); + finally { + if (null != conn) { + conn.disconnect(); + } } + return false; } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpoint.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpoint.java index a00281d0d..c47dcd928 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpoint.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpoint.java @@ -36,6 +36,7 @@ import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.Selector; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; /** * Router actuator endpoint. @@ -53,20 +54,21 @@ public class PolarisRouterEndpoint { @ReadOperation public Map router(@Selector String dstService) { - List routerRules = serviceRuleManager.getServiceRouterRule(MetadataContext.LOCAL_NAMESPACE, - MetadataContext.LOCAL_SERVICE, dstService); - Map result = new HashMap<>(); - List rules = new LinkedList<>(); - result.put("routerRules", rules); + if (StringUtils.hasText(dstService)) { + List routerRules = serviceRuleManager.getServiceRouterRule(MetadataContext.LOCAL_NAMESPACE, + MetadataContext.LOCAL_SERVICE, dstService); + List rules = new LinkedList<>(); + result.put("routerRules", rules); - if (CollectionUtils.isEmpty(routerRules)) { - return result; - } + if (CollectionUtils.isEmpty(routerRules)) { + return result; + } - for (RoutingProto.Route route : routerRules) { - rules.add(parseRouterRule(route)); + for (RoutingProto.Route route : routerRules) { + rules.add(parseRouterRule(route)); + } } return result; diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index eba15670f..84e1360da 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,10 +70,10 @@ - 1.9.0-Hoxton.SR12-SNAPSHOT + 1.9.0-Hoxton.SR12 - 1.10.2 + 1.10.5 31.0.1-jre 1.2.11 4.5.1 diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java index 06d71f65c..6ace65e70 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java @@ -34,6 +34,8 @@ import com.tencent.polaris.client.flow.FlowControlParam; import com.tencent.polaris.client.flow.ResourcesResponse; import com.tencent.polaris.client.pb.RateLimitProto; import com.tencent.polaris.client.pb.RoutingProto; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * the manager of service governance rules. for example: rate limit rule, router rules. @@ -42,8 +44,8 @@ import com.tencent.polaris.client.pb.RoutingProto; */ public class ServiceRuleManager { + private static final Logger LOG = LoggerFactory.getLogger(ServiceRuleManager.class); private final SDKContext sdkContext; - private final FlowControlParam controlParam; public ServiceRuleManager(SDKContext sdkContext) { @@ -55,6 +57,7 @@ public class ServiceRuleManager { } public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) { + LOG.debug("Get service rate limit rules with namespace:{} and service:{}.", namespace, service); ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(namespace, service), ServiceEventKey.EventType.RATE_LIMITING); @@ -76,6 +79,8 @@ public class ServiceRuleManager { } public List getServiceRouterRule(String namespace, String sourceService, String dstService) { + LOG.debug("Get service router rules with namespace:{} and sourceService:{} and dstService:{}.", + namespace, sourceService, dstService); Set routerKeys = new HashSet<>(); ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, dstService),