Spring Cloud Gateway 使用ribbon以及nacos实现灰度发布

1、Spring Cloud Gateway配置文件

java 复制代码
gateway:
  userId-limit: 1000
agent-bff:
  ribbon:
    NFLoadBalancerRuleClassName: com.anlitech.gateway.gray.GrayRule
operator-bff:
  ribbon:
    NFLoadBalancerRuleClassName: com.anlitech.gateway.gray.GrayRule
spring:
  cloud:
    gateway:
      locator:
        enabled: true
      routes:
        - id: operator-bff
          uri: lb://operator-bff
          predicates:
            - Path=/operatorPortal/**
        - id: agent-bff
          uri: lb://agent-bff
          predicates:
            - Path=/agentPortal/**

2、Spring Cloud Gateway配置类

java 复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;


@Data
@Component
@RefreshScope
@ConfigurationProperties("gateway")
public class GatewayConfigProperties {
    /**
     * 用户id灰度阈值
     */
    private Long userIdLimit;

}

3、Spring Cloud Gateway的Ribbon请求上下文持有器

java 复制代码
import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.UtilityClass;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ronshi
 * @date 2025/3/17 14:38
 */
@UtilityClass
public class RibbonRequestContextHolder {
    private final ThreadLocal<Map<String, String>> CONTEXT_HOLDER = new TransmittableThreadLocal<Map<String, String>>() {
        @Override
        protected Map<String, String> initialValue() {
            return new HashMap<>(16);
        }
    };

    /**
     * 获取当前线程的上下文Map
     */
    public Map<String, String> getCurrentContext() {
        return CONTEXT_HOLDER.get();
    }

    /**
     * 向当前上下文添加键值
     */
    public void put(String key, String value) {
        getCurrentContext().put(key, value);
    }

    /**
     * 从当前上下文获取值
     */
    public static String get(String key) {
        return getCurrentContext().get(key);
    }

    /**
     * 清理当前线程上下文(防止内存泄漏)
     */
    public void clear() {
        CONTEXT_HOLDER.remove();
    }
}

4、Spring Cloud Gateway的用户ID拦截器

java 复制代码
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


@Slf4j
@Component
@RequiredArgsConstructor
public class CommonGlobalFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String idStr = request.getQueryParams().getFirst("userId");
        RibbonRequestContextHolder.put("traffic-userId", idStr);
        return chain.filter(exchange).doFinally(signal -> RibbonRequestContextHolder.clear());
    }

    @Override
    public int getOrder() {
        return -300;
    }


}

5、Spring Cloud Gateway自定义 Ribbon 灰度规则

java 复制代码
import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;
import com.anlitech.gateway.config.GatewayConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ronshi
 * @date 2025/3/18 14:24
 */
public class GrayRule extends RoundRobinRule {
    @Autowired
    private GatewayConfigProperties gatewayConfigProperties;

    @Override
    public Server choose(Object key) {
        Long userIdLimit= gatewayConfigProperties.getUserIdLimit();
        String userId = RibbonRequestContextHolder.get("traffic-userId");
        String grayTag = userId == null || Long.parseLong(userId) < userIdLimit ? "gray" : "normal";
        List<Server> servers = getLoadBalancer().getReachableServers();
        List<Server> matchedServers = servers.stream()
            .filter(server -> {
                Map<String, String> metadata = ((NacosServer) server).getInstance().getMetadata();
                return metadata.getOrDefault("traffic-group", "normal").equals(grayTag);
            })
            .collect(Collectors.toList());

        // 处理空列表情况:回退到原始负载均衡器
        if (matchedServers.isEmpty()) {
            //return super.choose(getLoadBalancer(), key);
        }

        // 创建临时负载均衡器,仅包含匹配的服务器
        BaseLoadBalancer loadBalancer = new BaseLoadBalancer();
        loadBalancer.addServers(matchedServers);
        return super.choose(loadBalancer, key);
    }
}

6、BFF服务增加nacos元数据的灰度标识

java 复制代码
spring:
  application:
    # 服务名
    name: @artifactId@
  cloud:
    # nacos注册和配置中心相关配置
    nacos:
      discovery:
        server-addr: localhost:8848
        metadata:
          traffic-group: gray

上述方案即可实现根据用户ID进入灰度。ID不存在或者ID<1000进入灰度服务,否则进入普通服务。确保agent-bff和operator-bff的Ribbon客户端配置独立,避免共享实例列表。

相关推荐
述雾学java10 天前
Spring Cloud 服务调用详解:Ribbon 负载均衡与 Feign 声明式接口调用
spring cloud·ribbon·负载均衡
漫步者TZ11 天前
【Nacos系列】服务发现:一致性实现
nacos·服务发现
Byte Beat11 天前
springCloudAlibaba最新版本2023.0.3.2 与nacos3.0的连接不上问题记录
java·spring cloud·nacos
yuluo_YX12 天前
AI Gateway 介绍
人工智能·gateway
网硕互联的小客服15 天前
502 Bad Gateway:服务器作为网关或代理时收到无效响应处理方式
运维·服务器·gateway
sevevty-seven15 天前
什么是Gateway
gateway
秋意零15 天前
【排坑指南】MySQL初始化后,Nacos与微服务无法连接??
运维·数据库·mysql·微服务·nacos·报错
秋の花15 天前
【GateWay】和权限验证
java·gateway
Mr_hwt_12316 天前
基于nacos和gateway搭建微服务管理平台详细教程
java·spring boot·spring cloud·微服务·nacos
欧先生^_^16 天前
org.springframework.cloud.gateway 组件解释
gateway