欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring cloud zuul 实现动态路由

程序员文章站 2022-06-03 19:01:48
...

前期工作

服务提供者的eureka添加配置

# 配置 server.servlet.context-path                必须配置,不可单独为反斜杠 /
server.servlet.context-path=/user/
# 在eurek的metadata-map 中存入 server.servlet.context-path
eureka.instance.metadata-map.context-path=${server.servlet.context-path} 

改造zuul 自定义网关路由规则

package cn.zhangfusheng.zuul.server.config;

import com.netflix.appinfo.InstanceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fusheng.zhang
 * @Description 网关动态路由
 * @create 2020-04-17 20:04:00
 */
@Slf4j
@Configuration
public class ZuulRoutesConfig {

    /**
     * 注入 discoveryClient 用于获取注册中心的服务提供者列表
     */
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    ZuulProperties zuulProperties;
    @Autowired
    ServerProperties serverProperties;

    @Bean
    public DynamicRoutes dynamicRoutes() {
        return new DynamicRoutes(this.serverProperties.getServlet().getContextPath(), this.zuulProperties);
    }

    /**
     * 动态路由
     * 路由规则: /${eureka.instance.metadata-map.context-path}/2020-04-20/**
     * 目的: 路由一天一变化__未测试
     */
    class DynamicRoutes extends SimpleRouteLocator {

        public DynamicRoutes(String servletPath, ZuulProperties properties) {
            super(servletPath, properties);
        }

        /**
         * 重写 SimpleRouteLocator.locateRoutes()方法
         * 自定义路由生成方式
         * @return
         */
        @Override
        protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
            Map<String, ZuulProperties.ZuulRoute> routes = new LinkedHashMap<>();
            // 获取服务名称列表[serviceId]
            discoveryClient.getServices().forEach(serviceId -> {
                // 获取服务名称对应的实例
                discoveryClient.getInstances(serviceId).forEach(instance -> {
                    InstanceInfo instanceInfo = ((EurekaServiceInstance) instance).getInstanceInfo();
                    // 从 eureka.instance.metadata-map中获取context-path,既服务提供者的server.servlet.context-path
                    // 如果获取不到,则采用服务名称'-'切割的方式作为路由
                    String key = instanceInfo.getMetadata().getOrDefault(
                            "context-path", String.format("/%s/", instanceInfo.getSecureVipAddress().split("-")[0]));
                    // 今天的日期
                    String localDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    // 生成 /${eureka.instance.metadata-map.context-path}/2020-04-20/** 格式的key
                    key = String.format("%s%s/**", key, localDate);
                    ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
                    zuulRoute.setLocation(instanceInfo.getSecureVipAddress());
                    zuulRoute.setId(instanceInfo.getSecureVipAddress());
                    zuulRoute.setPath(key);
                    zuulRoute.setServiceId(instanceInfo.getSecureVipAddress());
                    routes.put(key, zuulRoute);
                });
            });
            return routes;
        }

        @Override
        public List<Route> getRoutes() {
            List<Route> values = new ArrayList<>();
            for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : locateRoutes().entrySet()) {
                values.add(getRoute(entry.getValue(), entry.getValue().getPath()));
            }
            return values;
        }
    }
}