hengyuss opened a new pull request, #6395:
URL: https://github.com/apache/shenyu/pull/6395

   <!-- Describe your PR here; e.g. Fixes #issueNo -->
   
   <!--
   Thank you for proposing a pull request. This template will guide you through 
the essential steps necessary for a pull request.
   -->
   Fix: Local SpringMvc Client Fails to Register
   ## Symptom
   When using local discovery mode (shenyu.discovery.type=local), the SpringMvc 
client fails to complete URI registration.
   ## Root Cause
   AbstractContextRefreshedEventListener originally declared 
isDiscoveryLocalMode as a final member field, initialized once in the 
constructor via 
props.getProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY). That 
discoveryLocalMode property, however, is written into props by 
ShenyuSpringMvcClientConfiguration before it constructs the 
SpringMvcClientEventListener bean.
   
   Due to a timing race between bean instantiation and the 
ContextRefreshedEvent 
firing—AbstractContextRefreshedEventListener#onApplicationEvent may execute 
before ShenyuSpringMvcClientConfiguration has finished populating props—the 
discoveryLocalMode property read at construction time is empty, so 
isDiscoveryLocalMode is always false. As a result, the local-registration 
branch inside onApplicationEvent is skipped, and the local SpringMvc client 
never initiates URI registration.
   Solution
   1. Remove the immutable field; read at event time instead: Deleted the 
isDiscoveryLocalMode field and its constructor initialization in 
AbstractContextRefreshedEventListener. Instead, onApplicationEvent now reads 
shenyu.discovery.type directly from the Spring Environment (defaulting to 
local) and evaluates the flag on the fly. This fully decouples discovery-mode 
evaluation from bean-construction / props-population timing, guaranteeing the 
correct environment config is consulted no matter when the listener callback 
fires.
   2. Keep ShenyuSpringMvcClientConfiguration logic intact: The second commit 
restores the code that writes discoveryLocalMode into props in the 
configuration class, keeping the fix minimal—only the core listener changes, 
leaving other flows that may depend on this prop unaffected.
   Test & Checkstyle Fixes
   - SpringMvcClientEventListenerTest, TarsServiceBeanPostProcessorTest: Added 
mocks for applicationContext.getEnvironment() and the shenyu.discovery.type 
property to align with the new runtime-read logic.
   - Fixed indentation of the buildApiSuperPath assertion continuation lines so 
Checkstyle passes.
   Scope
   - Core change: AbstractContextRefreshedEventListener.java
   - Test changes: SpringMvcClientEventListenerTest.java, 
TarsServiceBeanPostProcessorTest.java
   - Compatibility: Only the read timing changes; the judgment logic and 
default value (local) remain unchanged, with no impact on non-local discovery 
modes.
   
   
   
   Make sure that:
   
   - [x] You have read the [contribution 
guidelines](https://shenyu.apache.org/community/contributor-guide).
   - [x] You submit test cases (unit or integration tests) that back your 
changes.
   - [x] Your local test passed `./mvnw clean install 
-Dmaven.javadoc.skip=true`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to