RouteInterceptorContext可获转发的目标微服务信息

3.x
tanghc 4 years ago
parent a90d0214a8
commit 06229d6b90
  1. 12
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/DefaultRouteInterceptorContext.java
  2. 1
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java
  3. 8
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/SopLoadBalancerClientFilter.java
  4. 9
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java
  5. 8
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/interceptor/RouteInterceptorContext.java
  6. 9
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java
  7. 5
      sop-gateway/src/main/java/com/gitee/sop/gateway/interceptor/MyRouteInterceptor.java

@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.bean;
import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext;
import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.ApiParam;
import org.springframework.cloud.client.ServiceInstance;
/** /**
* @author tanghc * @author tanghc
@ -26,6 +27,8 @@ public class DefaultRouteInterceptorContext implements RouteInterceptorContext {
private long requestDataSize; private long requestDataSize;
/** 返回内容大小 */ /** 返回内容大小 */
private long responseDataSize; private long responseDataSize;
/** 负载均衡选中的微服务 */
private ServiceInstance serviceInstance;
@Override @Override
public ApiParam getApiParam() { public ApiParam getApiParam() {
@ -111,4 +114,13 @@ public class DefaultRouteInterceptorContext implements RouteInterceptorContext {
public void setResponseDataSize(long responseDataSize) { public void setResponseDataSize(long responseDataSize) {
this.responseDataSize = responseDataSize; this.responseDataSize = responseDataSize;
} }
@Override
public ServiceInstance getServiceInstance() {
return serviceInstance;
}
public void setServiceInstance(ServiceInstance serviceInstance) {
this.serviceInstance = serviceInstance;
}
} }

@ -51,5 +51,6 @@ public class SopConstants {
public static final String METADATA_ENV_GRAY_VALUE = "gray"; public static final String METADATA_ENV_GRAY_VALUE = "gray";
public static final String CACHE_ROUTE_INTERCEPTOR_CONTEXT = "cacheRouteInterceptorContext"; public static final String CACHE_ROUTE_INTERCEPTOR_CONTEXT = "cacheRouteInterceptorContext";
public static final String TARGET_SERVICE = "sop-target-service";
} }

@ -1,5 +1,6 @@
package com.gitee.sop.gatewaycommon.gateway.filter; package com.gitee.sop.gatewaycommon.gateway.filter;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.gateway.loadbalancer.SopLoadBalancerClient; import com.gitee.sop.gatewaycommon.gateway.loadbalancer.SopLoadBalancerClient;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
@ -22,11 +23,14 @@ public class SopLoadBalancerClientFilter extends LoadBalancerClientFilter {
@Override @Override
protected ServiceInstance choose(ServerWebExchange exchange) { protected ServiceInstance choose(ServerWebExchange exchange) {
ServiceInstance serviceInstance;
if (loadBalancer instanceof SopLoadBalancerClient) { if (loadBalancer instanceof SopLoadBalancerClient) {
SopLoadBalancerClient sopLoadBalancerClient = (SopLoadBalancerClient)loadBalancer; 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 { } else {
return super.choose(exchange); serviceInstance = super.choose(exchange);
} }
exchange.getAttributes().put(SopConstants.TARGET_SERVICE, serviceInstance);
return serviceInstance;
} }
} }

@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.gateway.result;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.gatewaycommon.bean.DefaultRouteInterceptorContext;
import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext;
import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.exception.ApiException; 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.BaseExecutorAdapter;
import com.gitee.sop.gatewaycommon.result.ResultExecutorForGateway; import com.gitee.sop.gatewaycommon.result.ResultExecutorForGateway;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -71,6 +73,13 @@ public class GatewayResultExecutor extends BaseExecutorAdapter<ServerWebExchange
return (RouteInterceptorContext) exchange.getAttributes().get(SopConstants.CACHE_ROUTE_INTERCEPTOR_CONTEXT); return (RouteInterceptorContext) exchange.getAttributes().get(SopConstants.CACHE_ROUTE_INTERCEPTOR_CONTEXT);
} }
@Override
protected void bindRouteInterceptorContextProperties(RouteInterceptorContext routeInterceptorContext, ServerWebExchange requestContext) {
ServiceInstance serviceInstance = requestContext.getAttribute(SopConstants.TARGET_SERVICE);
DefaultRouteInterceptorContext context = (DefaultRouteInterceptorContext) routeInterceptorContext;
context.setServiceInstance(serviceInstance);
}
@Override @Override
public String buildErrorResult(ServerWebExchange exchange, Throwable ex) { public String buildErrorResult(ServerWebExchange exchange, Throwable ex) {
Locale locale = getLocale(exchange); Locale locale = getLocale(exchange);

@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.interceptor;
import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.ApiParam;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
/** /**
@ -60,6 +61,13 @@ public interface RouteInterceptorContext {
*/ */
Object getRequestContext(); Object getRequestContext();
/**
* 获取目标微服务实例即具体选中哪台实例
*
* @return 返回目标微服务
*/
ServiceInstance getServiceInstance();
/** /**
* 获取请求内容大小 * 获取请求内容大小
* *

@ -31,7 +31,7 @@ import java.util.Map;
@Slf4j @Slf4j
public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R> { public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R> {
private static Map<Integer, ErrorEnum> HTTP_STATUS_ERROR_ENUM_MAP = new HashMap<>(8); private static final Map<Integer, ErrorEnum> HTTP_STATUS_ERROR_ENUM_MAP = new HashMap<>(8);
private static final String GATEWAY_CODE_NAME = "code"; private static final String GATEWAY_CODE_NAME = "code";
private static final String GATEWAY_MSG_NAME = "msg"; private static final String GATEWAY_MSG_NAME = "msg";
@ -107,7 +107,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
* *
* @param serviceResult 微服务结果 * @param serviceResult 微服务结果
* @param responseStatus 微服务状态码 * @param responseStatus 微服务状态码
* @param requestContext 微服务状态码 * @param requestContext 请求上下文
*/ */
private void doAfterRoute(String serviceResult, int responseStatus, T requestContext) { private void doAfterRoute(String serviceResult, int responseStatus, T requestContext) {
RouteInterceptorContext routeInterceptorContext = getRouteInterceptorContext(requestContext); RouteInterceptorContext routeInterceptorContext = getRouteInterceptorContext(requestContext);
@ -125,9 +125,14 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
defaultRouteInterceptorContext.setServiceErrorMsg(responseErrorMessage); defaultRouteInterceptorContext.setServiceErrorMsg(responseErrorMessage);
} }
} }
this.bindRouteInterceptorContextProperties(routeInterceptorContext, requestContext);
RouteInterceptorUtil.runAfterRoute(routeInterceptorContext); RouteInterceptorUtil.runAfterRoute(routeInterceptorContext);
} }
protected void bindRouteInterceptorContextProperties(RouteInterceptorContext routeInterceptorContext, T requestContext) {
}
/** /**
* 将微服务的返回结果解析成JSONObject * 将微服务的返回结果解析成JSONObject
* *

@ -3,6 +3,7 @@ package com.gitee.sop.gateway.interceptor;
import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor;
import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptorContext;
import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.ApiParam;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -21,7 +22,9 @@ public class MyRouteInterceptor implements RouteInterceptor {
@Override @Override
public void afterRoute(RouteInterceptorContext context) { 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 @Override

Loading…
Cancel
Save