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后缀就可以了,完美解决问题!!!

 

 以下总结了这次排查问题的步骤流程图。

 

posted @ 2021-10-05 22:04  背着泰山找黄河  阅读(2561)  评论(0编辑  收藏  举报