【发布时间】:2021-08-08 10:43:25
【问题描述】:
我已经实现了一个基于 Spring Cloud 的应用程序,它具有以下主要架构。 GatewayService 代表HelloService 的多个实例。我使用 Consul 进行服务发现。 GatewayService的实现方式如下:
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
@RestController
static class GateWayController {
private final WebClient.Builder webClientBuilder;
@Data
@NoArgsConstructor
private static class Response {
private String message;
private String appName;
private int port;
}
@Autowired
public GateWayController(WebClient.Builder webClientBuilder) {
this.webClientBuilder = webClientBuilder;
}
@GetMapping("/lbhello")
public Mono<Response> hello() {
return webClientBuilder.build()
.get()
.uri("lb://hello-service/hello")
.retrieve()
.bodyToMono(Response.class);
}
}
}
spring-cloud-starter-consul-discovery 和 spring-cloud-starter-loadbalancer 作为依赖项包含在 pom.xml 中。在application.yaml 中我只设置了application.name 并禁用Ribbon,以便使用Spring Cloud Loadbalancer。当我启动GatewayService 和HelloService 的两个实例时,一切都按预期工作。对lbhello 端点的服务调用交替委托给两个HelloService 实例。
当HelloService 的一个实例停止时,Consul 检测到该实例缺少将其标记为关键。因此,我希望负载均衡器至少在一段时间后停止将请求转发到不存在的服务。但这永远不会发生。每隔一个服务调用就会委托给这个服务实例,因此会失败。当使用 Eureka 作为发现服务时,注册表会被调整,并且只有剩余的 HelloService 会被考虑用于服务调用。
这是 Consul 的预期行为吗?如果答案是肯定的,是否有 Spring Cloud 设置来适应这种行为?
【问题讨论】:
-
@spencergibb - 哪个是首选?计划的健康检查与查询通过。谢谢
标签: java spring-boot spring-cloud spring-cloud-consul