Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the Hystrix request command in Spring Cloud

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Spring Cloud how to customize the Hystrix request command, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Custom HystrixCommand

In addition to using the @ HystrixCommand annotation, we can also inherit custom classes from HystrixCommand, as follows:

Public class BookCommand extends HystrixCommand {private RestTemplate restTemplate; @ Override protected Book getFallback () {return new Book (selected Notes of Song Poems, 88, Qian Zhongshu, Sanlian Bookstore);} public BookCommand (Setter setter, RestTemplate restTemplate) {super (setter); this.restTemplate = restTemplate } @ Override protected Book run () throws Exception {return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);}}

Inject RestTemplate into the BookCommand, and then rewrite two methods: one is getFallback, which will be called back when the service call fails, and the other is the run method, which is called when the request is executed. The first parameter of the constructor is mainly used to hold some grouping information.

Synchronous and asynchronous invocation

When BookCommand is created successfully, we can call it in our Controller, as follows:

@ RequestMapping ("/ test1") public Book test1 () throws ExecutionException, InterruptedException {BookCommand bookCommand = new BookCommand (HystrixCommand.Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey (")), restTemplate); / / synchronous call / / Book book1 = bookCommand.execute (); / / Asynchronous call Future queue = bookCommand.queue (); Book book = queue.get (); return book;}

With regard to this call, I would like to say the following:

1. After getting the BookCommand object, we have two ways to execute the request, one is to call the execute method to initiate a synchronous request, and the other is to call the queue method to initiate an asynchronous request.

two。 The request result can be returned directly in the synchronization request.

3. In an asynchronous request, we need to get the result of the request through the get method, and we can also pass in the timeout when calling the get method.

The implementation results are as follows:

If we start a service registry, start two service provider instances, start a service consumer, and then shut down a service provider, we will see the following page at intervals:

Implement asynchronous requests through annotations

In the previous article (Circuit Breaker Hystrix in Spring Cloud) we used annotations to configure Hystrix, which we wrote as follows:

@ HystrixCommandpublic Book test2 () {return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);}

So this kind of request is a form of synchronous request, what if we want to use annotations to implement asynchronous requests? It's simple, two steps:

1. Configure Bean for HystrixCommandAspect

Configure a Bean for HystrixCommandAspect in the entry class of the project, as follows:

@ Beanpublic HystrixCommandAspect hystrixCommandAspect () {return new HystrixCommandAspect ();}

two。 Execute the call through AsyncResult

The @ HystrixCommand annotation is still used, but the implementation of the method uses AsyncResult, as follows:

@ HystrixCommand public Future test3 () {return new AsyncResult () {@ Override public Book invoke () {return restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class);}

OK, after which we can implement asynchronous invocation through annotations. The calling method is as follows:

@ RequestMapping ("/ test3") public Book test3 () throws ExecutionException, InterruptedException {Future bookFuture = bookService.test3 (); / / you can also set the timeout return bookFuture.get () when calling the get method;} support for responsive function programming

Some partners may have a penchant for responsive functional programming, which is also supported by Hystrix. After we have obtained the BookCommand object, we can also obtain an Observable to process the data in the following two ways:

BookCommand bookCommand = new BookCommand (HystrixCommand.Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey (")), restTemplate); Observable observe = bookCommand.observe (); Observable bookObservable = bookCommand.toObservable ()

With regard to the use of RxJava for Observable friends, I am not going to repeat it here, but the difference between observe and toObservable:

The 1.observe command returns an Observable immediately when it is called.

2.toObservable does not immediately return an Observable, which is executed when the subscriber invokes the data.

Support responsive functional programming through annotations

Of course, responsive functional programming can also be achieved through annotations, as follows:

@ HystrixCommandpublic Observable test4 () {return Observable.create (new Observable.OnSubscribe () {@ Override public void call (Subscriber)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report