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.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;
}
}

@ -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";
}

@ -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;
}
}

@ -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<ServerWebExchange
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
public String buildErrorResult(ServerWebExchange exchange, Throwable ex) {
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.param.ApiParam;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpStatus;
/**
@ -60,6 +61,13 @@ public interface RouteInterceptorContext {
*/
Object getRequestContext();
/**
* 获取目标微服务实例即具体选中哪台实例
*
* @return 返回目标微服务
*/
ServiceInstance getServiceInstance();
/**
* 获取请求内容大小
*

@ -31,7 +31,7 @@ import java.util.Map;
@Slf4j
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_MSG_NAME = "msg";
@ -107,7 +107,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
*
* @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<T, R> implements ResultExecutor<T, R>
defaultRouteInterceptorContext.setServiceErrorMsg(responseErrorMessage);
}
}
this.bindRouteInterceptorContextProperties(routeInterceptorContext, requestContext);
RouteInterceptorUtil.runAfterRoute(routeInterceptorContext);
}
protected void bindRouteInterceptorContextProperties(RouteInterceptorContext routeInterceptorContext, T requestContext) {
}
/**
* 将微服务的返回结果解析成JSONObject
*

@ -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

Loading…
Cancel
Save