我已经使用feign和hystrix实现了一个关于以下文章的http调用:https://blog.crafties.fr/2017/07/23/setup-a-circuit-breaker-with-hystrix/
我正在使用这些依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign</artifactId>
<version>2.0.0.M1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>2.0.0.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
我的实现如下:
@FeignClient(name = "someName",
url = "https://my.url/to/service", fallbackFactory = RTKClientFallbackFactory.class)
public interface RTKClient {
@PostMapping(value = "/v1/topics/rt/messages")
KRestProducerResponse sendT(@RequestBody T t);
}
@Component
public class RTKClientFallbackFactory implements FallbackFactory<RTKClient>{
@Override
public RTKClient create(Throwable cause) {
return new RTKClientFallback(cause);
}
}
public class RTKClientFallback implements RTKClient{
private final Throwable cause;
private final static Logger LOG = LogManager.getLogger(RTKClientFallback.class);
public RTKClientFallback(Throwable cause) {
this.cause = cause;
}
@Override
public KRestProducerResponse sendTemplate(T t) {
KRestProducerResponse response = new KRestProducerResponse();
if (cause instanceof FeignException) {
response.setStatus(((FeignException)cause).status());
}
return response;
}
}
当我手动测试时,这是有效的。 对我来说,问题是对后备单元进行单元测试
这是我尝试的方式:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 0)
@EnableCircuitBreaker
@EnableFeignClients
public class RTKClientFallbackTest {
@Autowired
public RTKClient client;
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089);
@Test
public void should_fall_back_with_error_code_in_response() {
stubFor(get(urlEqualTo("https://my.url/to/service/v1/topics/rt/messages"))
.willReturn(aResponse().withStatus(404)));
T t = new T();
KRestProducerResponse response = client.sendTemplate(t);
assertEquals(404, response.getStatus());
}
}
我不明白的是以下几点:
如何链接我的存根和我的FeignClient? (截至目前,我的测试称为真实端点)
如何在此上下文中启用hystrix(如果我让它弄乱了我的网址,我发现它没有使用后备)
欢迎任何想法。 谢谢