javascript
在Spring中使用Netflix Hystrix批注
除了在主頁上引述之外,我想不出更好的方式來描述Netflix Hystrix庫的特定功能:
延遲和容錯方式:
停止級聯(lián)故障。 后備和正常降級。 無法快速快速恢復(fù)。 使用斷路器隔離線程和信號量。
我看到了Josh Long( @starbuxman )演示的示例 ,該示例使用了與Spring集成的Hystrix-具體代碼在這里 。 該示例利用注釋使hystrix啟用服務(wù)類。
我的目標(biāo)是在較小的單元測試模式下重新創(chuàng)建類似的設(shè)置。 考慮到這一點(diǎn),請考慮使用Hystrix庫將使以下接口具有容錯能力:
package hystrixtest;public interface RemoteCallService {String call(String request) throws Exception;}還有一個虛擬的實(shí)現(xiàn)。 虛擬實(shí)現(xiàn)委托給一個模擬實(shí)現(xiàn),該模擬實(shí)現(xiàn)在前兩次被調(diào)用時失敗,并在第三次調(diào)用時成功:
package hystrixtest;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer;import static org.mockito.Mockito.*;public class DummyRemoteCallService implements RemoteCallService {private RemoteCallService mockedDelegate;public DummyRemoteCallService() {try {mockedDelegate = mock(RemoteCallService.class);when(mockedDelegate.call(anyString())).thenThrow(new RuntimeException("Deliberately throwing an exception 1")).thenThrow(new RuntimeException("Deliberately throwing an exception 2")).thenAnswer(new Answer<String>() {@Overridepublic String answer(InvocationOnMock invocationOnMock) throws Throwable {return (String) invocationOnMock.getArguments()[0];}});}catch(Exception e) {throw new IllegalStateException(e);}}@Override@HystrixCommand(fallbackMethod = "fallBackCall")public String call(String request) throws Exception {return this.mockedDelegate.call(request);}public String fallBackCall(String request) {return "FALLBACK: " + request;} }遠(yuǎn)程調(diào)用已使用@Hystrixcommand批注進(jìn)行了批注,并具有基本配置,以便在遠(yuǎn)程調(diào)用失敗時退回到“ fallBackCall”方法。
現(xiàn)在,您可以想象,Hystrix庫中必須有一些東西可以攔截用@HystrixCommand注釋注釋的調(diào)用,并使其具有容錯能力。 這是一個有效的測試,將必要的基礎(chǔ)結(jié)構(gòu)包裝在一起–本質(zhì)上,Hystrix庫提供了一個基于AOP的配套庫,可攔截調(diào)用。 我在這里使用了Spring測試支持來引導(dǎo)AOP基礎(chǔ)結(jié)構(gòu),將HystrixCommandAspect創(chuàng)建為bean,對于前兩個失敗的調(diào)用,該調(diào)用轉(zhuǎn)到“ fallBackCall”,并在第三次成功進(jìn)行:
package hystrixtest;import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TestRemoteCallServiceHystrix {@Autowiredprivate RemoteCallService remoteCallService ;@Testpublic void testRemoteCall() throws Exception{assertThat(this.remoteCallService.call("test"), is("FALLBACK: test"));assertThat(this.remoteCallService.call("test"), is("FALLBACK: test"));assertThat(this.remoteCallService.call("test"), is("test"));}@Configuration@EnableAspectJAutoProxypublic static class SpringConfig {@Beanpublic HystrixCommandAspect hystrixCommandAspect() {return new HystrixCommandAspect();}@Beanpublic RemoteCallService remoteCallService() {return new DummyRemoteCallService();}} }Spring-Cloud為基于Spring-Boot的項(xiàng)目提供了一種配置Netflix庫的簡便方法,如果我要使用該庫,則測試會轉(zhuǎn)換為該庫,現(xiàn)在借助Spring-Boot注釋掉一堆配置:
package hystrixtest;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is;@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration public class TestRemoteCallServiceHystrix {@Autowiredprivate RemoteCallService remoteCallService;@Testpublic void testRemoteCall() throws Exception {assertThat(this.remoteCallService.call("test"), is("FALLBACK: test"));assertThat(this.remoteCallService.call("test"), is("FALLBACK: test"));assertThat(this.remoteCallService.call("test"), is("test"));}@Configuration@EnableAutoConfiguration // @EnableAspectJAutoProxy@EnableHystrixpublic static class SpringConfig {// @Bean // public HystrixCommandAspect hystrixCommandAspect() { // return new HystrixCommandAspect(); // }@Beanpublic RemoteCallService remoteCallService() {return new DummyRemoteCallService();}} }如果您有興趣進(jìn)一步探索這個樣本, 這里是GitHub庫與工作的測試。
翻譯自: https://www.javacodegeeks.com/2015/01/using-netflix-hystrix-annotations-with-spring.html
總結(jié)
以上是生活随笔為你收集整理的在Spring中使用Netflix Hystrix批注的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抗菌药物备案表范文(抗菌药物备案表)
- 下一篇: linux c库函数(Linux c库