我们正在将Sping Boot 从2迁移到3,并摆脱Netflix Ribbon。我们在尝试使用Consul查找服务时遇到问题。如果我们回滚到Sping Boot 2 + Netflix Ribbon,它的工作没有问题,所以我们放弃了任何连接问题。
日志错误:
RoundRobinLoadBalancer|No servers available for service: cachedavailability-integrations-service
ReactorLoadBalancerExchangeFilterFunction|LoadBalancer does not contain an instance for the service cachedavailability-integrations-service
Communication error with uri: http://cachedavailability-integrations-service/testing org.springframework.web.reactive.function.client.WebClientResponseException$ServiceUnavailable: 503 Service Unavailable from UNKNOWN
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:336)
Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
Error has been observed at the following site(s):
*__checkpoint ⇢ 503 SERVICE_UNAVAILABLE from GET http://cachedavailability-integrations-service/testing [DefaultWebClient]
领事界面x1c 0d1x
1.服务“si-manager”是尝试发现和访问“cachedavailability”的服务。
1.它注册得很好,但是在使用WebClientbean时没有发现其他注册的服务。
我们已经尝试了很多方法,比如:
- https://docs.spring.io/spring-cloud-consul/docs/current/reference/html/#using-the-discoveryclient
- https://www.appsloveworld.com/springboot/100/7/service-discovery-with-spring-webflux-webclient
- Configuring spring-cloud loadbalancer without autoconfiguration
样本
主类
@org.springframework.cloud.client.discovery.EnableDiscoveryClient
public class MainApplication {...}
WebClient配置
@Bean(name = "webClientConsulAvailability")
public WebClient webClientConsulAvailability(
WebClient.Builder webClientBuilder,
ReactorLoadBalancerExchangeFilterFunction lbFunction,
ExchangeFilterFunction logFilter
) {
return webClientBuilder
.filter(lbFunction)
.filter(logFilter)
.build();
}
bootstrap.yml**
spring:
application:
name: si-manager-service
profiles:
active: ${SPRING_PROFILES_ACTIVE:local}
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
serviceName: ${spring.application.name}
instanceId: ${spring.application.name}8500
enabled: true
# Register as a service in consul.
register: true
registerHealthCheck: true
依赖关系
**领事版本:**v1.15.3
用法示例:
webClientConsulAvailability.get()
.uri("http://cachedavailability-integrations-service/testing")
.retrieve()
.bodyToFlux(MyDTO.class)
.doOnError(e -> {
if (isErrorLogLevel(e)) {
log.error(COMMUNICATION_ERROR_WITH_URI + uri, e);
} else {
log.warn(COMMUNICATION_ERROR_WITH_URI + uri, e);
}
})
.onErrorResume(e -> Flux.empty());
1条答案
按热度按时间yi0zb3m41#
用下面的代码修复。