每天20分钟之zuul网关的动态路由

2022-08-14 14:10:58 浏览数 (1)

实现动态路由的网关才是真正可用的网关

  1. 先上代码,基于redis存储了配置信息
  2. 继承SimpleRouteLocator类和实现了RefreshableRouteLocator接口

最最核心的代码了,如果需要实现动态路由,可以基于这个去改造了

代码语言:txt复制
package cn.beckbi.route;

import cn.beckbi.route.entity.ZuulEntity;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
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.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.*;


/**
 * @program: spring-cloud
 * @description:
 * @author: bikang
 * @create: 2022-08-14 13:04
 */
public class DynamicRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {

    private static final String ZUUL_EKY = "LOCAL_ZUUL_RULES_REDIS_KEY";

    private ZuulProperties zuulProperties;

    public static String getRouteKey() {
        return ZUUL_EKY;
    }

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public DynamicRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
        this.zuulProperties = properties;
    }

    @Override
    public void refresh() {
        doRefresh();
    }

    @Override
    protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
        Map<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap(20);

        zuulProperties.getRoutes().clear();

        routesMap.putAll(super.locateRoutes());

        routesMap.putAll(locateRoutesFromRedis());

        zuulProperties.getRoutes().putAll(routesMap);

        return routesMap;
    }

    private Map<? extends String, ? extends ZuulProperties.ZuulRoute> locateRoutesFromRedis() {
        LinkedHashMap routesMap = new LinkedHashMap();

        String routeData = stringRedisTemplate.opsForValue().get(ZUUL_EKY);
        List<ZuulEntity> zuulEntityList = JSONObject.parseObject(routeData, new TypeReference<List<ZuulEntity>>(){});

        for (ZuulEntity zuulEntity: zuulEntityList){
            routesMap.put(zuulEntity.getPath(),new ZuulProperties.ZuulRoute(
                    zuulEntity.getId(),
                    zuulEntity.getPath(),
                    zuulEntity.getServiceId(),
                    zuulEntity.getUrl(),
                    zuulEntity.isStripPrefix(),
                    zuulEntity.getRetryable(),
                    new LinkedHashSet(1)));
        }
        return routesMap;
    }

    @Override
    public Collection<String> getIgnoredPaths() {
        return super.getIgnoredPaths();
    }

    @Override
    public List<Route> getRoutes() {
        return super.getRoutes();
    }

    @Override
    public Route getMatchingRoute(String path) {
        return super.getMatchingRoute(path);
    }
}

刷新路由

  • http://127.0.0.1:9021/refresh

直接转发

  • http://127.0.0.1:9021/abc

基于eureka的路由

  • http://127.0.0.1:9021/user/123

具体代码可以看这个:

https://github.com/beckbikang/spring-cloud/tree/main/kzuul

0 人点赞