master
This commit is contained in:
43
.gitignore
vendored
Normal file
43
.gitignore
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
|
||||
#日志文件
|
||||
logs
|
||||
/.idea/
|
||||
119
pom.xml
Normal file
119
pom.xml
Normal file
@@ -0,0 +1,119 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<version>2.6.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.deepinnet</groupId>
|
||||
<artifactId>deepinnet-dubbo-tag-starter</artifactId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<logback.version>1.2.7</logback.version>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>3.1.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>transmittable-thread-local</artifactId>
|
||||
<version>2.14.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-to-slf4j</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- 要将源码放上去,需要加入这个插件 -->
|
||||
<plugin>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<configuration>
|
||||
<attach>true</attach>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 发布 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
20
src/main/java/com/deepinnet/tag/EnvContext.java
Normal file
20
src/main/java/com/deepinnet/tag/EnvContext.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.deepinnet.tag;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
public class EnvContext {
|
||||
|
||||
private static final ThreadLocal<String> DUBBO_TAG_CONTEXT = TransmittableThreadLocal.withInitial(() -> "");
|
||||
|
||||
public static void setDubboTag(String dubboTag) {
|
||||
DUBBO_TAG_CONTEXT.set(dubboTag);
|
||||
}
|
||||
|
||||
public static String getDubboTag() {
|
||||
return DUBBO_TAG_CONTEXT.get();
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
DUBBO_TAG_CONTEXT.remove();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.deepinnet.tag.constants;
|
||||
|
||||
public interface TagKeyConstants {
|
||||
|
||||
String TAG_KEY = "dubbo.tag";
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.deepinnet.tag.filter;
|
||||
|
||||
|
||||
import com.deepinnet.tag.EnvContext;
|
||||
import com.deepinnet.tag.constants.TagKeyConstants;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.apache.dubbo.rpc.Invocation;
|
||||
import org.apache.dubbo.rpc.Invoker;
|
||||
import org.apache.dubbo.rpc.Result;
|
||||
import org.apache.dubbo.rpc.RpcException;
|
||||
import org.apache.dubbo.rpc.cluster.filter.ClusterFilter;
|
||||
|
||||
/**
|
||||
* 用于传递dubbo调用链路中的tag的过滤器
|
||||
* 必须实现ClusterFilter。不能通过扩展Filter的方式来进行流量的打标的原因是因为Dubbo的调用链中到Filter这里就已经完成了路由选址和负载均衡
|
||||
*/
|
||||
@Activate(group = CommonConstants.CONSUMER)
|
||||
public class DubboConsumerTagFilter implements ClusterFilter {
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
String tagValue = EnvContext.getDubboTag();
|
||||
// 设置到上下文中,在后续调用链路中传递
|
||||
if (StringUtils.isNotBlank(tagValue)) {
|
||||
invocation.setAttachment(TagKeyConstants.TAG_KEY, tagValue);
|
||||
}
|
||||
|
||||
// 继续执行
|
||||
return invoker.invoke(invocation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.deepinnet.tag.filter;
|
||||
|
||||
|
||||
import com.deepinnet.tag.EnvContext;
|
||||
import com.deepinnet.tag.constants.TagKeyConstants;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.apache.dubbo.rpc.*;
|
||||
|
||||
/**
|
||||
* 用于传递dubbo调用链路中的tag的过滤器
|
||||
* 必须实现ClusterFilter。不能通过扩展Filter的方式来进行流量的打标的原因是因为Dubbo的调用链中到Filter这里就已经完成了路由选址和负载均衡
|
||||
*/
|
||||
@Activate(group = CommonConstants.PROVIDER)
|
||||
public class DubboProviderTagFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
String tagValue = invocation.getAttachment(TagKeyConstants.TAG_KEY);
|
||||
if (StringUtils.isNotBlank(tagValue)) {
|
||||
EnvContext.setDubboTag(tagValue);
|
||||
}
|
||||
|
||||
// 继续执行
|
||||
return invoker.invoke(invocation);
|
||||
}
|
||||
}
|
||||
50
src/main/java/com/deepinnet/tag/filter/RestTagFilter.java
Normal file
50
src/main/java/com/deepinnet/tag/filter/RestTagFilter.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.deepinnet.tag.filter;
|
||||
|
||||
import com.deepinnet.tag.EnvContext;
|
||||
import com.deepinnet.tag.constants.TagKeyConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* tag的rest请求过滤器,将tag从请求头中取出,放入EnvContext中
|
||||
*/
|
||||
@Slf4j
|
||||
public class RestTagFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
Filter.super.init(filterConfig);
|
||||
log.info("Web 过滤器:restTagFilter 初始化");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
try {
|
||||
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||
String tagValue = request.getHeader(TagKeyConstants.TAG_KEY);
|
||||
if (StringUtils.isNotBlank(tagValue)) {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
||||
requestAttributes.setAttribute(TagKeyConstants.TAG_KEY, tagValue, RequestAttributes.SCOPE_REQUEST);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes, true);
|
||||
// 设置tag
|
||||
EnvContext.setDubboTag(tagValue);
|
||||
}
|
||||
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
} finally {
|
||||
EnvContext.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
Filter.super.destroy();
|
||||
log.info("Web 过滤器:restTagFilter 销毁");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
dubboProviderTagFilter=com.deepinnet.tag.filter.DubboProviderTagFilter
|
||||
@@ -0,0 +1 @@
|
||||
dubboConsumerTagFilter=com.deepinnet.tag.filter.DubboConsumerTagFilter
|
||||
1
src/main/resources/META-INF/spring.factories
Normal file
1
src/main/resources/META-INF/spring.factories
Normal file
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.deepinnet.tag.filter.RestTagFilter
|
||||
Reference in New Issue
Block a user