jeecgboot启动gateway不到路由的问题(跨域访问的问题)
将nacos改为本地,并且将gateway获取路由的⽅式改为从nacos获取,结果⽆法获取。
开始排查问题,⾸先看打印⽇志,发现已经连接本地nacos但是没有到路由。
2021-10-0521:51:58.483 [main] INFO org.jeecg.loader.DynamicRouteLoader:70 - 初始化路由,dataType:nacos
2021-10-0521:52:00.477 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [After]
2021-10-0521:52:00.477 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Before]
2021-10-0521:52:00.478 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Between]
2021-10-0521:52:00.478 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Cookie]
2021-10-0521:52:00.478 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Header]
2021-10-0521:52:00.478 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Host]
2021-10-0521:52:00.478 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Method]
2021-10-0521:52:00.479 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Path]
2021-10-0521:52:00.479 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Query]
2021-10-0521:52:00.479 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
2021-10-0521:52:00.479 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [RemoteAddr]
2021-10-0521:52:00.479 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [Weight]
2021-10-0521:52:00.479 [main] INFO ute.RouteDefinitionRouteLocator:139 - Loaded RoutePredicateFactory [CloudFoundryRouteService]
2021-10-0521:52:00.540 [boundedElastic-1] INFO com.alibaba.nacos.client.naming:64 - initializer namespace from System Property :null
2021-10-0521:52:00.540 [boundedElastic-1] INFO com.alibaba.nacos.client.naming:73 - initializer namespace from System Environment :null
2021-10-0521:52:00.541 [boundedElastic-1] INFO com.alibaba.nacos.client.naming:83 - initializer namespace from System Property :null
2021-10-0521:52:00.541 [main] INFO com.alibaba.fig.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg-gateway-router.json+DEFAULT_GROUP
2021-10-0521:52:00.542 [main] INFO com.alibaba.fig.impl.CacheData:93 - [fixed-192.168.0.2_8848] [add-listener] ok, tenant=, dataId=jeecg-gateway-router.json, group=DEFAULT_GR
OUP, cnt=1
2021-10-0521: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
2021-10-0521: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
2021-10-0521: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
2021-10-0521: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
2021-10-0521: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 2021-10-0521: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
2021-10-0521:52:01.522 [main] WARN fig.sources.URLConfigurationSource:121 - No URLs will be polled as dynamic configuration sources.
2021-10-0521:52:01.522 [main] INFO fig.sources.URLConfigurationSource:122 - To enable URLs as dynamic configuration sources, define System figurationSource.additionalUrls or make config.properties av 2021-10-0521:52:01.525 [main] WARN fig.sources.URLConfigurationSource:121 - No URLs will be polled as dynamic configuration sources.
2021-10-0521:52:01.526 [main] INFO fig.sources.URLConfigurationSource:122 - To enable URLs as dynamic configuration sources, define System figurationSource.additionalUrls or make config.properties av 2021-10-0521:52:02.889 [ma
in] INFO s.gateway.scg.SentinelSCGAutoConfiguration:144 - [Sentinel SpringCloudGateway] register SentinelGatewayFilter with order: -2147483648
2021-10-0521:52:03.110 [main] INFO o.s.dpoint.web.EndpointLinksResolver:58 - Exposing 21 endpoint(s) beneath base path '/actuator'
2021-10-0521:52:03.183 [main] INFO s.gateway.scg.SentinelSCGAutoConfiguration:134 - [Sentinel SpringCloudGateway] register SentinelGatewayBlockExceptionHandler
2021-10-0521:52:04.615 [main] INFO o.s.beddedty.NettyWebServer:109 - Netty started on port(s): 9999
2021-10-0521: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 2021-10-0521: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
2021-10-0521:52:06.186 [main] INFO c.a.istry.NacosServiceRegistry:71 - nacos registry, DEFAULT_GROUP jeecg-gateway 192.168.0.2:9999 register finished
2021-10-0521:52:06.401 [main] INFO org.jeecg.JeecgGatewayApplication:61 - Started JeecgGatewayApplication in12.123 seconds (JVM running for13.387)
2021-10-0521:52:06.402 [main] INFO org.jeecg.loader.DynamicRouteLoader:70 -初始化路由,dataType:nacos
2021-10-0521:52:06.479 [main] INFO com.alibaba.fig.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg-gateway-router.json+DEFAULT_GROUP
2021-10-0521:52:06.479 [main] INFO com.alibaba.fig.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-0521:52:06.482 [main] INFO com.alibaba.fig.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg.yaml+DEFAULT_GROUP
2021-10-0521:52:06.482 [main] INFO com.alibaba.fig.impl.CacheData:93 - [fixed-192.
168.0.2_8848] [add-listener] ok, tenant=, dataId=jeecg.yaml, group=DEFAULT_GROUP, cnt=1
2021-10-0521:52:06.482 [main] INFO com.alibaba.fig.impl.ClientWorker:262 - [fixed-192.168.0.2_8848] [subscribe] jeecg+DEFAULT_GROUP
2021-10-0521:52:06.482 [main] INFO com.alibaba.fig.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 (String().endsWith(dataType)) {
loadRoutesByNacos();
}
//从数据库加载路由
if (String().endsWith(dataType)) {
loadRoutesByRedis();
}
}spring boot原理流程
进⼊loadRoutesByNacos这个⽅法。
/**
* 从nacos中读取路由配置
*
* @return
*/
private void loadRoutesByNacos() {
List<RouteDefinition> routes = wArrayList();
configService = createConfigService();
if (configService == null) {
log.warn("initConfigService fail");
}
try {
String configInfo = Config(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) {
<("初始化⽹关路由时发⽣错误", e);
e.printStackTrace();
}
for (RouteDefinition definition : routes) {
log.info("update route : {}", String());
dynamicRouteService.add(definition);
}
this.publisher.publishEvent(new RefreshRoutesEvent(this));
dynamicRouteByNacosListener(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP);
}
⾛到标红的地⽅,再往下⾛就是空了,所以问题基本定位到这⼀⾏,开始检查三个参数。
第⼀个DATA_ID,就发现了这⾏代码,通过配置获取data-id之后,他这⾥⼜进⾏了⼀次处理,拼接了“.json”,坑⼈。
@Value("${fig.data-id:#{null}}")
public void setRouteDataId(String dataId) {
DATA_ID = dataId + ".json";
}
敲⿊板了我是这样配的,以前没⽤过nacos,以为这个dataId是data ID+配置格式(也就是后缀),结果啪啪打脸了,想的太多了,后台的是jeecg-gateway-
router.json,咱们配的没有.json这个后缀,所以肯定不到咯
于是使⽤克隆,克隆时data ID加上.json后缀就可以了,完美解决问题
以下总结了这次排查问题的步骤流程图。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论