06苦囱、SpringCloud Ribbon整合Hystrix

一、代碼示例

說明:此處使用的SpringBoot版本為2.1.13.RELEASE宁脊,SpringCloud版本為Greenwich.SR5断国。
此處不再貼server與client代碼
1、maven依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

2榆苞、application.yml

server:
  port: 9004
spring:
  application:
    name: ribbon-hystrix
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ribbon-hystrix-9004
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: ribbon-hystrix-9004
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

3稳衬、啟動(dòng)類
啟動(dòng)類增加@EnableHystrix注解開啟Hystrix

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableHystrix
public class RibbonHystrix9004Application {

    public static void main(String[] args) {
        SpringApplication.run(RibbonHystrix9004Application.class,args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4、其他java類
Controller類
Controller類增加@HystrixCommand注解坐漏,通過fallbackMethod指定熔斷對(duì)應(yīng)的方法薄疚,fallbackMethod對(duì)應(yīng)的值為Controller類下的方法

package org.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
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 org.springframework.web.client.RestTemplate;

@RestController
public class RibbonHystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello/{name}")
    @HystrixCommand(fallbackMethod = "helloError")
    public String hello(@PathVariable String name){
        return restTemplate.getForObject("http://client/hello/"+name,String.class);
    }

    public String helloError(String name){
        return "helloError,"+name;
    }
}

2、測(cè)試驗(yàn)證

先后啟動(dòng)服務(wù)server赊琳、client與本服務(wù)街夭,訪問http://localhost:7001/

image.png

說明服務(wù)均啟動(dòng)成功。
再訪問http://localhost:9004/hello/zs
image.png


image.png

然后躏筏,再把client停了板丽,再次訪問
image.png

說明代碼生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽(yáng)老師的視頻

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末趁尼,一起剝皮案震驚了整個(gè)濱河市埃碱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌弱卡,老刑警劉巖乃正,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異婶博,居然都是意外死亡瓮具,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來名党,“玉大人叹阔,你說我怎么就攤上這事〈茫” “怎么了耳幢?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)欧啤。 經(jīng)常有香客問我睛藻,道長(zhǎng),這世上最難降的妖魔是什么邢隧? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任店印,我火速辦了婚禮,結(jié)果婚禮上倒慧,老公的妹妹穿的比我還像新娘按摘。我一直安慰自己,他們只是感情好纫谅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布炫贤。 她就那樣靜靜地躺著,像睡著了一般付秕。 火紅的嫁衣襯著肌膚如雪兰珍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天盹牧,我揣著相機(jī)與錄音俩垃,去河邊找鬼。 笑死汰寓,一個(gè)胖子當(dāng)著我的面吹牛口柳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播有滑,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼跃闹,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了毛好?” 一聲冷哼從身側(cè)響起望艺,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎肌访,沒想到半個(gè)月后找默,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡吼驶,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年惩激,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了店煞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡风钻,死狀恐怖顷蟀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情骡技,我是刑警寧澤鸣个,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站布朦,受9級(jí)特大地震影響囤萤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜喝滞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一阁将、第九天 我趴在偏房一處隱蔽的房頂上張望膏秫。 院中可真熱鬧右遭,春花似錦、人聲如沸缤削。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)亭敢。三九已至滚婉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間帅刀,已是汗流浹背让腹。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留扣溺,地道東北人骇窍。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像锥余,于是被迫代替她去往敵國(guó)和親腹纳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容