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

pull/837/head
Haotian Zhang 2 years ago committed by GitHub
parent 598f4f55da
commit 11db82f743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,3 +26,4 @@
- [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/804) - [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/804)
- [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/813) - [fix:fix some compile output issues.](https://github.com/Tencent/spring-cloud-tencent/pull/813)
- [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/819) - [fix:nacos & polaris discovery](https://github.com/Tencent/spring-cloud-tencent/pull/819)
- [fix:fix router parameter not checking bug.](https://github.com/Tencent/spring-cloud-tencent/pull/827)

@ -89,7 +89,7 @@
<properties> <properties>
<!-- Project revision --> <!-- Project revision -->
<revision>1.9.0-2020.0.5-SNAPSHOT</revision> <revision>1.9.0-2020.0.5</revision>
<!-- Spring Cloud --> <!-- Spring Cloud -->
<spring.cloud.version>2020.0.5</spring.cloud.version> <spring.cloud.version>2020.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) {
conn.disconnect();
/** }
* build header.
* @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,23 +144,25 @@ 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);
// filter instance by routers List<ServiceInstance> filteredInstances = new ArrayList<>();
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, routerContext); if (serviceInstances.getInstances().size() > 0) {
// filter instance by routers
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, routerContext);
// process request interceptors // process request interceptors
processRouterRequestInterceptors(processRoutersRequest, routerContext); processRouterRequestInterceptors(processRoutersRequest, routerContext);
// process router chain // process router chain
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest); ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
// process response interceptors // process response interceptors
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.
@ -53,20 +54,21 @@ 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<>();
List<Object> rules = new LinkedList<>(); if (StringUtils.hasText(dstService)) {
result.put("routerRules", rules); List<RoutingProto.Route> routerRules = serviceRuleManager.getServiceRouterRule(MetadataContext.LOCAL_NAMESPACE,
MetadataContext.LOCAL_SERVICE, dstService);
List<Object> rules = new LinkedList<>();
result.put("routerRules", rules);
if (CollectionUtils.isEmpty(routerRules)) { if (CollectionUtils.isEmpty(routerRules)) {
return result; return result;
} }
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-2020.0.5-SNAPSHOT</revision> <revision>1.9.0-2020.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