Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
97b931f7
提交
97b931f7
authored
6月 25, 2022
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完善 gateway 服务,支持 tag 过滤服务实例
上级
b8fb106a
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
74 行增加
和
4 行删除
+74
-4
pom.xml
pom.xml
+0
-1
pom.xml
yudao-framework/pom.xml
+1
-0
GrayLoadBalancer.java
...n/iocoder/yudao/gateway/filter/grey/GrayLoadBalancer.java
+32
-2
EnvUtils.java
...src/main/java/cn/iocoder/yudao/gateway/util/EnvUtils.java
+39
-0
WebFrameworkUtils.java
...java/cn/iocoder/yudao/gateway/util/WebFrameworkUtils.java
+2
-1
没有找到文件。
pom.xml
浏览文件 @
97b931f7
...
@@ -16,7 +16,6 @@
...
@@ -16,7 +16,6 @@
<!-- <module>yudao-module-bpm</module>-->
<!-- <module>yudao-module-bpm</module>-->
<module>
yudao-module-system
</module>
<module>
yudao-module-system
</module>
<module>
yudao-module-infra
</module>
<module>
yudao-module-infra
</module>
<module>
yudao-spring-boot-starter-env
</module>
<!-- <module>yudao-module-pay</module>-->
<!-- <module>yudao-module-pay</module>-->
</modules>
</modules>
...
...
yudao-framework/pom.xml
浏览文件 @
97b931f7
...
@@ -11,6 +11,7 @@
...
@@ -11,6 +11,7 @@
<packaging>
pom
</packaging>
<packaging>
pom
</packaging>
<modules>
<modules>
<module>
yudao-common
</module>
<module>
yudao-common
</module>
<module>
yudao-spring-boot-starter-env
</module>
<module>
yudao-spring-boot-starter-banner
</module>
<module>
yudao-spring-boot-starter-banner
</module>
<module>
yudao-spring-boot-starter-mybatis
</module>
<module>
yudao-spring-boot-starter-mybatis
</module>
<module>
yudao-spring-boot-starter-redis
</module>
<module>
yudao-spring-boot-starter-redis
</module>
...
...
yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/grey/GrayLoadBalancer.java
浏览文件 @
97b931f7
...
@@ -3,6 +3,7 @@ package cn.iocoder.yudao.gateway.filter.grey;
...
@@ -3,6 +3,7 @@ package cn.iocoder.yudao.gateway.filter.grey;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.iocoder.yudao.framework.common.util.collection.CollectionUtils
;
import
cn.iocoder.yudao.framework.common.util.collection.CollectionUtils
;
import
cn.iocoder.yudao.gateway.util.EnvUtils
;
import
com.alibaba.cloud.nacos.balancer.NacosBalancer
;
import
com.alibaba.cloud.nacos.balancer.NacosBalancer
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -62,7 +63,7 @@ public class GrayLoadBalancer implements ReactorServiceInstanceLoadBalancer {
...
@@ -62,7 +63,7 @@ public class GrayLoadBalancer implements ReactorServiceInstanceLoadBalancer {
return
new
EmptyResponse
();
return
new
EmptyResponse
();
}
}
// 筛选满足条件的实例列表
// 筛选满足
version
条件的实例列表
String
version
=
headers
.
getFirst
(
VERSION
);
String
version
=
headers
.
getFirst
(
VERSION
);
List
<
ServiceInstance
>
chooseInstances
;
List
<
ServiceInstance
>
chooseInstances
;
if
(
StrUtil
.
isEmpty
(
version
))
{
if
(
StrUtil
.
isEmpty
(
version
))
{
...
@@ -70,12 +71,41 @@ public class GrayLoadBalancer implements ReactorServiceInstanceLoadBalancer {
...
@@ -70,12 +71,41 @@ public class GrayLoadBalancer implements ReactorServiceInstanceLoadBalancer {
}
else
{
}
else
{
chooseInstances
=
CollectionUtils
.
filterList
(
instances
,
instance
->
version
.
equals
(
instance
.
getMetadata
().
get
(
"version"
)));
chooseInstances
=
CollectionUtils
.
filterList
(
instances
,
instance
->
version
.
equals
(
instance
.
getMetadata
().
get
(
"version"
)));
if
(
CollUtil
.
isEmpty
(
chooseInstances
))
{
if
(
CollUtil
.
isEmpty
(
chooseInstances
))
{
log
.
warn
(
"[getInstanceResponse][serviceId({}) 没有满足版本
的服务实例列表,直接使用所有服务实例列表]"
,
serviceId
);
log
.
warn
(
"[getInstanceResponse][serviceId({}) 没有满足版本
({})的服务实例列表,直接使用所有服务实例列表]"
,
serviceId
,
version
);
chooseInstances
=
instances
;
chooseInstances
=
instances
;
}
}
}
}
// 基于 tag 过滤实例列表
chooseInstances
=
filterTagServiceInstances
(
chooseInstances
,
headers
);
// 随机 + 权重获取实例列表 TODO 芋艿:目前直接使用 Nacos 提供的方法,如果替换注册中心,需要重新失败该方法
// 随机 + 权重获取实例列表 TODO 芋艿:目前直接使用 Nacos 提供的方法,如果替换注册中心,需要重新失败该方法
return
new
DefaultResponse
(
NacosBalancer
.
getHostByRandomWeight3
(
chooseInstances
));
return
new
DefaultResponse
(
NacosBalancer
.
getHostByRandomWeight3
(
chooseInstances
));
}
}
/**
* 基于 tag 请求头,过滤匹配 tag 的服务实例列表
*
* copy from EnvLoadBalancerClient
*
* @param instances 服务实例列表
* @param headers 请求头
* @return 服务实例列表
*/
private
List
<
ServiceInstance
>
filterTagServiceInstances
(
List
<
ServiceInstance
>
instances
,
HttpHeaders
headers
)
{
// 情况一,没有 tag 时,直接返回
String
tag
=
EnvUtils
.
getTag
(
headers
);
if
(
StrUtil
.
isEmpty
(
tag
))
{
return
instances
;
}
// 情况二,有 tag 时,使用 tag 匹配服务实例
List
<
ServiceInstance
>
chooseInstances
=
CollectionUtils
.
filterList
(
instances
,
instance
->
tag
.
equals
(
EnvUtils
.
getTag
(
instance
)));
if
(
CollUtil
.
isEmpty
(
chooseInstances
))
{
log
.
warn
(
"[filterTagServiceInstances][serviceId({}) 没有满足 tag({}) 的服务实例列表,直接使用所有服务实例列表]"
,
serviceId
,
tag
);
chooseInstances
=
instances
;
}
return
chooseInstances
;
}
}
}
yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/util/EnvUtils.java
0 → 100644
浏览文件 @
97b931f7
package
cn
.
iocoder
.
yudao
.
gateway
.
util
;
import
cn.hutool.core.net.NetUtil
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.StrUtil
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.http.HttpHeaders
;
import
java.util.Objects
;
/**
* 环境 Utils
*
* copy from yudao-spring-boot-starter-env 的 EnvUtils 类
*
* @author 芋道源码
*/
public
class
EnvUtils
{
private
static
final
String
HEADER_TAG
=
"tag"
;
public
static
final
String
HOST_NAME_VALUE
=
"${HOSTNAME}"
;
public
static
String
getTag
(
HttpHeaders
headers
)
{
String
tag
=
headers
.
getFirst
(
HEADER_TAG
);
// 如果请求的是 "${HOSTNAME}",则解析成对应的本地主机名
// 目的:特殊逻辑,解决 IDEA Rest Client 不支持环境变量的读取,所以就服务器来做
return
Objects
.
equals
(
tag
,
HOST_NAME_VALUE
)
?
getHostName
()
:
tag
;
}
public
static
String
getTag
(
ServiceInstance
instance
)
{
return
instance
.
getMetadata
().
get
(
HEADER_TAG
);
}
public
static
String
getHostName
()
{
return
StrUtil
.
blankToDefault
(
NetUtil
.
getLocalHostName
(),
IdUtil
.
fastSimpleUUID
());
}
}
yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/util/WebFrameworkUtils.java
浏览文件 @
97b931f7
...
@@ -26,9 +26,10 @@ import reactor.core.publisher.Mono;
...
@@ -26,9 +26,10 @@ import reactor.core.publisher.Mono;
@Slf4j
@Slf4j
public
class
WebFrameworkUtils
{
public
class
WebFrameworkUtils
{
@SuppressWarnings
(
"UastIncorrectHttpHeaderInspection"
)
private
static
final
String
HEADER_TENANT_ID
=
"tenant-id"
;
private
static
final
String
HEADER_TENANT_ID
=
"tenant-id"
;
private
static
final
String
HEADER_TAG
=
"tag"
;
private
WebFrameworkUtils
()
{}
private
WebFrameworkUtils
()
{}
/**
/**
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论