Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Failed to resolve 'xxx' [A(1)] after 6 queries" occur with WebClient but RestTemplate call success with Nacos Discovery 2022.0.0.0 #3918

Closed
tanjh opened this issue Dec 5, 2024 · 3 comments
Labels
area/nacos spring cloud alibaba nacos kind/question

Comments

@tanjh
Copy link

tanjh commented Dec 5, 2024

When I call interface with WebClient and RestTemplate, and the type of RestTemplate is successful but the other occur RuntimeException: Failed to fetch user: Failed to resolve 'zy-cloud-user' [A(1)] after 6 queries.

Which Component
[Nacos Discovery] com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery -> 2022.0.0.0

Describe what problem you have encountered
There is a Spring WebFlux project with 4 modules: zy-cloud-common, zy-cloud-gateway, zy-cloud-auth, zy-cloud-user.
I use nacos-discovery and nacos-config to manage configuration and service. and the nacos server is 2.4.3, the project integrated with
com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery -> 2022.0.0.0 and com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config -> 2022.0.0.0;
When I call one endpoint in zy-cloud-user with WebClient, it failed and throws exception as following:
java.lang.RuntimeException: Failed to fetch user: Failed to resolve 'zy-cloud-user' [A(1)] after 6 queries
at com.zy.cloud.auth.rpc.WebClientRemoteCall.lambda$findByUsername$0(WebClientRemoteCall.java:36) ~[classes!/:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
*__checkpoint ⇢ Handler com.zy.cloud.auth.controller.AuthController#login(Map) [DispatcherHandler]
Original Stack Trace:
at com.zy.cloud.auth.rpc.WebClientRemoteCall.lambda$findByUsername$0(WebClientRemoteCall.java:36) ~[classes!/:na]
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onError(MonoFlatMap.java:180) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onError(FluxContextWrite.java:121) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.FluxDoFinally$DoFinallySubscriber.onError(FluxDoFinally.java:119) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onError(MonoPeekTerminal.java:258) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.FluxPeekFuseable$PeekConditionalSubscriber.onError(FluxPeekFuseable.java:903) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onError(Operators.java:2210) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222) ~[reactor-core-3.5.5.jar!/:3.5.5]
at reactor.core.publisher.FluxMap$MapSubscriber.onError(FluxMap.java:134) ~[reactor-core-3.5.5.jar!/:3.5.5]

src/main/resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.zy.cloud.common.WebClientConfig

----------------------WebClientConfig.java --------------------------
package com.zy.cloud.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;

@slf4j
@configuration
public class WebClientConfig {

static {
    log.info("WebClientConfig is being loaded.");
}

@LoadBalanced
@Bean
public WebClient.Builder loadBalancedWebClientBuilder() {
    log.info("Creating load-balanced WebClient builder.");
    return WebClient.builder();
}

}

----------------------WebClientRemoteCall.java -------------------------

package com.zy.cloud.auth.rpc;

import com.zy.cloud.common.model.UserDTO;
import reactor.core.publisher.Mono;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;

@service
public class WebClientRemoteCall implements BaseRemoteCall {

private final WebClient.Builder webClientBuilder;

@Autowired
public WebClientRemoteCall(WebClient.Builder webClientBuilder) {
    this.webClientBuilder = webClientBuilder;
}

/**
 * 调用用户服务获取用户信息
 */
public Mono<UserDTO> findByUsername(String username) {
    return webClientBuilder
            .baseUrl("http://zy-cloud-user")
            .build()
            .get()
            .uri("/users/username/{username}", username)
            .retrieve()
            .bodyToMono(UserDTO.class)
            .onErrorResume(e -> Mono.error(new RuntimeException("Failed to fetch user: " + e.getMessage())));
}

}

---------------------------zy-cloud-auth launch log-----------------------------
2024-12-05T13:20:32.532+08:00 DEBUG 15548 --- [ main] c.a.c.n.c.NacosConfigDataLoader : [Nacos Config] config[dataId=zy-cloud-auth.yml, group=ZY-AIANALYSIS] content:
server:
port: 9200
servlet:
context-path: /

