进一步完善 Dubbo 路由封装
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package cn.iocoder.mall.dubbo.config;
|
||||
|
||||
import cn.iocoder.common.framework.util.OSUtils;
|
||||
import cn.iocoder.common.framework.util.StringUtils;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Dubbo 配置项的后置处理器,主要目的如下:
|
||||
*
|
||||
* 1. 生成 {@link #DUBBO_TAG_PROPERTIES_KEY} 配置项,可用于本地开发环境下的 dubbo.provider.tag 配置项
|
||||
*/
|
||||
public class DubboEnvironmentPostProcessor implements EnvironmentPostProcessor {
|
||||
|
||||
/**
|
||||
* 默认配置项的 PropertySource 名字
|
||||
*/
|
||||
private static final String PROPERTY_SOURCE_NAME = "mallDubboProperties";
|
||||
|
||||
/**
|
||||
* Dubbo 路由标签属性 KEY
|
||||
*/
|
||||
private static final String DUBBO_TAG_PROPERTIES_KEY = "DUBBO_TAG";
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
// 需要修改的配置项
|
||||
Map<String, Object> modifyProperties = new HashMap<>();
|
||||
// 生成 DUBBO_TAG_PROPERTIES_KEY,使用 hostname
|
||||
String dubboTag = OSUtils.getHostName();
|
||||
if (!StringUtils.hasText(dubboTag)) {
|
||||
dubboTag = StringUtils.uuid(true); // 兜底,强行生成一个
|
||||
}
|
||||
modifyProperties.put(DUBBO_TAG_PROPERTIES_KEY, dubboTag);
|
||||
// 添加到 environment 中,排在最优,最低优先级
|
||||
addOrReplace(environment.getPropertySources(), modifyProperties);
|
||||
}
|
||||
|
||||
private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
|
||||
if (CollectionUtils.isEmpty(map)) {
|
||||
return;
|
||||
}
|
||||
// 情况一,如果存在 defaultProperties 的 PropertySource,则进行 key 的修改
|
||||
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
|
||||
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
|
||||
if (source instanceof MapPropertySource) {
|
||||
MapPropertySource target = (MapPropertySource) source;
|
||||
for (String key : map.keySet()) {
|
||||
target.getSource().put(key, map.get(key));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 情况二,不存在 defaultProperties 的 PropertySource,则直接添加到其中
|
||||
propertySources.addLast(new MapPropertySource(PROPERTY_SOURCE_NAME, map));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -51,6 +51,7 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
|
||||
appResponse.setValue(CommonResult.error((ServiceException) exception));
|
||||
// 2.2 如果是 GlobalException 全局异常,则直接抛出
|
||||
} else {
|
||||
// TODO 优化点:尝试修改成 RpcException
|
||||
appResponse.setException(exception);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package cn.iocoder.mall.dubbo.core.web;
|
||||
|
||||
import cn.iocoder.common.framework.util.OSUtils;
|
||||
import cn.iocoder.common.framework.util.StringUtils;
|
||||
import cn.iocoder.mall.dubbo.core.cluster.interceptor.DubboConsumerRouterTagClusterInterceptor;
|
||||
import cn.iocoder.mall.dubbo.core.filter.DubboProviderRouterTagFilter;
|
||||
import cn.iocoder.mall.dubbo.core.router.DubboRouterTagContextHolder;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@@ -22,12 +21,18 @@ public class DubboRouterTagWebInterceptor implements HandlerInterceptor {
|
||||
|
||||
private static final String HEADER_DUBBO_TAG = "dubbo-tag";
|
||||
|
||||
private static final String HOST_NAME_VALUE = "${HOSTNAME}";
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
String tag = request.getHeader(HEADER_DUBBO_TAG);
|
||||
if (StringUtils.hasText(tag)) {
|
||||
// 特殊逻辑,解决 IDEA Rest Client 不支持环境变量的读取,所以就服务器来做
|
||||
if (HOST_NAME_VALUE.equals(tag)) {
|
||||
tag = OSUtils.getHostName();
|
||||
}
|
||||
// 设置到 DubboRouterTagContextHolder 上下文
|
||||
DubboRouterTagContextHolder.setTag(tag);
|
||||
RpcContext.getContext().setAttachment(CommonConstants.TAG_KEY, tag);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user