Gateway如何使用Nacos动态配置路由

2024-08-03 19:21:14 浏览数 (2)

Gateway如何使用Nacos动态配置路由

一、介绍

在前面,我使用了腾讯云AI工具在配置文件中配置了路由,将配置信息移动到Nacos配置中心也能同样达成效果

那么本篇玩个不一样的,我们去Nacosjson中读取信息,来动态加载我们的路由

二、代码

首先,我要想通过Nacos配置中心得到这么一份json文件,

而且还要时刻监听这份json的变化,一旦文件发生了变动,我们也得时刻更新路由

这就要用到com.alibaba.nacos.api.config.listener.Listener了,我们需要实现这个监听器,同时将其加入到我们的监听队列中

com.alibaba.nacos.api.config.listener.Listener.java源码

代码语言:java复制
package com.alibaba.nacos.api.config.listener;
​
import java.util.concurrent.Executor;
​
public interface Listener {
    Executor getExecutor();
​
    void receiveConfigInfo(String var1);
}

同时,我们启动一个bean实现RouteDefinitionRepository来完成

点入源码,可以注意到,这个接口继承了RouteDefinitionLocatorRouteDefinitionWriter

那么,我们先实现

代码语言:java复制
package com.banmoon.route;
​
import cn.hutool.core.util.StrUtil;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
​
import java.util.*;
import java.util.concurrent.Executor;
​
/**
 * nacos动态路由
 */
@Slf4j
@Component
public class NacosRouteDefinitionRepository implements RouteDefinitionRepository {
​
    private final ApplicationEventPublisher publisher;
    private final NacosConfigProperties nacosConfigProperties;
    private final NacosConfigManager nacosConfigManager;
    private static final ObjectMapper objectMapper = new ObjectMapper();
    public final Map<String, RouteDefinition> routes = Collections.synchronizedMap(new LinkedHashMap<>());
    private static final String DATA_ID = "gateway-router.json";
​
    public NacosRouteDefinitionRepository(ApplicationEventPublisher publisher, NacosConfigProperties nacosConfigProperties) {
        this.publisher = publisher;
        this.nacosConfigProperties = nacosConfigProperties;
        this.nacosConfigManager = new NacosConfigManager(nacosConfigProperties);
        nacosListener();
    }
​
    /**
     * Nacos监听器
     */
    private void nacosListener() {
        try {
            nacosConfigManager.getConfigService().addListener(DATA_ID, nacosConfigProperties.getGroup(), new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }
​
                @Override
                public void receiveConfigInfo(String configInfo) {
                    publisher.publishEvent(new RefreshRoutesEvent(this));
                }
            });
        } catch (NacosException e) {
            log.error("nacosListener error", e);
        }
    }
​
    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        try {
            String routeConfig = nacosConfigManager.getConfigService()
                    .getConfig(DATA_ID, nacosConfigProperties.getGroup(), 3000);
​
            List<RouteDefinition> routeDefinitionList = new ArrayList<>();
            if (StrUtil.isNotBlank(routeConfig)) {
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                routeDefinitionList = objectMapper.readValue(routeConfig, new TypeReference<List<RouteDefinition>>() {
                });
            }
​
            return Flux.fromIterable(routeDefinitionList);
        } catch (Exception e) {
            log.error("getRouteDefinitions error ", e);
        }
​
        return Flux.fromIterable(new ArrayList<>());
    }
​
    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {
        return route.flatMap(r -> {
            log.info("新增路由信息:{}", r);
            routes.put(r.getId(), r);
            return Mono.empty();
        });
    }
​
    @Override
    public Mono<Void> delete(Mono<String> routeId) {
        return Mono.empty();
    }
​
}

再然后,在Nacos中添加配置,gateway-router.json如下

代码语言:json复制
[
    {
        "id": "web-base",
        "uri": "lb://web-base",
        "predicates": [
            {
                "name": "Path",
                "args": {
                    "pattern": "/web/base/**"
                }
            }
        ],
        "filters": [
            {
                "name": "StripPrefix",
                "args": {
                    "_genkey_0": 2
                }
            }
        ]
    },
]

如此便完成了代码Gateway如何使用Nacos动态配置路由

三、最后

实际上,我一直在思考,如何使用两个数据源来共同配置这个动态路由

有点灵感,但不多,一会儿实践一下

尝试失败 不要多次实现RouteDefinitionRepository,一个作为Nacos的,一个作为MySQL数据库的 但它会报错,查找bean的时候找到了两个,只能想想其他的方案了

0 人点赞