mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-12 21:49:01 +08:00
✨ Introducing new features. #1935 灰度路由支持非WEB调用传递
This commit is contained in:
1 parent
590620c651
commit
cf1d4c8f06
4 files changed
+71
-2
No files matched your search
+2
-1
@@ -20,6 +20,7 @@ package com.pig4cloud.pigx.common.gray.feign;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||||
import com.pig4cloud.pigx.common.core.util.WebUtils;
|
import com.pig4cloud.pigx.common.core.util.WebUtils;
|
||||||
|
import com.pig4cloud.pigx.common.gray.support.NonWebVersionContextHolder;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import feign.RequestTemplate;
|
import feign.RequestTemplate;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -41,7 +42,7 @@ public class GrayFeignRequestInterceptor implements RequestInterceptor {
|
|||||||
@Override
|
@Override
|
||||||
public void apply(RequestTemplate template) {
|
public void apply(RequestTemplate template) {
|
||||||
String reqVersion = WebUtils.getRequest() != null ? WebUtils.getRequest().getHeader(CommonConstants.VERSION)
|
String reqVersion = WebUtils.getRequest() != null ? WebUtils.getRequest().getHeader(CommonConstants.VERSION)
|
||||||
: null;
|
: NonWebVersionContextHolder.getVersion();
|
||||||
|
|
||||||
if (StrUtil.isNotBlank(reqVersion)) {
|
if (StrUtil.isNotBlank(reqVersion)) {
|
||||||
log.debug("feign gray add header version :{}", reqVersion);
|
log.debug("feign gray add header version :{}", reqVersion);
|
||||||
|
|||||||
+3
-1
@@ -28,6 +28,7 @@ import com.netflix.loadbalancer.ILoadBalancer;
|
|||||||
import com.netflix.loadbalancer.Server;
|
import com.netflix.loadbalancer.Server;
|
||||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||||
import com.pig4cloud.pigx.common.core.util.WebUtils;
|
import com.pig4cloud.pigx.common.core.util.WebUtils;
|
||||||
|
import com.pig4cloud.pigx.common.gray.support.NonWebVersionContextHolder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -60,7 +61,8 @@ public class GrayRibbonLoadBalancerRule extends AbstractLoadBalancerRule {
|
|||||||
|
|
||||||
// 获取请求version,无则随机返回可用实例
|
// 获取请求version,无则随机返回可用实例
|
||||||
String reqVersion = WebUtils.getRequest() != null ? WebUtils.getRequest().getHeader(CommonConstants.VERSION)
|
String reqVersion = WebUtils.getRequest() != null ? WebUtils.getRequest().getHeader(CommonConstants.VERSION)
|
||||||
: null;
|
: NonWebVersionContextHolder.getVersion();
|
||||||
|
|
||||||
if (StrUtil.isBlank(reqVersion)) {
|
if (StrUtil.isBlank(reqVersion)) {
|
||||||
return reachableServers.get(RandomUtil.randomInt(reachableServers.size()));
|
return reachableServers.get(RandomUtil.randomInt(reachableServers.size()));
|
||||||
}
|
}
|
||||||
|
|||||||
+54
@@ -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
|
||||||
|
* <p>
|
||||||
|
* 灰度版本号传递工具 ,在非web 调用feign 传递之前手动setVersion
|
||||||
|
*/
|
||||||
|
@UtilityClass
|
||||||
|
public class NonWebVersionContextHolder {
|
||||||
|
|
||||||
|
private final ThreadLocal<String> THREAD_LOCAL_VERSION = new TransmittableThreadLocal<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TTL 设置版本号<br/>
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Executable
+12
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"name": "gray.rule.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": false,
|
||||||
|
"sourceType": "com.pig4cloud.pigx.common.gray.GrayRibbonLoadBalancerConfiguration",
|
||||||
|
"description": "是否开启灰度路由功能"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hints": []
|
||||||
|
}
|
||||||
Reference in new issue
Block a user