fix:fix router parameter not checking bug. (#826)

pull/832/head
Haotian Zhang 2 years ago committed by GitHub
parent beebcecb50
commit 465ce1bc11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -30,3 +30,4 @@
- [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/814) - [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/814)
- [Feature: upgrade framework version.](https://github.com/Tencent/spring-cloud-tencent/pull/815) - [Feature: upgrade framework version.](https://github.com/Tencent/spring-cloud-tencent/pull/815)
- [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/816) - [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/816)
- [fix:fix router parameter not checking bug.](https://github.com/Tencent/spring-cloud-tencent/pull/826)

@ -89,7 +89,7 @@
<properties> <properties>
<!-- Project revision --> <!-- Project revision -->
<revision>1.9.0-2021.0.5-SNAPSHOT</revision> <revision>1.9.0-2021.0.5</revision>
<!-- Spring Cloud --> <!-- Spring Cloud -->
<spring.cloud.version>2021.0.5</spring.cloud.version> <spring.cloud.version>2021.0.5</spring.cloud.version>

@ -17,15 +17,18 @@
package com.tencent.cloud.polaris.util; 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.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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
/** /**
* okhttp util. * okhttp util.
* *
@ -38,52 +41,46 @@ public final class OkHttpUtil {
*/ */
public final static Logger LOGGER = LoggerFactory.getLogger(OkHttpUtil.class); public final static Logger LOGGER = LoggerFactory.getLogger(OkHttpUtil.class);
/**
* client.
*/
private final static OkHttpClient HTTP_CLIENT = new OkHttpClient();
private OkHttpUtil() { private OkHttpUtil() {
} }
/** /**
* get request. * get request.
* @param url url * @param path path
* @param headers headers * @param headers headers
* @return response * @return response
*/ */
public static boolean get(String url, Map<String, String> headers) { public static boolean get(String path, Map<String, String> headers) {
HttpURLConnection conn = null;
try { try {
Request.Builder builder = new Request.Builder(); URL url = new java.net.URL(path);
buildHeader(builder, headers); conn = (HttpURLConnection) url.openConnection();
Request request = builder.url(url).build();
Response response = HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful() && Objects.nonNull(response.body())) { conn.setRequestMethod("GET");
String result = response.body().string(); conn.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(2));
LOGGER.debug("exec get request, url: {} success, response data: {}", url, result); 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; return true;
} }
} }
catch (Exception e) { 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;
} }
finally {
/** if (null != conn) {
* build header. conn.disconnect();
* @param builder builder
* @param headers headers
*/
private static void buildHeader(Request.Builder builder, Map<String, String> headers) {
if (Objects.nonNull(headers) && headers.size() > 0) {
headers.forEach((k, v) -> {
if (Objects.nonNull(k) && Objects.nonNull(v)) {
builder.addHeader(k, v);
} }
});
} }
return false;
} }
} }

@ -144,6 +144,8 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
Flux<List<ServiceInstance>> doRouter(Flux<List<ServiceInstance>> allServers, PolarisRouterContext routerContext) { Flux<List<ServiceInstance>> doRouter(Flux<List<ServiceInstance>> allServers, PolarisRouterContext routerContext) {
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers); ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers);
List<ServiceInstance> filteredInstances = new ArrayList<>();
if (serviceInstances.getInstances().size() > 0) {
// filter instance by routers // filter instance by routers
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, routerContext); ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, routerContext);
@ -157,11 +159,11 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
processRouterResponseInterceptors(routerContext, processRoutersResponse); processRouterResponseInterceptors(routerContext, processRoutersResponse);
// transfer polaris server to ServiceInstance // transfer polaris server to ServiceInstance
List<ServiceInstance> filteredInstances = new ArrayList<>();
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances(); ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
for (Instance instance : filteredServiceInstances.getInstances()) { for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServiceInstance(instance)); filteredInstances.add(new PolarisServiceInstance(instance));
} }
}
return Flux.fromIterable(Collections.singletonList(filteredInstances)); return Flux.fromIterable(Collections.singletonList(filteredInstances));
} }

@ -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.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector; import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/** /**
* Router actuator endpoint. * Router actuator endpoint.
@ -54,11 +55,11 @@ public class PolarisRouterEndpoint {
@ReadOperation @ReadOperation
public Map<String, Object> router(@Selector String dstService) { public Map<String, Object> router(@Selector String dstService) {
List<RoutingProto.Route> routerRules = serviceRuleManager.getServiceRouterRule(MetadataContext.LOCAL_NAMESPACE,
MetadataContext.LOCAL_SERVICE, dstService);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (StringUtils.hasText(dstService)) {
List<RoutingProto.Route> routerRules = serviceRuleManager.getServiceRouterRule(MetadataContext.LOCAL_NAMESPACE,
MetadataContext.LOCAL_SERVICE, dstService);
List<Object> rules = new LinkedList<>(); List<Object> rules = new LinkedList<>();
result.put("routerRules", rules); result.put("routerRules", rules);
@ -69,6 +70,7 @@ public class PolarisRouterEndpoint {
for (RoutingProto.Route route : routerRules) { for (RoutingProto.Route route : routerRules) {
rules.add(parseRouterRule(route)); rules.add(parseRouterRule(route));
} }
}
return result; return result;
} }

@ -70,10 +70,10 @@
</developers> </developers>
<properties> <properties>
<revision>1.9.0-2021.0.5-SNAPSHOT</revision> <revision>1.9.0-2021.0.5</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.10.2</polaris.version> <polaris.version>1.10.5</polaris.version>
<guava.version>31.0.1-jre</guava.version> <guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version> <logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version> <mocktio.version>4.5.1</mocktio.version>

@ -34,6 +34,8 @@ import com.tencent.polaris.client.flow.FlowControlParam;
import com.tencent.polaris.client.flow.ResourcesResponse; import com.tencent.polaris.client.flow.ResourcesResponse;
import com.tencent.polaris.client.pb.RateLimitProto; import com.tencent.polaris.client.pb.RateLimitProto;
import com.tencent.polaris.client.pb.RoutingProto; 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. * 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 { public class ServiceRuleManager {
private static final Logger LOG = LoggerFactory.getLogger(ServiceRuleManager.class);
private final SDKContext sdkContext; private final SDKContext sdkContext;
private final FlowControlParam controlParam; private final FlowControlParam controlParam;
public ServiceRuleManager(SDKContext sdkContext) { public ServiceRuleManager(SDKContext sdkContext) {
@ -55,6 +57,7 @@ public class ServiceRuleManager {
} }
public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) { 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 serviceEventKey = new ServiceEventKey(new ServiceKey(namespace, service),
ServiceEventKey.EventType.RATE_LIMITING); ServiceEventKey.EventType.RATE_LIMITING);
@ -76,6 +79,8 @@ public class ServiceRuleManager {
} }
public List<RoutingProto.Route> getServiceRouterRule(String namespace, String sourceService, String dstService) { public List<RoutingProto.Route> getServiceRouterRule(String namespace, String sourceService, String dstService) {
LOG.debug("Get service router rules with namespace:{} and sourceService:{} and dstService:{}.",
namespace, sourceService, dstService);
Set<ServiceEventKey> routerKeys = new HashSet<>(); Set<ServiceEventKey> routerKeys = new HashSet<>();
ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, dstService), ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, dstService),

Loading…
Cancel
Save