Spring Cloud Alibaba實戰(七) - Gateway搭配Nacos實現動態路由

大雨將至發表於2019-06-03

目錄

(一)Nacos動態配置
(二)Nacos註冊中心
(三)Sentinel之限流
(四)Sentinel之熔斷
(五)Gateway之路由、限流
(六)Gateway之鑑權、日誌
(七)Gateway搭配Nacos實現動態路由
(八)Dubbo + Nacos

正文

在 Spring Cloud Alibaba實戰(五) - Gateway之路由、限流​ 中,路由資訊定義在配置檔案中,這種方式有一個缺點就是修改路由資訊必須重啟服務才能生效。閘道器作為全部流量的入口,可用時間當然越長越好,不重啟服務而修改路由是一個更好的選擇,結合Nacos可以做到這一點。

首先,參考前面章節啟動Nacos、account-service和payment-service。並以gateway專案為基礎增加本節功能。

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。 

 

/acc/user請求成功轉發到account-service,而/pay/balance沒有找到匹配的路由資訊,與期望行為一致。

下面來動態增加/pay的路由,修改Nacos中的gateway-router配置如下:

[{
    "id": "account-router",
    "order": 0,
    "predicates": [{
        "args": {
            "pattern": "/acc/**"
        },
        "name": "Path"
    }],
    "uri": "lb://account-service"
},{
    "id": "payment-router",
    "order": 2,
    "predicates": [{
        "args": {
            "pattern": "/pay/**"
        },
        "name": "Path"
    }],
    "uri": "lb://payment-service"
}]

不重啟gateway再次測試/pay/balance請求 

 

可以看到新增加的路由配置已生效。

本期程式碼 

連結:https://pan.baidu.com/s/1ANe7slFiUw0nm1HBXdzzFg
提取碼:2was 

 

 

相關文章