詳解spring cloud使用Hystrix實(shí)現(xiàn)單個(gè)方法的fallback
本文介紹了spring cloud-使用Hystrix實(shí)現(xiàn)單個(gè)方法的fallback,分享給大家,具體如下:
一、加入Hystrix依賴(lài)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
二、編寫(xiě)Controller
package com.chhliu.springboot.restful.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.chhliu.springboot.restful.feignclient.UserFeignClient;
import com.chhliu.springboot.restful.vo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class RestTemplateControllerHystrixCommand {
@Autowired
private UserFeignClient client; // 使用Feign來(lái)消費(fèi)Restful服務(wù)
@GetMapping("/get/{id}")
@HystrixCommand(fallbackMethod="findByIdFallback")// 使用HystrixCommand注解,在fallbackMethod屬性中指定fallback的方法
public User findById(@PathVariable Long id) {
return client.findById(id);
}
// 覆寫(xiě)fallbackMethod中指定的方法,注意,此方法的返回值,參數(shù)必須與原方法一致
public User findByIdFallback(Long id){
User u = new User();
u.setName("zhangsan");
u.setUsername("chhliu");
u.setId(9L);
return u;
}
}
三、在啟動(dòng)類(lèi)中添加Hystrix支持
@EnableCircuitBreaker
四、添加配置文件
server.port:7904 # spring boot服務(wù)注冊(cè)到Eureka Server上的應(yīng)用名稱(chēng) spring.application.name=springboot-rest-template-feign-hystrix eureka.instance.prefer-ip-address=true # Eureka Server注冊(cè)服務(wù)的地址 eureka.client.service-url.defaultZone=http://chhliu:chhliu123456@localhost:8764/eureka springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RetryRule hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1 #為了測(cè)試Hystrix的fallback效果,此處將超時(shí)時(shí)間設(shè)置成1毫秒
五、測(cè)試
在瀏覽器中輸入:http://localhost:7904/get/2
測(cè)試結(jié)果如下:
{"id":9,"username":"chhliu","name":"zhangsan","age":null,"balance":null}
從上面的測(cè)試結(jié)果可以看出,由于連接超時(shí),直接進(jìn)入了fallback方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java集合中HashSet LinkedHashSet TreeSet三者異同面試精講
這篇文章主要為大家介紹了java集合中HashSet LinkedHashSet TreeSet三者異同面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
java 請(qǐng)求跨域問(wèn)題解決方法實(shí)例詳解
這篇文章主要介紹了java 請(qǐng)求跨域問(wèn)題解決方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java代碼實(shí)現(xiàn)對(duì)properties文件有序的讀寫(xiě)的示例
本篇文章主要介紹了Java代碼實(shí)現(xiàn)對(duì)properties文件有序的讀寫(xiě)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Java中創(chuàng)建線(xiàn)程的四種方法解析
這篇文章主要介紹了Java中創(chuàng)建線(xiàn)程的四種方法解析,線(xiàn)程是Java編程語(yǔ)言中的一個(gè)重要概念,它允許程序在同一時(shí)間執(zhí)行多個(gè)任務(wù),線(xiàn)程是程序中的執(zhí)行路徑,可以同時(shí)執(zhí)行多個(gè)線(xiàn)程,每個(gè)線(xiàn)程都有自己的執(zhí)行流程,需要的朋友可以參考下2023-10-10
Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(概要分析)
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Java計(jì)算兩個(gè)程序運(yùn)行時(shí)間的實(shí)例
下面小編就為大家?guī)?lái)一篇Java計(jì)算兩個(gè)程序運(yùn)行時(shí)間的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

