Spring Cloud Alibaba-Gateway搭配Nacos实现动态路由(4)

路由信息定义在配置文件中,这种方式有一个缺点就是修改路由信息必须重启服务才能生效。网关作为全部流量的入口,可用时间当然越长越好,不重启服务而修改路由是一个更好的选择,结合Nacos可以做到这一点。

Spring Cloud Gateway本身还不支持直接从Nacos动态加载路由配置表,需要自己编写监听器监听配置变化并刷新路由表。

NacosDynamicRouteService.java

@Component
public class NacosDynamicRouteService implements ApplicationEventPublisherAware {
 
    private String dataId = "gateway-router";
 
    private String group = "DEFAULT_GROUP";
 
    @Value("${spring.cloud.nacos.config.server-addr}")
    private String serverAddr;
 
    @Autowired
    private RouteDefinitionWriter routeDefinitionWriter;
 
    private ApplicationEventPublisher applicationEventPublisher;
 
    private static final List<String> ROUTE_LIST = new ArrayList<>();
 
    @PostConstruct
    public void dynamicRouteByNacosListener() {
        try {
            ConfigService configService = NacosFactory.createConfigService(serverAddr);
            configService.getConfig(dataId, group, 5000);
            configService.addListener(dataId, group, new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    clearRoute();
                    try {
                        List<RouteDefinition> gatewayRouteDefinitions = JSONObject.parseArray(configInfo, RouteDefinition.class);
                        for (RouteDefinition routeDefinition : gatewayRouteDefinitions) {
                            addRoute(routeDefinition);
                        }
                        publish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
 
                @Override
                public Executor getExecutor() {
                    return null;
                }
            });
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
 
    private void clearRoute() {
        for(String id : ROUTE_LIST) {
            this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
        }
        ROUTE_LIST.clear();
    }
 
    private void addRoute(RouteDefinition definition) {
        try {
            routeDefinitionWriter.save(Mono.just(definition)).subscribe();
            ROUTE_LIST.add(definition.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void publish() {
        this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
    }
 
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
}

代码中监听的配置ID为gateway-router,按此ID在Nacos中创建配置

从bootstrap.yml中删除路由配置,即删除以下内容

      routes:
      - id: payment-router
        uri: lb://payment-service
        predicates:
        - Path=/pay/**
        filters:
        - name: RequestRateLimiter
          args:
            redis-rate-limiter.replenishRate: 1
            redis-rate-limiter.burstCapacity: 5
            key-resolver: '#{@ipKeyResolver}'

动态路由功能修改完成,启动gateway测试,目前路由表中仅匹配了/acc/**的,分别测试一下/acc和/pay。

 

posted @ 2021-03-29 17:47  hzy_叶子  阅读(373)  评论(0编辑  收藏  举报