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)
- [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:fix router parameter not checking bug.](https://github.com/Tencent/spring-cloud-tencent/pull/827)

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

@ -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,52 +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<String, String> headers) {
public static boolean get(String path, Map<String, String> 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<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);
}
});
finally {
if (null != conn) {
conn.disconnect();
}
}
return false;
}
}

@ -144,23 +144,25 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
Flux<List<ServiceInstance>> doRouter(Flux<List<ServiceInstance>> allServers, PolarisRouterContext routerContext) {
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers);
// filter instance by routers
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, routerContext);
List<ServiceInstance> filteredInstances = new ArrayList<>();
if (serviceInstances.getInstances().size() > 0) {
// filter instance by routers
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, routerContext);
// process request interceptors
processRouterRequestInterceptors(processRoutersRequest, routerContext);
// process request interceptors
processRouterRequestInterceptors(processRoutersRequest, routerContext);
// process router chain
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
// process router chain
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
// process response interceptors
processRouterResponseInterceptors(routerContext, processRoutersResponse);
// process response interceptors
processRouterResponseInterceptors(routerContext, processRoutersResponse);
// transfer polaris server to ServiceInstance
List<ServiceInstance> filteredInstances = new ArrayList<>();
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServiceInstance(instance));
// transfer polaris server to ServiceInstance
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServiceInstance(instance));
}
}
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.Selector;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* Router actuator endpoint.
@ -53,20 +54,21 @@ public class PolarisRouterEndpoint {
@ReadOperation
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<>();
List<Object> rules = new LinkedList<>();
result.put("routerRules", rules);
if (StringUtils.hasText(dstService)) {
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)) {
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;

@ -70,10 +70,10 @@
</developers>
<properties>
<revision>1.9.0-2020.0.5-SNAPSHOT</revision>
<revision>1.9.0-2020.0.5</revision>
<!-- Dependencies -->
<polaris.version>1.10.2</polaris.version>
<polaris.version>1.10.5</polaris.version>
<guava.version>31.0.1-jre</guava.version>
<logback.version>1.2.11</logback.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.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<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<>();
ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, dstService),

Loading…
Cancel
Save