新增restful模式

This commit is contained in:
六如
2025-02-04 11:58:25 +08:00
parent 1cecae512c
commit a2a438562b
20 changed files with 212 additions and 50 deletions

View File

@@ -39,6 +39,12 @@
<artifactId>dubbo-nacos-spring-boot-starter</artifactId> <artifactId>dubbo-nacos-spring-boot-starter</artifactId>
</dependency> </dependency>
<!-- zookeeper注册中心 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
@@ -63,15 +69,6 @@
<!-- provided --> <!-- provided -->
<!-- 仅在开发中使用 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>

View File

@@ -5,7 +5,6 @@ import com.gitee.sop.productweb.open.resp.ProductResponse;
import com.gitee.sop.support.annotation.Open; import com.gitee.sop.support.annotation.Open;
import com.gitee.sop.support.context.OpenContext; import com.gitee.sop.support.context.OpenContext;
import com.gitee.sop.support.dto.FileData; import com.gitee.sop.support.dto.FileData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@@ -18,7 +17,6 @@ import java.util.List;
* @author 六如 * @author 六如
* @dubbo * @dubbo
*/ */
@Api("产品服务")
public interface OpenProduct { public interface OpenProduct {
/** /**

View File

@@ -16,9 +16,9 @@ public class ProductSaveRequest implements Serializable {
private static final long serialVersionUID = -1214422742659231037L; private static final long serialVersionUID = -1214422742659231037L;
/** /**
* 故事名称 * 产品名称
*/ */
@NotBlank(message = "故事名称必填") @NotBlank(message = "产品名称必填")
@Length(max = 64) @Length(max = 64)
private String productName; private String productName;

View File

@@ -1,6 +1,7 @@
package com.sop.example.rest.examplerest.rest.impl; package com.sop.example.rest.examplerest.rest.impl;
import com.gitee.sop.support.context.OpenContext; import com.gitee.sop.support.context.OpenContext;
import com.gitee.sop.support.context.WebContext;
import com.sop.example.rest.examplerest.rest.GoodsController; import com.sop.example.rest.examplerest.rest.GoodsController;
import com.sop.example.rest.examplerest.rest.vo.GoodsVO; import com.sop.example.rest.examplerest.rest.vo.GoodsVO;
import org.apache.dubbo.config.annotation.DubboService; import org.apache.dubbo.config.annotation.DubboService;
@@ -19,7 +20,7 @@ public class GoodsControllerImpl implements GoodsController {
goodsVO.setId(id); goodsVO.setId(id);
goodsVO.setName("冰箱"); goodsVO.setName("冰箱");
Map<String, String> headers = OpenContext.current().getHeaders(); Map<String, String> headers = WebContext.current().getHeaders();
String token = headers.get("token"); String token = headers.get("token");
System.out.println(token); System.out.println(token);
return goodsVO; return goodsVO;

View File

@@ -1,6 +1,5 @@
package com.gitee.sop.gateway.controller; package com.gitee.sop.gateway.controller;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.request.ApiRequest; import com.gitee.sop.gateway.request.ApiRequest;
import com.gitee.sop.gateway.request.ApiRequestContext; import com.gitee.sop.gateway.request.ApiRequestContext;
import com.gitee.sop.gateway.response.Response; import com.gitee.sop.gateway.response.Response;
@@ -12,7 +11,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;

View File

@@ -1,12 +1,12 @@
package com.gitee.sop.gateway.request; package com.gitee.sop.gateway.request;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.gitee.sop.support.context.WebContext;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
/** /**
* @author 六如 * @author 六如
@@ -53,7 +53,7 @@ public class ApiRequestContext {
private JSONObject rawParams; private JSONObject rawParams;
/** /**
* 请求头 * WEB Context
*/ */
private Map<String, String> headers; private WebContext webContext;
} }

View File

@@ -2,7 +2,6 @@ package com.gitee.sop.gateway.service.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.config.ApiConfig; import com.gitee.sop.gateway.config.ApiConfig;
import com.gitee.sop.gateway.request.ApiRequest; import com.gitee.sop.gateway.request.ApiRequest;
import com.gitee.sop.gateway.request.ApiRequestContext; import com.gitee.sop.gateway.request.ApiRequestContext;
@@ -14,6 +13,9 @@ import com.gitee.sop.gateway.service.ParamExecutor;
import com.gitee.sop.gateway.service.Serde; import com.gitee.sop.gateway.service.Serde;
import com.gitee.sop.gateway.util.RequestUtil; import com.gitee.sop.gateway.util.RequestUtil;
import com.gitee.sop.gateway.util.ResponseUtil; import com.gitee.sop.gateway.util.ResponseUtil;
import com.gitee.sop.support.constant.SopConstants;
import com.gitee.sop.support.context.DefaultWebContext;
import com.gitee.sop.support.context.WebContext;
import com.gitee.sop.support.dto.FileData; import com.gitee.sop.support.dto.FileData;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@@ -24,9 +26,11 @@ import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.UUID; import java.util.UUID;
/** /**
* 请求参数默认实现 * 请求参数默认实现
* *
@@ -93,10 +97,31 @@ public class ParamExecutorImpl implements ParamExecutor<HttpServletRequest, Http
.uploadContext(uploadContext) .uploadContext(uploadContext)
.traceId(UUID.randomUUID().toString().replace("-", "")) .traceId(UUID.randomUUID().toString().replace("-", ""))
.rawParams(params) .rawParams(params)
.headers(RequestUtil.getHeaders(request)) .webContext(buildWebContext(request))
.build(); .build();
} }
protected WebContext buildWebContext(HttpServletRequest request) {
DefaultWebContext defaultWebContext = new DefaultWebContext();
defaultWebContext.setMethod(request.getMethod());
defaultWebContext.setPathInfo(request.getPathInfo());
defaultWebContext.setPathTranslated(request.getPathTranslated());
defaultWebContext.setContextPath(request.getContextPath());
defaultWebContext.setQueryString(request.getQueryString());
defaultWebContext.setRequestURI(request.getRequestURI());
defaultWebContext.setRequestURL(request.getRequestURL());
defaultWebContext.setServletPath(request.getServletPath());
defaultWebContext.setContentLength(request.getContentLength());
defaultWebContext.setContentType(request.getContentType());
defaultWebContext.setRemoteAddr(request.getRemoteAddr());
defaultWebContext.setRemoteHost(request.getRemoteHost());
defaultWebContext.setRemotePort(request.getRemotePort());
defaultWebContext.setLocale(request.getLocale());
defaultWebContext.setHeaders(RequestUtil.getHeaders(request));
defaultWebContext.setParamtreMap(new LinkedHashMap<>(request.getParameterMap()));
return defaultWebContext;
}
protected String getTag(HttpServletRequest request) { protected String getTag(HttpServletRequest request) {
return request.getHeader(apiConfig.getHeaderKeyTag()); return request.getHeader(apiConfig.getHeaderKeyTag());

View File

@@ -4,7 +4,6 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.gitee.sop.gateway.common.ApiInfoDTO; import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.common.ParamInfoDTO; import com.gitee.sop.gateway.common.ParamInfoDTO;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.exception.ApiException; import com.gitee.sop.gateway.exception.ApiException;
import com.gitee.sop.gateway.exception.ExceptionExecutor; import com.gitee.sop.gateway.exception.ExceptionExecutor;
import com.gitee.sop.gateway.interceptor.RouteInterceptor; import com.gitee.sop.gateway.interceptor.RouteInterceptor;
@@ -20,8 +19,10 @@ import com.gitee.sop.gateway.service.RouteService;
import com.gitee.sop.gateway.service.Serde; import com.gitee.sop.gateway.service.Serde;
import com.gitee.sop.gateway.service.validate.Validator; import com.gitee.sop.gateway.service.validate.Validator;
import com.gitee.sop.gateway.util.ClassUtil; import com.gitee.sop.gateway.util.ClassUtil;
import com.gitee.sop.support.constant.SopConstants;
import com.gitee.sop.support.context.DefaultOpenContext; import com.gitee.sop.support.context.DefaultOpenContext;
import com.gitee.sop.support.context.OpenContext; import com.gitee.sop.support.context.OpenContext;
import com.gitee.sop.support.context.WebContext;
import com.gitee.sop.support.dto.CommonFileData; import com.gitee.sop.support.dto.CommonFileData;
import com.gitee.sop.support.dto.FileData; import com.gitee.sop.support.dto.FileData;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -108,7 +109,8 @@ public class RouteServiceImpl implements RouteService {
String paramInfo = apiInfo.getParamInfo(); String paramInfo = apiInfo.getParamInfo();
List<ParamInfoDTO> paramInfoList = JSON.parseArray(paramInfo, ParamInfoDTO.class); List<ParamInfoDTO> paramInfoList = JSON.parseArray(paramInfo, ParamInfoDTO.class);
OpenContext openRequest = buildOpenContext(apiRequestContext); OpenContext openRequest = buildOpenContext(apiRequestContext);
clientAttachment.setAttachment(SopConstants.OPEN_CONTEXT, JSON.toJSONString(openRequest)); clientAttachment.setObjectAttachment(SopConstants.OPEN_CONTEXT, openRequest);
clientAttachment.setObjectAttachment(SopConstants.WEB_CONTEXT, apiRequestContext.getWebContext());
return genericServiceInvoker.invoke( return genericServiceInvoker.invoke(
apiInfo.getInterfaceClassName(), apiInfo.getInterfaceClassName(),
apiInfo.getMethodName(), apiInfo.getMethodName(),
@@ -156,6 +158,8 @@ public class RouteServiceImpl implements RouteService {
// 上下文 // 上下文
if (Objects.equals(type, OpenContext.class.getName())) { if (Objects.equals(type, OpenContext.class.getName())) {
params.add(openRequest); params.add(openRequest);
} else if (Objects.equals(type, WebContext.class.getName())) {
params.add(apiRequestContext.getWebContext());
} else if (Objects.equals(type, FileData.class.getName()) || Objects.equals(actualType, FileData.class.getName())) { } else if (Objects.equals(type, FileData.class.getName()) || Objects.equals(actualType, FileData.class.getName())) {
// 处理文件上传 // 处理文件上传
Optional<Object> fileParam = buildFileParam(apiRequestContext, paramInfoDTO); Optional<Object> fileParam = buildFileParam(apiRequestContext, paramInfoDTO);
@@ -203,8 +207,6 @@ public class RouteServiceImpl implements RouteService {
defaultOpenRequest.setNotifyUrl(apiRequest.getNotifyUrl()); defaultOpenRequest.setNotifyUrl(apiRequest.getNotifyUrl());
defaultOpenRequest.setTraceId(apiRequestContext.getTraceId()); defaultOpenRequest.setTraceId(apiRequestContext.getTraceId());
defaultOpenRequest.setLocale(apiRequestContext.getLocale()); defaultOpenRequest.setLocale(apiRequestContext.getLocale());
defaultOpenRequest.setHeaders(apiRequestContext.getHeaders());
defaultOpenRequest.initContext(); defaultOpenRequest.initContext();
return defaultOpenRequest; return defaultOpenRequest;
} }

View File

@@ -2,10 +2,10 @@ package com.gitee.sop.gateway.service.manager.impl;
import com.gitee.sop.gateway.common.ApiInfoDTO; import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.common.CacheKey; import com.gitee.sop.gateway.common.CacheKey;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.dao.entity.IsvInfo; import com.gitee.sop.gateway.dao.entity.IsvInfo;
import com.gitee.sop.gateway.dao.mapper.IsvInfoMapper; import com.gitee.sop.gateway.dao.mapper.IsvInfoMapper;
import com.gitee.sop.gateway.util.JsonUtil; import com.gitee.sop.gateway.util.JsonUtil;
import com.gitee.sop.support.constant.SopConstants;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.BoundHashOperations;

View File

@@ -1,11 +1,11 @@
package com.gitee.sop.gateway.service.manager.impl; package com.gitee.sop.gateway.service.manager.impl;
import com.gitee.sop.gateway.common.CacheKey; import com.gitee.sop.gateway.common.CacheKey;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.dao.entity.IsvInfo; import com.gitee.sop.gateway.dao.entity.IsvInfo;
import com.gitee.sop.gateway.service.manager.dto.IsvDTO; import com.gitee.sop.gateway.service.manager.dto.IsvDTO;
import com.gitee.sop.gateway.util.CopyUtil; import com.gitee.sop.gateway.util.CopyUtil;
import com.gitee.sop.gateway.util.JsonUtil; import com.gitee.sop.gateway.util.JsonUtil;
import com.gitee.sop.support.constant.SopConstants;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;

View File

@@ -1,8 +1,8 @@
package com.gitee.sop.gateway.service.manager.impl; package com.gitee.sop.gateway.service.manager.impl;
import com.gitee.sop.gateway.common.CacheKey; import com.gitee.sop.gateway.common.CacheKey;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.dao.entity.IsvKeys; import com.gitee.sop.gateway.dao.entity.IsvKeys;
import com.gitee.sop.support.constant.SopConstants;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;

View File

@@ -1,7 +1,8 @@
package com.gitee.sop.gateway.service.validate; package com.gitee.sop.gateway.service.validate;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.support.constant.SopConstants;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;

View File

@@ -1,9 +1,9 @@
package com.gitee.sop.gateway.util; package com.gitee.sop.gateway.util;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.gitee.sop.gateway.common.SopConstants;
import com.gitee.sop.gateway.request.ApiUploadContext; import com.gitee.sop.gateway.request.ApiUploadContext;
import com.gitee.sop.gateway.request.UploadContext; import com.gitee.sop.gateway.request.UploadContext;
import com.gitee.sop.support.constant.SopConstants;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItem;

View File

@@ -1,4 +1,4 @@
package com.gitee.sop.gateway.common; package com.gitee.sop.support.constant;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -15,7 +15,8 @@ public final class SopConstants {
public static final String UTF8 = "UTF-8"; public static final String UTF8 = "UTF-8";
public static final String NULL = "null"; public static final String NULL = "null";
public static final String DUBBO_TAG = "dubbo.tag"; public static final String DUBBO_TAG = "dubbo.tag";
public static final String OPEN_CONTEXT = "open.context"; public static final String OPEN_CONTEXT = "sop.open-context";
public static final String WEB_CONTEXT = "sop.web-context";
public static final String DEFAULT_VERSION = "1.0"; public static final String DEFAULT_VERSION = "1.0";
} }

View File

@@ -5,7 +5,6 @@ import lombok.EqualsAndHashCode;
import java.io.Serializable; import java.io.Serializable;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
/** /**
* @author 六如 * @author 六如
@@ -55,11 +54,6 @@ public class DefaultOpenContext extends OpenContext implements Serializable {
*/ */
private Locale locale; private Locale locale;
/**
* 请求头
*/
private Map<String, String> headers;
public void initContext() { public void initContext() {
this.setContext(this); this.setContext(this);
} }

View File

@@ -0,0 +1,66 @@
package com.gitee.sop.support.context;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* @author 六如
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class DefaultWebContext extends WebContext implements Serializable {
private static final long serialVersionUID = -6070543618514206391L;
private String method;
private String pathInfo;
private String pathTranslated;
private String contextPath;
private String queryString;
private String requestURI;
private StringBuffer requestURL;
private String servletPath;
private int contentLength;
private String contentType;
private String remoteAddr;
private String remoteHost;
private int remotePort;
private Locale locale;
private Map<String, String> headers;
private Map<String, String[]> paramtreMap;
@Override
public String getParameter(String name) {
String[] value = paramtreMap.get(name);
return value == null || value.length == 0 ? null : String.join(",", value);
}
@Override
public List<String> getParameterNames() {
return new ArrayList<>(paramtreMap.keySet());
}
@Override
public String[] getParameterValues(String name) {
return paramtreMap.get(name);
}
@Override
public Map<String, String[]> getParameterMap() {
return paramtreMap;
}
@Override
public Map<String, String> getHeaders() {
return headers;
}
public void initContext() {
this.setContext(this);
}
}

View File

@@ -3,7 +3,6 @@ package com.gitee.sop.support.context;
import com.alibaba.ttl.TransmittableThreadLocal; import com.alibaba.ttl.TransmittableThreadLocal;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
/** /**
* @author 六如 * @author 六如
@@ -52,11 +51,6 @@ public abstract class OpenContext {
*/ */
public abstract Locale getLocale(); public abstract Locale getLocale();
/**
* 获取请求头
*/
public abstract Map<String, String> getHeaders();
protected void setContext(OpenContext openContext) { protected void setContext(OpenContext openContext) {
THREAD_LOCAL.set(openContext); THREAD_LOCAL.set(openContext);
} }

View File

@@ -0,0 +1,73 @@
package com.gitee.sop.support.context;
import com.alibaba.ttl.TransmittableThreadLocal;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* @author 六如
*/
public abstract class WebContext {
private static final ThreadLocal<WebContext> THREAD_LOCAL = new TransmittableThreadLocal<>();
public abstract String getMethod();
public abstract String getPathInfo();
public abstract String getPathTranslated();
public abstract String getContextPath();
public abstract String getQueryString();
public abstract String getRequestURI();
public abstract StringBuffer getRequestURL();
public abstract String getServletPath();
public abstract int getContentLength();
public abstract String getContentType();
public abstract String getRemoteAddr();
public abstract String getRemoteHost();
public abstract int getRemotePort();
public abstract String getParameter(String name);
public abstract List<String> getParameterNames();
public abstract String[] getParameterValues(String name);
public abstract Map<String, String[]> getParameterMap();
/**
* 获取locale
*/
public abstract Locale getLocale();
/**
* 获取请求头
*/
public abstract Map<String, String> getHeaders();
protected void setContext(WebContext openContext) {
THREAD_LOCAL.set(openContext);
}
/**
* 获取当前WebContext
*
* @return 返回WebContext
*/
public static WebContext current() {
return THREAD_LOCAL.get();
}
}

View File

@@ -1,7 +1,10 @@
package com.gitee.sop.support.dubbo; package com.gitee.sop.support.dubbo;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.gitee.sop.support.constant.SopConstants;
import com.gitee.sop.support.context.DefaultOpenContext; import com.gitee.sop.support.context.DefaultOpenContext;
import com.gitee.sop.support.context.DefaultWebContext;
import com.gitee.sop.support.context.WebContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.common.extension.Activate;
@@ -21,11 +24,10 @@ import org.apache.dubbo.rpc.RpcServiceContext;
@Activate(group = {CommonConstants.PROVIDER}) @Activate(group = {CommonConstants.PROVIDER})
public class DubboProviderTraceFilter implements Filter { public class DubboProviderTraceFilter implements Filter {
private static final String OPEN_CONTEXT = "open.context";
@Override @Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
initOpenContext(); initOpenContext();
initWebContext();
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
long endTime = 0; long endTime = 0;
try { try {
@@ -40,10 +42,19 @@ public class DubboProviderTraceFilter implements Filter {
private void initOpenContext() { private void initOpenContext() {
// 从 ServerAttachment 中读取的参数是从 Client 中传递过来的 // 从 ServerAttachment 中读取的参数是从 Client 中传递过来的
RpcContextAttachment serverAttachment = RpcContext.getServerAttachment(); RpcContextAttachment serverAttachment = RpcContext.getServerAttachment();
String attachment = serverAttachment.getAttachment(OPEN_CONTEXT); Object objectAttachment = serverAttachment.getObjectAttachment(SopConstants.OPEN_CONTEXT);
if (attachment != null) { if (objectAttachment instanceof DefaultOpenContext) {
DefaultOpenContext defaultOpenContext = JSON.parseObject(attachment, DefaultOpenContext.class); DefaultOpenContext openContext = (DefaultOpenContext) objectAttachment;
defaultOpenContext.initContext(); openContext.initContext();
}
}
private void initWebContext() {
RpcContextAttachment serverAttachment = RpcContext.getServerAttachment();
Object objectAttachment = serverAttachment.getObjectAttachment(SopConstants.WEB_CONTEXT);
if (objectAttachment instanceof DefaultWebContext) {
DefaultWebContext webContext = (DefaultWebContext) objectAttachment;
webContext.initContext();
} }
} }

View File

@@ -1,2 +1,3 @@
com.gitee.sop.support.dto.CommonFileData com.gitee.sop.support.dto.CommonFileData
com.gitee.sop.support.context.DefaultOpenContext com.gitee.sop.support.context.DefaultOpenContext
com.gitee.sop.support.context.DefaultWebContext