diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/DefaultRouteInterceptorContext.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/DefaultRouteInterceptorContext.java index 7fc5497b..abc7aea4 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/DefaultRouteInterceptorContext.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/DefaultRouteInterceptorContext.java @@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.bean; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext; import com.gitee.sop.gatewaycommon.param.ApiParam; +import org.springframework.cloud.client.ServiceInstance; /** * @author tanghc @@ -26,6 +27,8 @@ public class DefaultRouteInterceptorContext implements RouteInterceptorContext { private long requestDataSize; /** 返回内容大小 */ private long responseDataSize; + /** 负载均衡选中的微服务 */ + private ServiceInstance serviceInstance; @Override public ApiParam getApiParam() { @@ -111,4 +114,13 @@ public class DefaultRouteInterceptorContext implements RouteInterceptorContext { public void setResponseDataSize(long responseDataSize) { this.responseDataSize = responseDataSize; } + + @Override + public ServiceInstance getServiceInstance() { + return serviceInstance; + } + + public void setServiceInstance(ServiceInstance serviceInstance) { + this.serviceInstance = serviceInstance; + } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java index 1e176a42..beb5e859 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java @@ -51,5 +51,6 @@ public class SopConstants { public static final String METADATA_ENV_GRAY_VALUE = "gray"; public static final String CACHE_ROUTE_INTERCEPTOR_CONTEXT = "cacheRouteInterceptorContext"; + public static final String TARGET_SERVICE = "sop-target-service"; } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/SopLoadBalancerClientFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/SopLoadBalancerClientFilter.java index b8e74c55..b048c8af 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/SopLoadBalancerClientFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/SopLoadBalancerClientFilter.java @@ -1,5 +1,6 @@ package com.gitee.sop.gatewaycommon.gateway.filter; +import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.gateway.loadbalancer.SopLoadBalancerClient; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; @@ -22,11 +23,14 @@ public class SopLoadBalancerClientFilter extends LoadBalancerClientFilter { @Override protected ServiceInstance choose(ServerWebExchange exchange) { + ServiceInstance serviceInstance; if (loadBalancer instanceof SopLoadBalancerClient) { SopLoadBalancerClient sopLoadBalancerClient = (SopLoadBalancerClient)loadBalancer; - return sopLoadBalancerClient.choose(((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost(), exchange); + serviceInstance = sopLoadBalancerClient.choose(((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost(), exchange); } else { - return super.choose(exchange); + serviceInstance = super.choose(exchange); } + exchange.getAttributes().put(SopConstants.TARGET_SERVICE, serviceInstance); + return serviceInstance; } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java index 87df32b9..03eab2c4 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java @@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.gateway.result; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.gatewaycommon.bean.DefaultRouteInterceptorContext; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext; import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.exception.ApiException; @@ -12,6 +13,7 @@ import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.result.BaseExecutorAdapter; import com.gitee.sop.gatewaycommon.result.ResultExecutorForGateway; import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.client.ServiceInstance; import org.springframework.http.HttpStatus; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -71,6 +73,13 @@ public class GatewayResultExecutor extends BaseExecutorAdapter implements ResultExecutor { - private static Map HTTP_STATUS_ERROR_ENUM_MAP = new HashMap<>(8); + private static final Map HTTP_STATUS_ERROR_ENUM_MAP = new HashMap<>(8); private static final String GATEWAY_CODE_NAME = "code"; private static final String GATEWAY_MSG_NAME = "msg"; @@ -107,7 +107,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor * * @param serviceResult 微服务结果 * @param responseStatus 微服务状态码 - * @param requestContext 微服务状态码 + * @param requestContext 请求上下文 */ private void doAfterRoute(String serviceResult, int responseStatus, T requestContext) { RouteInterceptorContext routeInterceptorContext = getRouteInterceptorContext(requestContext); @@ -125,9 +125,14 @@ public abstract class BaseExecutorAdapter implements ResultExecutor defaultRouteInterceptorContext.setServiceErrorMsg(responseErrorMessage); } } + this.bindRouteInterceptorContextProperties(routeInterceptorContext, requestContext); RouteInterceptorUtil.runAfterRoute(routeInterceptorContext); } + protected void bindRouteInterceptorContextProperties(RouteInterceptorContext routeInterceptorContext, T requestContext) { + + } + /** * 将微服务的返回结果解析成JSONObject * diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/interceptor/MyRouteInterceptor.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/interceptor/MyRouteInterceptor.java index 91039ac8..0a3b8884 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/interceptor/MyRouteInterceptor.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/interceptor/MyRouteInterceptor.java @@ -3,6 +3,7 @@ package com.gitee.sop.gateway.interceptor; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext; import com.gitee.sop.gatewaycommon.param.ApiParam; +import org.springframework.cloud.client.ServiceInstance; import org.springframework.stereotype.Component; /** @@ -21,7 +22,9 @@ public class MyRouteInterceptor implements RouteInterceptor { @Override public void afterRoute(RouteInterceptorContext context) { - System.out.println("请求成功,微服务返回结果:" + context.getServiceResult()); + ServiceInstance serviceInstance = context.getServiceInstance(); + System.out.println("请求成功,serviceId:" + serviceInstance.getServiceId() + "(" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + + ",微服务返回结果:" + context.getServiceResult()); } @Override