增加 yudao-spring-boot-starter-env 组件,完成 tag 请求头的读取到上下文
This commit is contained in:
@@ -13,6 +13,8 @@ public interface WebFilterOrderEnum {
|
||||
|
||||
int TRACE_FILTER = CORS_FILTER + 1;
|
||||
|
||||
int ENV_TAG_FILTER = TRACE_FILTER + 1;
|
||||
|
||||
int REQUEST_BODY_CACHE_FILTER = Integer.MIN_VALUE + 500;
|
||||
|
||||
// OrderedRequestContextFilter 默认为 -105,用于国际化上下文等等
|
||||
|
||||
51
yudao-framework/yudao-spring-boot-starter-env/pom.xml
Normal file
51
yudao-framework/yudao-spring-boot-starter-env/pom.xml
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>yudao-spring-boot-starter-env</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>
|
||||
开发环境拓展,实现类似阿里的特性环境的能力
|
||||
1. https://segmentfault.com/a/1190000018022987
|
||||
</description>
|
||||
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring 核心 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.iocoder.yudao.framework.env.config;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 环境
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Data
|
||||
public class EnvProperties {
|
||||
|
||||
/**
|
||||
* 环境标签
|
||||
*/
|
||||
private String tag;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.iocoder.yudao.framework.env.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
||||
import cn.iocoder.yudao.framework.env.core.web.EnvWebFilter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
||||
public class YudaoEnvWebAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 创建 {@link EnvWebFilter} Bean
|
||||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean<EnvWebFilter> envWebFilterFilter() {
|
||||
EnvWebFilter filter = new EnvWebFilter();
|
||||
FilterRegistrationBean<EnvWebFilter> bean = new FilterRegistrationBean<>(filter);
|
||||
bean.setOrder(WebFilterOrderEnum.ENV_TAG_FILTER);
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.yudao.framework.env.core.context;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 开发环境上下文
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class EnvContextHolder {
|
||||
|
||||
/**
|
||||
* 标签的上下文
|
||||
*
|
||||
* 使用 {@link List} 的原因,可能存在多层设置或者清理
|
||||
*/
|
||||
private static final ThreadLocal<List<String>> tagContext = TransmittableThreadLocal.withInitial(ArrayList::new);
|
||||
|
||||
public static void setTag(String tag) {
|
||||
tagContext.get().add(tag);
|
||||
}
|
||||
|
||||
public static String getTag() {
|
||||
return CollUtil.getLast(tagContext.get());
|
||||
}
|
||||
|
||||
public static void removeTag() {
|
||||
List<String> tags = tagContext.get();
|
||||
if (CollUtil.isEmpty(tags)) {
|
||||
return;
|
||||
}
|
||||
tags.remove(tags.size() - 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.framework.env.core;
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.yudao.framework.env.core.util;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 环境 Utils
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class EnvUtils {
|
||||
|
||||
private static final String HEADER_DUBBO_TAG = "tag";
|
||||
|
||||
private static final String HOST_NAME_VALUE = "${HOSTNAME}";
|
||||
|
||||
public static String getTag(HttpServletRequest request) {
|
||||
String tag = request.getHeader(HEADER_DUBBO_TAG);
|
||||
// 如果请求的是 "${HOSTNAME}",则解析成对应的本地主机名
|
||||
// 目的:特殊逻辑,解决 IDEA Rest Client 不支持环境变量的读取,所以就服务器来做
|
||||
return Objects.equals(tag, HOST_NAME_VALUE) ? NetUtil.getLocalHostName() : tag;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.iocoder.yudao.framework.env.core.web;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.env.core.context.EnvContextHolder;
|
||||
import cn.iocoder.yudao.framework.env.core.util.EnvUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 环境的 {@link javax.servlet.Filter} 实现类
|
||||
* 当有 tag 请求头时,设置到 {@link EnvContextHolder} 的标签上下文
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class EnvWebFilter extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
// 如果没有 tag,则走默认的流程
|
||||
String tag = EnvUtils.getTag(request);
|
||||
if (StrUtil.isEmpty(tag)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果有 tag,则设置到上下文
|
||||
EnvContextHolder.setTag(tag);
|
||||
try {
|
||||
chain.doFilter(request, response);
|
||||
} finally {
|
||||
EnvContextHolder.removeTag();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 开发环境拓展,实现类似阿里的特性环境的能力
|
||||
* 1. https://segmentfault.com/a/1190000018022987
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
package cn.iocoder.yudao.framework.env;
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.iocoder.yudao.framework.env.config.YudaoEnvWebAutoConfiguration
|
||||
@@ -38,7 +38,7 @@
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-common</artifactId> <!-- 兜底,保证在不引入 spring-cloud-starter-dubbo 时,注解等不报错 -->
|
||||
</dependency>
|
||||
<!-- -->
|
||||
<!-- -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.alibaba.cloud</groupId>-->
|
||||
<!-- <artifactId>spring-cloud-starter-dubbo</artifactId>-->
|
||||
|
||||
Reference in New Issue
Block a user