我使用 Spring Cloud Feign 客户端实现了请求。我试过这个:
假装客户端:
@FeignClient(name = "mail-service")
public interface EmailClient {
@RequestMapping(method = RequestMethod.POST, value = "/register")
void setUserRegistration(RegisterUserDTO registerUserDTO);
@RequestMapping(method = RequestMethod.POST, value = "/password_reset")
void setUserPasswordReset(PasswordResetDTO passwordResetDTO);
}
假装配置:
@Configuration
public class LoadBalancerConfiguration {
@Bean
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return ServiceInstanceListSupplier.builder()
.withBlockingDiscoveryClient()
.withSameInstancePreference()
.withHealthChecks()
.build(context);
}
}
请求 DTO:
@Getter
@Setter
public class PasswordResetDTO {
private int id;
}
控制器:
@Autowire
EmailClient emailClient;
@PostMapping("/dummy")
public ResponseEntity<?> test() {
RegisterUserDTO obj = new RegisterUserDTO();
obj.setId(12);
emailClient.setUserRegistration(obj);
return ok().build();
}
假装配置:
spring:
cloud:
loadbalancer:
ribbon:
enable: false
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
preferIpAddress: true
POM.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
我启动了 3 个端点服务实例,它们以循环顺序从 Feign 客户端作为端点使用。到目前为止一切顺利,它按预期工作。但是,例如当我关闭第一个服务实例时,Feign 客户端不会收到此更改的通知,并继续向非活动服务发送请求。你知道当端点服务关闭并且不在活动服务列表中时如何通知消费者服务吗?我想我需要配置一些健康检查?
你知道我该如何解决这个问题吗?
测试问题的源代码:https ://github.com/rcbandit111/eureka-discovery-poc/tree/master