增加 yudao-spring-boot-starter-env 组件,完成 tag 请求头的读取到上下文
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user