一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解spring cloud使用Hystrix實現(xiàn)單個方法的fallback

詳解spring cloud使用Hystrix實現(xiàn)單個方法的fallback

2021-03-26 13:39牛奮lch Java教程

本篇文章主要介紹了詳解spring cloud-使用Hystrix實現(xiàn)單個方法的fallback,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了spring cloud-使用Hystrix實現(xiàn)單個方法的fallback,分享給大家,具體如下:

一、加入Hystrix依賴

?
1
2
3
4
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

二、編寫Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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來消費Restful服務(wù)
   
  @GetMapping("/get/{id}")
  @HystrixCommand(fallbackMethod="findByIdFallback")// 使用HystrixCommand注解,在fallbackMethod屬性中指定fallback的方法
  public User findById(@PathVariable Long id) {
    return client.findById(id);
  }
   
    // 覆寫fallbackMethod中指定的方法,注意,此方法的返回值,參數(shù)必須與原方法一致
  public User findByIdFallback(Long id){
    User u = new User();
    u.setName("zhangsan");
    u.setUsername("chhliu");
    u.setId(9L);
    return u;
  }
}

三、在啟動類中添加Hystrix支持

?
1
@EnableCircuitBreaker

四、添加配置文件

?
1
2
3
4
5
6
7
8
server.port:7904
# spring boot服務(wù)注冊到Eureka Server上的應(yīng)用名稱
spring.application.name=springboot-rest-template-feign-hystrix
eureka.instance.prefer-ip-address=true
# Eureka Server注冊服務(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 #為了測試Hystrix的fallback效果,此處將超時時間設(shè)置成1毫秒

五、測試

在瀏覽器中輸入:http://localhost:7904/get/2

測試結(jié)果如下:

{"id":9,"username":"chhliu","name":"zhangsan","age":null,"balance":null} 

從上面的測試結(jié)果可以看出,由于連接超時,直接進入了fallback方法。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/54917272

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 999久久久免费精品国产牛牛 | 日本又大又硬又粗的视频 | 亚洲aⅴ男人的天堂在线观看 | 精品国产品香蕉在线观看75 | 亚洲成片在线看 | 婷婷久久综合九色综合九七 | 999精品视频这里只有精品 | 羞羞答答免费人成黄页在线观看国产 | 国产一区二区三区欧美精品 | 免费观看视频网站 | 欧美美女一区二区三区 | 亚洲国产成人久久99精品 | 亚洲AV 日韩 国产 有码 | 午夜亚洲福利 | 国产一二在线观看视频网站 | 秋葵视频成人 | 天海翼最新作品 | 国色天香社区视频免费观看3 | 任我鲁精品视频精品 | 精品一二三区久久AAA片 | 成人在线免费观看 | 香蕉视频在线观看网址 | 我与白丝同桌的故事h文 | 99精品国产美女福到在线不卡 | 97青草香蕉依人在线播放 | 国产麻豆精品原创 | 国产极品精频在线观看 | 午夜dj影院在线观看完整版 | 美女把腿开让我 | 欧美日韩国产精品综合 | 国产二区三区 | 91久久偷偷做嫩草影院免费看 | 波多野结衣178部中文字幕 | 亚洲成人77777 | 武侠古典久久亚洲精品 | 99精品影视| 国产自在自线午夜精品之la | 99国产精品久久久久久久... | 黄动漫软件车车好快的车车 | 成人网子| 视频在线观看高清免费看 |