日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在Spring中使用Netflix Hystrix批注

發(fā)布時(shí)間:2023/12/3 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring中使用Netflix Hystrix批注 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

除了在主頁上引述之外,我想不出更好的方式來描述Netflix Hystrix庫的特定功能:

延遲和容錯(cuò)方式:
停止級(jí)聯(lián)故障。 后備和正常降級(jí)。 無法快速快速恢復(fù)。 使用斷路器隔離線程和信號(hào)量。

我看到了Josh Long( @starbuxman )演示的示例 ,該示例使用了與Spring集成的Hystrix-具體代碼在這里 。 該示例利用注釋使hystrix啟用服務(wù)類。

我的目標(biāo)是在較小的單元測試模式下重新創(chuàng)建類似的設(shè)置。 考慮到這一點(diǎn),請考慮使用Hystrix庫將使以下接口具有容錯(cuò)能力:

package hystrixtest;public interface RemoteCallService {String call(String request) throws Exception;}

還有一個(gè)虛擬的實(shí)現(xiàn)。 虛擬實(shí)現(xiàn)委托給一個(gè)模擬實(shí)現(xiàn),該模擬實(shí)現(xiàn)在前兩次被調(diào)用時(shí)失敗,并在第三次調(diào)用時(shí)成功:

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)用失敗時(shí)退回到“ fallBackCall”方法。

現(xiàn)在,您可以想象,Hystrix庫中必須有一些東西可以攔截用@HystrixCommand注釋注釋的調(diào)用,并使其具有容錯(cuò)能力。 這是一個(gè)有效的測試,將必要的基礎(chǔ)結(jié)構(gòu)包裝在一起–本質(zhì)上,Hystrix庫提供了一個(gè)基于AOP的配套庫,可攔截調(diào)用。 我在這里使用了Spring測試支持來引導(dǎo)AOP基礎(chǔ)結(jié)構(gòu),將HystrixCommandAspect創(chuàng)建為bean,對于前兩個(gè)失敗的調(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庫的簡便方法,如果我要使用該庫,則測試會(huì)轉(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)一步探索這個(gè)樣本, 這里是GitHub庫與工作的測試。

翻譯自: https://www.javacodegeeks.com/2015/01/using-netflix-hystrix-annotations-with-spring.html

總結(jié)

以上是生活随笔為你收集整理的在Spring中使用Netflix Hystrix批注的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。