fix:fix reporting bug when port is -1.

pull/1038/head
Haotian Zhang 2 years ago
parent 0dd00d5f71
commit a032ac3f11

@ -14,3 +14,4 @@
- [feat: update sct-all package method by maven shaded plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1024)
- [fix:use path parameter in `@FeignClient` for circuit-breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1029)
- [build(deps): bump guava in /spring-cloud-tencent-dependencies](https://github.com/Tencent/spring-cloud-tencent/pull/1034)
- [fix:fix reporting bug when port is -1.](https://github.com/Tencent/spring-cloud-tencent/pull/1038)

@ -71,8 +71,8 @@ public class EnhancedFeignClient implements Client {
enhancedPluginContext.setRequest(enhancedRequestContext);
enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
enhancedPluginContext.setTargetServiceInstance(
(ServiceInstance) MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE));
enhancedPluginContext.setTargetServiceInstance((ServiceInstance) MetadataContextHolder.get()
.getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE), url);
// Run pre enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);

@ -18,6 +18,14 @@
package com.tencent.cloud.rpc.enhancement.plugin;
import java.net.URI;
import java.util.Objects;
import com.tencent.polaris.configuration.client.JustForTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
/**
@ -27,6 +35,8 @@ import org.springframework.cloud.client.ServiceInstance;
*/
public class EnhancedPluginContext {
private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedPluginContext.class);
private EnhancedRequestContext request;
private EnhancedResponseContext response;
@ -86,8 +96,30 @@ public class EnhancedPluginContext {
return targetServiceInstance;
}
@JustForTest
public void setTargetServiceInstance(ServiceInstance targetServiceInstance) {
this.targetServiceInstance = targetServiceInstance;
this.setTargetServiceInstance(targetServiceInstance, null);
}
public void setTargetServiceInstance(ServiceInstance targetServiceInstance, URI url) {
if (Objects.nonNull(targetServiceInstance)) {
this.targetServiceInstance = targetServiceInstance;
}
else if (Objects.nonNull(url)) {
DefaultServiceInstance defaultServiceInstance = new DefaultServiceInstance();
defaultServiceInstance.setUri(url);
if (defaultServiceInstance.isSecure()) {
defaultServiceInstance.setPort(443);
}
else {
defaultServiceInstance.setPort(80);
}
this.targetServiceInstance = defaultServiceInstance;
}
else {
this.targetServiceInstance = new DefaultServiceInstance();
LOGGER.warn("TargetServiceInstance is empty.");
}
}
@Override

@ -60,8 +60,8 @@ public class EnhancedRestTemplateInterceptor implements ClientHttpRequestInterce
enhancedPluginContext.setRequest(enhancedRequestContext);
enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
enhancedPluginContext.setTargetServiceInstance(
(ServiceInstance) MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE));
enhancedPluginContext.setTargetServiceInstance((ServiceInstance) MetadataContextHolder.get()
.getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE), request.getURI());
// Run pre enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);

@ -62,7 +62,10 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
Response<ServiceInstance> serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
if (serviceInstanceResponse != null && serviceInstanceResponse.hasServer()) {
ServiceInstance instance = serviceInstanceResponse.getServer();
enhancedPluginContext.setTargetServiceInstance(instance);
enhancedPluginContext.setTargetServiceInstance(instance, exchange.getRequest().getURI());
}
else {
enhancedPluginContext.setTargetServiceInstance(null, exchange.getRequest().getURI());
}
// Run pre enhanced plugins.

@ -57,8 +57,8 @@ public class EnhancedWebClientExchangeFilterFunction implements ExchangeFilterFu
enhancedPluginContext.setRequest(enhancedRequestContext);
enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
enhancedPluginContext.setTargetServiceInstance(
(ServiceInstance) MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE));
enhancedPluginContext.setTargetServiceInstance((ServiceInstance) MetadataContextHolder.get()
.getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE), request.url());
// Run post enhanced plugins.
pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext);

Loading…
Cancel
Save