spring-cloud / spring-cloud/spring-cloud-gateway

Multiple loadings of route configurations when use Gateway together with Nacos-Discovery

Open
#3,794 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Describe the bug
spring-boot:2.6.4
spring-cloud-starter-gateway:3.1.4
spring-cloud-starter-alibaba-nacos-discovery:2021.0.1.0
1.
When I use Gateway and Nacos as the service registry center, I set the log level to DEBUG. I noticed that the routing information is printed twice during service startup. After analyzing the source code, I found that the first loading occurs during the initialization of the web container, where the NacosAutoServiceRegistration class publishes the InstanceRegisteredEvent. The second loading happens after the service has fully started, when the ContextRefreshedEvent is published, which also triggers the loading of the routing definitions.

Image

Another issue is that when used together with Nacos-Discovery, the NacosWatch in Nacos-Discovery will start a scheduled task that executes every 30 milliseconds by default. This task publishes a HeartbeatEvent.

NacosWatch:
this.watchFuture = this.taskScheduler.scheduleWithFixedDelay( this::nacosServicesWatch, this.properties.getWatchDelay());

The key point is that the data carried by the event is a long-type value that increases each time. This leads to this.monitor.update(value) is always true. As a result, the reset method is executed periodically.

Image

I noticed that NacosWatch can be disabled using the spring.cloud.nacos.discovery.watch.enabled property, but I'm not sure whether it will affect the update of registered services.


Overall, these phenomena currently do not seem to have any impact on the functionality of the program. Indeed, there might be room for optimization here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the startup events through NacosAutoServiceRegistration and ContextRefreshedEvent, then inspect NacosWatch and its scheduled nacosServicesWatch callback. Reproduce the duplicate route loading and repeated reset behavior with the listed Spring Boot, Gateway, and Nacos versions. Done should define whether route loading or resets can be avoided without affecting registered-service updates, with regression coverage for the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.