jeecgboot啟動gateway找不到路由的問題(跨域訪問的問題)


將nacos改為本地,並且將gateway獲取路由的方式改為從nacos獲取,結果無法獲取。

開始排查問題,首先看打印日志,發現已經連接本地nacos但是沒有找到路由。

2021-10-05 21:51:58.483 [main] INFO  org.jeecg.loader.DynamicRouteLoader:70 - 初始化路由,dataType:nacos
2021-10-05 21:52:00.477 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [After]
2021-10-05 21:52:00.477 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Before]
2021-10-05 21:52:00.478 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Between]
2021-10-05 21:52:00.478 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Cookie]
2021-10-05 21:52:00.478 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Header]
2021-10-05 21:52:00.478 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Host]
2021-10-05 21:52:00.478 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Method]
2021-10-05 21:52:00.479 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Path]
2021-10-05 21:52:00.479 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Query]
2021-10-05 21:52:00.479 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
2021-10-05 21:52:00.479 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [RemoteAddr]
2021-10-05 21:52:00.479 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Weight]
2021-10-05 21:52:00.479 [main] INFO  o.s.c.gateway.route.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [CloudFoundryRouteService]
2021-10-05 21:52:00.540 [boundedElastic-1] INFO  com.alibaba.nacos.client.naming:64 - initializer namespace from System Property :null
2021-10-05 21:52:00.540 [boundedElastic-1] INFO  com.alibaba.nacos.client.naming:73 - initializer namespace from System Environment :null
2021-10-05 21:52:00.541 [boundedElastic-1] INFO  com.alibaba.nacos.client.naming:83 - initializer namespace from System Property :null
2021-10-05 21:52:00.541 [main] INFO  com.alibaba.nacos.client.config.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg-gateway-router.json+DEFAULT_GROUP
2021-10-05 21:52:00.542 [main] INFO  com.alibaba.nacos.client.config.impl.CacheData:93 - [fixed-192.168.0.2_8848] [add-listener] ok, tenant=, dataId=jeecg-gateway-router.json, group=DEFAULT_GROUP, cnt=1
2021-10-05 21:52:00.621 [boundedElastic-3] INFO  com.alibaba.nacos.client.naming:191 - new ips(1) service: DEFAULT_GROUP@@jeecg-gateway -> [{"instanceId":"192.168.0.2#9999#DEFAULT#DEFAULT_GROUP@@jeecg-gateway","ip":"192.168.0.2","port":9999,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@jeecg-gateway","metadata":{"preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2021-10-05 21:52:00.621 [boundedElastic-4] INFO  com.alibaba.nacos.client.naming:191 - new ips(1) service: DEFAULT_GROUP@@jeecg-system -> [{"instanceId":"192.168.0.2#7001#DEFAULT#DEFAULT_GROUP@@jeecg-system","ip":"192.168.0.2","port":7001,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@jeecg-system","metadata":{"preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2021-10-05 21:52:00.621 [boundedElastic-2] INFO  com.alibaba.nacos.client.naming:191 - new ips(1) service: DEFAULT_GROUP@@jeecg-activiti -> [{"instanceId":"192.168.0.2#8099#DEFAULT#DEFAULT_GROUP@@jeecg-activiti","ip":"192.168.0.2","port":8099,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@jeecg-activiti","metadata":{"preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2021-10-05 21:52:00.627 [boundedElastic-2] INFO  com.alibaba.nacos.client.naming:228 - current ips:(1) service: DEFAULT_GROUP@@jeecg-activiti -> [{"instanceId":"192.168.0.2#8099#DEFAULT#DEFAULT_GROUP@@jeecg-activiti","ip":"192.168.0.2","port":8099,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@jeecg-activiti","metadata":{"preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2021-10-05 21:52:00.627 [boundedElastic-3] INFO  com.alibaba.nacos.client.naming:228 - current ips:(1) service: DEFAULT_GROUP@@jeecg-gateway -> [{"instanceId":"192.168.0.2#9999#DEFAULT#DEFAULT_GROUP@@jeecg-gateway","ip":"192.168.0.2","port":9999,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@jeecg-gateway","metadata":{"preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2021-10-05 21:52:00.627 [boundedElastic-4] INFO  com.alibaba.nacos.client.naming:228 - current ips:(1) service: DEFAULT_GROUP@@jeecg-system -> [{"instanceId":"192.168.0.2#7001#DEFAULT#DEFAULT_GROUP@@jeecg-system","ip":"192.168.0.2","port":7001,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@jeecg-system","metadata":{"preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2021-10-05 21:52:01.522 [main] WARN  com.netflix.config.sources.URLConfigurationSource:121 - No URLs will be polled as dynamic configuration sources.
2021-10-05 21:52:01.522 [main] INFO  com.netflix.config.sources.URLConfigurationSource:122 - To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2021-10-05 21:52:01.525 [main] WARN  com.netflix.config.sources.URLConfigurationSource:121 - No URLs will be polled as dynamic configuration sources.
2021-10-05 21:52:01.526 [main] INFO  com.netflix.config.sources.URLConfigurationSource:122 - To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2021-10-05 21:52:02.889 [main] INFO  c.a.c.s.gateway.scg.SentinelSCGAutoConfiguration:144 - [Sentinel SpringCloudGateway] register SentinelGatewayFilter with order: -2147483648
2021-10-05 21:52:03.110 [main] INFO  o.s.b.actuate.endpoint.web.EndpointLinksResolver:58 - Exposing 21 endpoint(s) beneath base path '/actuator'
2021-10-05 21:52:03.183 [main] INFO  c.a.c.s.gateway.scg.SentinelSCGAutoConfiguration:134 - [Sentinel SpringCloudGateway] register SentinelGatewayBlockExceptionHandler
2021-10-05 21:52:04.615 [main] INFO  o.s.boot.web.embedded.netty.NettyWebServer:109 - Netty started on port(s): 9999
2021-10-05 21:52:06.182 [main] INFO  com.alibaba.nacos.client.naming:81 - [BEAT] adding beat: BeatInfo{port=9999, ip='192.168.0.2', weight=1.0, serviceName='DEFAULT_GROUP@@jeecg-gateway', cluster='DEFAULT', metadata={preserved.register.source=SPRING_CLOUD}, scheduled=false, period=5000, stopped=false} to beat map.
2021-10-05 21:52:06.183 [main] INFO  com.alibaba.nacos.client.naming:223 - [REGISTER-SERVICE] public registering service DEFAULT_GROUP@@jeecg-gateway with instance: Instance{instanceId='null', ip='192.168.0.2', port=9999, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName='DEFAULT', serviceName='null', metadata={preserved.register.source=SPRING_CLOUD}}
2021-10-05 21:52:06.186 [main] INFO  c.a.cloud.nacos.registry.NacosServiceRegistry:71 - nacos registry, DEFAULT_GROUP jeecg-gateway 192.168.0.2:9999 register finished
2021-10-05 21:52:06.401 [main] INFO  org.jeecg.JeecgGatewayApplication:61 - Started JeecgGatewayApplication in 12.123 seconds (JVM running for 13.387)
2021-10-05 21:52:06.402 [main] INFO  org.jeecg.loader.DynamicRouteLoader:70 - 初始化路由,dataType:nacos 2021-10-05 21:52:06.479 [main] INFO  com.alibaba.nacos.client.config.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg-gateway-router.json+DEFAULT_GROUP
2021-10-05 21:52:06.479 [main] INFO  com.alibaba.nacos.client.config.impl.CacheData:93 - [fixed-192.168.0.2_8848] [add-listener] ok, tenant=, dataId=jeecg-gateway-router.json, group=DEFAULT_GROUP, cnt=1
2021-10-05 21:52:06.482 [main] INFO  com.alibaba.nacos.client.config.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg.yaml+DEFAULT_GROUP
2021-10-05 21:52:06.482 [main] INFO  com.alibaba.nacos.client.config.impl.CacheData:93 - [fixed-192.168.0.2_8848] [add-listener] ok, tenant=, dataId=jeecg.yaml, group=DEFAULT_GROUP, cnt=1
2021-10-05 21:52:06.482 [main] INFO  com.alibaba.nacos.client.config.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg+DEFAULT_GROUP
2021-10-05 21:52:06.482 [main] INFO  com.alibaba.nacos.client.config.impl.CacheData:93 - [fixed-192.168.0.2_8848] [add-listener] ok, tenant=, dataId=jeecg, group=DEFAULT_GROUP, cnt=1

日志已經打印,初始化路由,dataType:nacos,於是從這里繼續排查。

找到代碼中的這個日志輸出位置。

@PostConstruct
    public void init() {
        String dataType = GatewayRoutersConfiguration.DATA_TYPE;
        log.info("初始化路由,dataType:"+ dataType);
        if (RouterDataType.nacos.toString().endsWith(dataType)) {
            loadRoutesByNacos();
        }
        //從數據庫加載路由
        if (RouterDataType.database.toString().endsWith(dataType)) {
            loadRoutesByRedis();
        }
    }

進入loadRoutesByNacos這個方法。

/**
     * 從nacos中讀取路由配置
     *
     * @return
     */
    private void loadRoutesByNacos() {
        List<RouteDefinition> routes = Lists.newArrayList();
        configService = createConfigService();
        if (configService == null) {
            log.warn("initConfigService fail");
        }
        try {
            String configInfo = configService.getConfig(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP, GatewayRoutersConfiguration.DEFAULT_TIMEOUT); if (StringUtils.isNotBlank(configInfo)) {
                log.info("獲取網關當前配置:\r\n{}", configInfo);
                routes = JSON.parseArray(configInfo, RouteDefinition.class);
            }
        } catch (NacosException e) {
            log.error("初始化網關路由時發生錯誤", e);
            e.printStackTrace();
        }
        for (RouteDefinition definition : routes) {
            log.info("update route : {}", definition.toString());
            dynamicRouteService.add(definition);
        }
        this.publisher.publishEvent(new RefreshRoutesEvent(this));
        dynamicRouteByNacosListener(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP);
    }

走到標紅的地方,再往下走就是空了,所以問題基本定位到這一行,開始檢查三個參數。

第一個DATA_ID,就發現了這行代碼,通過配置獲取data-id之后,他這里又進行了一次處理,拼接了“.json”,坑人!!!。

@Value("${jeecg.route.config.data-id:#{null}}")
    public void setRouteDataId(String dataId) {
        DATA_ID = dataId + ".json";
    }

敲黑板了!!!我是這樣配的,以前沒用過nacos,以為這個dataId是data ID+配置格式(也就是后綴),結果啪啪打臉了,想的太多了,后台找的是jeecg-gateway-router.json,咱們配的沒有.json這個后綴,所以肯定找不到咯!!!

 

 於是使用克隆,克隆時data ID加上.json后綴就可以了,完美解決問題!!!

 

 以下總結了這次排查問題的步驟流程圖。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM