【Spring Cloud Hystrix】快速入門(一)

前面文章基于http://www.reibang.com/writer#/notebooks/24441977/notes/35969879文章的內(nèi)容憔辫,搭建了Spring Cloud Ribbon + Eureka的Spring Boot的工程躯嫉,介紹了Ribbon客戶端負載的相關(guān)內(nèi)容训貌。這篇文章在此基礎上作媚,來聊聊Spring Cloud Hystrix蔬充。另外复哆,本文基于Spring Boot的Finchley.SR2版本所構(gòu)建歧沪。

快速入門

Hystrix服務端

  1. application.yml配置
    構(gòu)建兩個Spring Boot項目碳褒,分別為hystrix-server:9091折砸、hystrix-server:9092,并配置分布式注冊中心Eureka
server:
  port: 9091
spring:
  application:
    name: hystrix-server

eureka:
  client:
     serviceUrl:
        defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  instance:
    hostname: ${spring.application.name}
    instance-id: ${spring.application.name}:${server.port}
    appname: hystrix-server

Hystrix-server啟動類沙峻,端口為9091睦授、9092的Hystrix-server啟動類一樣,并提供了Restful風格的hello接口摔寨,這里就只列出Hystrix-server:9091的啟動類去枷。

@SpringBootApplication
@EnableEurekaClient
@RestController
public class HystrixServer1Application {
    public static void main(String[] args) {
        SpringApplication.run(HystrixServer1Application.class, args);
    }
    @GetMapping("/hello")
    public String hello(){
        return "hello,this is :"+this.getClass().getName();
    }
}

Hystrix客戶端:Hystrix-client
pom配置

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yml配置

server:
  port: 9094
spring:
  application:
    name: hystrix-client
  cloud:
    loadbalancer:
      retry:
        enabled: true #開啟重試機制
hystrix-server:
  ribbon:
    ConnectTimeout: 250 #請求連接超時
    ReadTimeout: 1000  #請求處理的超時時間
    OkToRetryOnAllOperations: true #對所有操作請求都進行重試
    MaxAutoRetriesNextServer: 2 #切換實例的重試次數(shù)
    maxAutoRetries: 2 #對當前實例的重試次數(shù)

eureka:
  client:
     serviceUrl:
        defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  instance:
    hostname: ${spring.application.name}
    instance-id: ${spring.application.name}:${server.port}
    appname: hystrix-client

客戶端代碼:

@SpringBootApplication
@RestController
@EnableCircuitBreaker
@EnableEurekaClient
public class HystrixClient2Application {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @Resource
    HelloService helloService;

    @GetMapping("hystrix-hello")
    public String hello(){
        return helloService.hello();
    }
    public static void main(String[] args) {
        SpringApplication.run(HystrixClient2Application.class, args);
    }
}

@Component
public class HelloService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String hello(){
        String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody();
        return msg;
    }
    public String helloFallback(){
        return "hello , error";
    }
}

依次啟動Eureka-server、hystrix-client服務


Eureka界面

此時調(diào)用http://localhost:9094/hystrix-hello是复,由于Hystrix-server并沒有啟動删顶,所以在執(zhí)行String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody()時,發(fā)生異常淑廊,繼而執(zhí)行helloFallback()方法逗余。會打印出 hello , error

現(xiàn)在啟動Hystrix-server服務,Eureka監(jiān)控界面如下:


Eureka監(jiān)控界面

再次調(diào)用http://localhost:9094/hystrix-hello蒋纬,則會輪番打印出下面的信息

hello,this is :springcloud.hystrixserver2.HystrixServer2Application$$EnhancerBySpringCGLIB$$5b5dd2e6
hello,this is :springcloud.hystrixserver1.HystrixServer1Application$$EnhancerBySpringCGLIB$$92afab75
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末猎荠,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蜀备,更是在濱河造成了極大的恐慌关摇,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碾阁,死亡現(xiàn)場離奇詭異输虱,居然都是意外死亡,警方通過查閱死者的電腦和手機脂凶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門宪睹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愁茁,“玉大人橘券,你說我怎么就攤上這事良姆。” “怎么了厚者?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵罪帖,是天一觀的道長促煮。 經(jīng)常有香客問我,道長整袁,這世上最難降的妖魔是什么菠齿? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮坐昙,結(jié)果婚禮上绳匀,老公的妹妹穿的比我還像新娘。我一直安慰自己炸客,他們只是感情好疾棵,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著嚷量,像睡著了一般陋桂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蝶溶,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天嗜历,我揣著相機與錄音,去河邊找鬼抖所。 笑死梨州,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的田轧。 我是一名探鬼主播暴匠,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼傻粘!你這毒婦竟也來了每窖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弦悉,失蹤者是張志新(化名)和其女友劉穎窒典,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稽莉,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡瀑志,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劈猪。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡昧甘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出战得,到底是詐尸還是另有隱情充边,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布贡避,位于F島的核電站痛黎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏刮吧。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一掖蛤、第九天 我趴在偏房一處隱蔽的房頂上張望杀捻。 院中可真熱鬧,春花似錦蚓庭、人聲如沸致讥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽垢袱。三九已至,卻和暖如春港柜,著一層夾襖步出監(jiān)牢的瞬間请契,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工夏醉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留爽锥,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓畔柔,卻偏偏與公主長得像氯夷,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子靶擦,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355