spring:
cloud:
loadbalancer:
ribbon:
enabled: false

Describe what information you have read
zy-cloud-user and zy-cloud-auth are running in the same machine with different ports.
when the trouble occur, I change the service name into IP:port, it is successful.
return webClientBuilder
// .baseUrl("http://zy-cloud-user")
.baseUrl("http://192.168.3.109:9100")
.build()
.get()
.uri("/users/username/{username}", username)
.retrieve()
.bodyToMono(UserDTO.class)
.onErrorResume(e -> Mono.error(new RuntimeException("Failed to fetch user: " + e.getMessage())));

Then I use RestTemplate instead of WebClient, it is successful.
public Mono findByUsername(String username) {
return Mono.fromCallable(() ->
restTemplate.getForObject("http://zy-cloud-user/users/username/" + username, UserDTO.class)
).subscribeOn(Schedulers.boundedElastic());
}

@tanjh
Copy link
Author

tanjh commented Dec 6, 2024

from the debug information, I found the project use netty DNS resolver to resolve the service name. Then I add the DeferringLoadBalancerExchangeFilterFunction in the webclient, it is successful to find the service with service name.
But after resolved the service name, the following call will register new nacos client with the resolved service IP. Is there any question while cache the resolved result?
======================= 1st call =================================
2024-12-06T13:22:57.935+08:00 DEBUG 2416 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [965b3edc-1] HTTP POST "/auth/login"
2024-12-06T13:22:57.964+08:00 DEBUG 2416 --- [ctor-http-nio-2] s.w.r.r.m.a.RequestMappingHandlerMapping : [965b3edc-1] Mapped to com.zy.cloud.auth.controller.AuthController#login(Map)
2024-12-06T13:22:57.978+08:00 DEBUG 2416 --- [ctor-http-nio-2] .r.m.a.RequestBodyMethodArgumentResolver : [965b3edc-1] Content-Type:application/json
2024-12-06T13:22:57.981+08:00 DEBUG 2416 --- [ctor-http-nio-2] .r.m.a.RequestBodyMethodArgumentResolver : [965b3edc-1] 0..1 [java.util.Map<java.lang.String, java.lang.Object>]
2024-12-06T13:22:58.090+08:00 DEBUG 2416 --- [ctor-http-nio-2] o.s.w.r.r.m.a.ResponseBodyResultHandler : [965b3edc-1] Using 'application/json' given [/] and supported [application/json, application/*+json, application/x-ndjson, text/event-stream]
2024-12-06T13:22:58.091+08:00 DEBUG 2416 --- [ctor-http-nio-2] o.s.w.r.r.m.a.ResponseBodyResultHandler : [965b3edc-1] 0..1 [com.zy.cloud.common.model.ApiResponse<?>]
2024-12-06T13:22:58.225+08:00 INFO 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : [SUBSCRIBE-SERVICE] service:zy-cloud-user, group:ZY-AIANALYSIS, clusters:
2024-12-06T13:22:58.225+08:00 DEBUG 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : [GRPC-SUBSCRIBE] service:zy-cloud-user, group:ZY-AIANALYSIS, cluster:
2024-12-06T13:22:58.229+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] OUTBOUND HEADERS: streamId=17 headers=GrpcHttp2OutboundHeaders[:authority: 192.168.1.222:9848, :path: /Request/request, :method: POST, :scheme: http, content-type: application/grpc, te: trailers, user-agent: grpc-java-netty/1.50.2, grpc-accept-encoding: gzip] streamDependency=0 weight=16 exclusive=false padding=0 endStream=false
2024-12-06T13:22:58.229+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] OUTBOUND DATA: streamId=17 padding=0 endStream=true length=237 bytes=00000000e812341a1753756273637269626553657276696365526571756573743a0e0a036170701207756e6b6e6f776e420931302e382e302e31341aaf0112ac...
2024-12-06T13:22:58.235+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND HEADERS: streamId=17 headers=GrpcHttp2ResponseHeaders[:status: 200, content-type: application/grpc, grpc-encoding: identity, grpc-accept-encoding: gzip] padding=0 endStream=false
2024-12-06T13:22:58.237+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND DATA: streamId=17 padding=0 endStream=false length=713 bytes=00000002c4121a1a1853756273637269626553657276696365526573706f6e73651aa50512a2057b22726573756c74436f6465223a3230302c226572726f7243...
2024-12-06T13:22:58.237+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] OUTBOUND PING: ack=false bytes=1234
2024-12-06T13:22:58.237+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND HEADERS: streamId=17 headers=GrpcHttp2ResponseHeaders[grpc-status: 0] padding=0 endStream=true
2024-12-06T13:22:58.241+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND PING: ack=true bytes=1234
2024-12-06T13:22:58.241+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : Window: 1,048,576
2024-12-06T13:22:58.249+08:00 INFO 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : init new ips(1) service: ZY-AIANALYSIS@@zy-cloud-user -> [{"instanceId":"10.8.0.14#9100#DEFAULT#ZY-AIANALYSIS@@zy-cloud-user","ip":"10.8.0.14","port":9100,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"ZY-AIANALYSIS@@zy-cloud-user","metadata":{"preserved.register.source":"SPRING_CLOUD","ephemeral":"false"},"ipDeleteTimeout":30000,"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000}]
2024-12-06T13:22:58.254+08:00 INFO 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : current ips:(1) service: ZY-AIANALYSIS@@zy-cloud-user -> [{"instanceId":"10.8.0.14#9100#DEFAULT#ZY-AIANALYSIS@@zy-cloud-user","ip":"10.8.0.14","port":9100,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"ZY-AIANALYSIS@@zy-cloud-user","metadata":{"preserved.register.source":"SPRING_CLOUD","ephemeral":"false"},"ipDeleteTimeout":30000,"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000}]
2024-12-06T13:22:58.254+08:00 DEBUG 2416 --- [ncesChangeEvent] c.a.nacos.common.notify.NotifyCenter : [NotifyCenter] the com.alibaba.nacos.client.naming.event.InstancesChangeEvent@4eaa4c19 will received by com.alibaba.nacos.client.naming.event.InstancesChangeNotifier@d400943
2024-12-06T13:22:58.313+08:00 DEBUG 2416 --- [oundedElastic-1] o.s.w.r.f.client.ExchangeFunctions : [7fcd3540] HTTP GET http://10.8.0.14:9100/users/username/babc

==================== the 2nd call ========================================
2024-12-06T13:23:01.655+08:00 DEBUG 2416 --- [ctor-http-nio-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [965b3edc-2] HTTP POST "/auth/login"
2024-12-06T13:23:01.655+08:00 DEBUG 2416 --- [ctor-http-nio-2] s.w.r.r.m.a.RequestMappingHandlerMapping : [965b3edc-2] Mapped to com.zy.cloud.auth.controller.AuthController#login(Map)
2024-12-06T13:23:01.656+08:00 DEBUG 2416 --- [ctor-http-nio-2] .r.m.a.RequestBodyMethodArgumentResolver : [965b3edc-2] Content-Type:application/json
2024-12-06T13:23:01.656+08:00 DEBUG 2416 --- [ctor-http-nio-2] .r.m.a.RequestBodyMethodArgumentResolver : [965b3edc-2] 0..1 [java.util.Map<java.lang.String, java.lang.Object>]
2024-12-06T13:23:01.658+08:00 DEBUG 2416 --- [ctor-http-nio-2] o.s.w.r.r.m.a.ResponseBodyResultHandler : [965b3edc-2] Using 'application/json' given [/] and supported [application/json, application/*+json, application/x-ndjson, text/event-stream]
2024-12-06T13:23:01.658+08:00 DEBUG 2416 --- [ctor-http-nio-2] o.s.w.r.r.m.a.ResponseBodyResultHandler : [965b3edc-2] 0..1 [com.zy.cloud.common.model.ApiResponse<?>]
2024-12-06T13:23:01.687+08:00 TRACE 2416 --- [ctor-http-nio-2] PreviousInstanceAndRetryEnabledCondition : Condition LoadBalancerClientConfiguration.ReactiveOnAvoidPreviousInstanceAndRetryEnabledCondition on org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration$ReactiveRetryConfiguration matched due to AllNestedConditions 2 matched 0 did not; NestedCondition on LoadBalancerClientConfiguration.ReactiveOnAvoidPreviousInstanceAndRetryEnabledCondition.AvoidPreviousInstanceEnabled; NestedCondition on LoadBalancerClientConfiguration.ReactiveOnAvoidPreviousInstanceAndRetryEnabledCondition.LoadBalancerRetryEnabled @ConditionalOnProperty (spring.cloud.loadbalancer.retry.enabled=true) matched
2024-12-06T13:23:01.701+08:00 DEBUG 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : failover-mode: false
2024-12-06T13:23:01.702+08:00 INFO 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : [SUBSCRIBE-SERVICE] service:10.8.0.14, group:ZY-AIANALYSIS, clusters:
2024-12-06T13:23:01.702+08:00 DEBUG 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : [GRPC-SUBSCRIBE] service:10.8.0.14, group:ZY-AIANALYSIS, cluster:
2024-12-06T13:23:01.703+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] OUTBOUND HEADERS: streamId=19 headers=GrpcHttp2OutboundHeaders[:authority: 192.168.1.222:9848, :path: /Request/request, :method: POST, :scheme: http, content-type: application/grpc, te: trailers, user-agent: grpc-java-netty/1.50.2, grpc-accept-encoding: gzip] streamDependency=0 weight=16 exclusive=false padding=0 endStream=false
2024-12-06T13:23:01.704+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] OUTBOUND DATA: streamId=19 padding=0 endStream=true length=233 bytes=00000000e412341a1753756273637269626553657276696365526571756573743a0e0a036170701207756e6b6e6f776e420931302e382e302e31341aab0112a8...
2024-12-06T13:23:01.709+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND HEADERS: streamId=19 headers=GrpcHttp2ResponseHeaders[:status: 200, content-type: application/grpc, grpc-encoding: identity, grpc-accept-encoding: gzip] padding=0 endStream=false
2024-12-06T13:23:01.710+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND DATA: streamId=19 padding=0 endStream=false length=317 bytes=0000000138121a1a1853756273637269626553657276696365526573706f6e73651a99021296027b22726573756c74436f6465223a3230302c226572726f7243...
2024-12-06T13:23:01.711+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] OUTBOUND PING: ack=false bytes=1234
2024-12-06T13:23:01.711+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND HEADERS: streamId=19 headers=GrpcHttp2ResponseHeaders[grpc-status: 0] padding=0 endStream=true
2024-12-06T13:23:01.715+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : [id: 0x2f1788c1, L:/192.168.3.109:64580 - R:/192.168.1.222:9848] INBOUND PING: ack=true bytes=1234
2024-12-06T13:23:01.716+08:00 DEBUG 2416 --- [-worker-ELG-1-4] c.a.n.s.i.g.n.s.i.g.n.NettyClientHandler : Window: 1,048,576
2024-12-06T13:23:01.719+08:00 INFO 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : init new ips(0) service: ZY-AIANALYSIS@@10.8.0.14 -> []
2024-12-06T13:23:01.719+08:00 INFO 2416 --- [oundedElastic-1] com.alibaba.nacos.client.naming : current ips:(0) service: ZY-AIANALYSIS@@10.8.0.14 -> []

@yuluo-yx yuluo-yx added kind/question area/nacos spring cloud alibaba nacos labels Dec 7, 2024
@yuluo-yx
Copy link
Collaborator

yuluo-yx commented Dec 7, 2024

nacos 2.4.3 sca 目前应该没有更新到,可以用最新的版本试试看

@tanjh
Copy link
Author

tanjh commented Dec 9, 2024

it is resolved.
I think it is more better with webflux support sample in the nacos document.

@tanjh tanjh closed this as completed Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/nacos spring cloud alibaba nacos kind/question
Projects
None yet
Development

No branches or pull requests

2 participants