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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在Spring中使用Netflix Hystrix批注

發布時間:2023/12/3 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring中使用Netflix Hystrix批注 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

延遲和容錯方式:
停止級聯故障。 后備和正常降級。 無法快速快速恢復。 使用斷路器隔離線程和信號量。

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

我的目標是在較小的單元測試模式下重新創建類似的設置。 考慮到這一點,請考慮使用Hystrix庫將使以下接口具有容錯能力:

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

還有一個虛擬的實現。 虛擬實現委托給一個模擬實現,該模擬實現在前兩次被調用時失敗,并在第三次調用時成功:

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;} }

遠程調用已使用@Hystrixcommand批注進行了批注,并具有基本配置,以便在遠程調用失敗時退回到“ fallBackCall”方法。

現在,您可以想象,Hystrix庫中必須有一些東西可以攔截用@HystrixCommand注釋注釋的調用,并使其具有容錯能力。 這是一個有效的測試,將必要的基礎結構包裝在一起–本質上,Hystrix庫提供了一個基于AOP的配套庫,可攔截調用。 我在這里使用了Spring測試支持來引導AOP基礎結構,將HystrixCommandAspect創建為bean,對于前兩個失敗的調用,該調用轉到“ fallBackCall”,并在第三次成功進行:

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的項目提供了一種配置Netflix庫的簡便方法,如果我要使用該庫,則測試會轉換為該庫,現在借助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();}} }

如果您有興趣進一步探索這個樣本, 這里是GitHub庫與工作的測試。

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。