diff --git a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/feign/GrayFeignRequestInterceptor.java b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/feign/GrayFeignRequestInterceptor.java index 77747f2e2..507499d7a 100644 --- a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/feign/GrayFeignRequestInterceptor.java +++ b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/feign/GrayFeignRequestInterceptor.java @@ -20,6 +20,7 @@ package com.pig4cloud.pigx.common.gray.feign; import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.util.WebUtils; +import com.pig4cloud.pigx.common.gray.support.NonWebVersionContextHolder; import feign.RequestInterceptor; import feign.RequestTemplate; import lombok.extern.slf4j.Slf4j; @@ -41,7 +42,7 @@ public class GrayFeignRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { String reqVersion = WebUtils.getRequest() != null ? WebUtils.getRequest().getHeader(CommonConstants.VERSION) - : null; + : NonWebVersionContextHolder.getVersion(); if (StrUtil.isNotBlank(reqVersion)) { log.debug("feign gray add header version :{}", reqVersion); diff --git a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRibbonLoadBalancerRule.java b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRibbonLoadBalancerRule.java index 01d650d38..49d6f492d 100644 --- a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRibbonLoadBalancerRule.java +++ b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRibbonLoadBalancerRule.java @@ -28,6 +28,7 @@ import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.Server; import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.util.WebUtils; +import com.pig4cloud.pigx.common.gray.support.NonWebVersionContextHolder; import lombok.extern.slf4j.Slf4j; import java.util.List; @@ -60,7 +61,8 @@ public class GrayRibbonLoadBalancerRule extends AbstractLoadBalancerRule { // 获取请求version,无则随机返回可用实例 String reqVersion = WebUtils.getRequest() != null ? WebUtils.getRequest().getHeader(CommonConstants.VERSION) - : null; + : NonWebVersionContextHolder.getVersion(); + if (StrUtil.isBlank(reqVersion)) { return reachableServers.get(RandomUtil.randomInt(reachableServers.size())); } diff --git a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/support/NonWebVersionContextHolder.java b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/support/NonWebVersionContextHolder.java new file mode 100644 index 000000000..1cc16e953 --- /dev/null +++ b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/support/NonWebVersionContextHolder.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gray.support; + +import com.alibaba.ttl.TransmittableThreadLocal; +import lombok.experimental.UtilityClass; + +/** + * @author lengleng + * @date 2020-10-16 + *

+ * 灰度版本号传递工具 ,在非web 调用feign 传递之前手动setVersion + */ +@UtilityClass +public class NonWebVersionContextHolder { + + private final ThreadLocal THREAD_LOCAL_VERSION = new TransmittableThreadLocal<>(); + + /** + * TTL 设置版本号
+ * @param version 版本号 + */ + public void setVersion(String version) { + THREAD_LOCAL_VERSION.set(version); + } + + /** + * 获取TTL中的版本号 + * @return 版本 || null + */ + public String getVersion() { + return THREAD_LOCAL_VERSION.get(); + } + + public void clear() { + THREAD_LOCAL_VERSION.remove(); + } + +} diff --git a/pigx-common/pigx-common-gray/src/main/resources/META-INF/spring-configuration-metadata.json b/pigx-common/pigx-common-gray/src/main/resources/META-INF/spring-configuration-metadata.json new file mode 100755 index 000000000..182991096 --- /dev/null +++ b/pigx-common/pigx-common-gray/src/main/resources/META-INF/spring-configuration-metadata.json @@ -0,0 +1,12 @@ +{ + "properties": [ + { + "name": "gray.rule.enabled", + "type": "java.lang.Boolean", + "defaultValue": false, + "sourceType": "com.pig4cloud.pigx.common.gray.GrayRibbonLoadBalancerConfiguration", + "description": "是否开启灰度路由功能" + } + ], + "hints": [] +}