• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • SpringCloud 2.x學習筆記:3、Hystrix(Greenwich版本)

    1、Hystrix基本思想

    在分布式系統當中,服務之間調用關系會隨著業務的發展而變的復雜,一個服務可能依賴多個服務,服務之間層層依賴;一個服務的癱瘓可能導致整個系統的崩潰。

    與電閘思想類似:每棟房子,每戶都安裝了電閘,電閘的作用是保證有一家電路出現短路時,電閘進行斷電跳閘的操作,這樣不至于導致整棟樓用電癱瘓。

    2、新建hystrix模塊

    2.1 pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>hystrix</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>hystrix</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>com.cntaiping.tpa</groupId>
            <artifactId>cloud</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <dependencies>
            <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.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    
    

    2.2 配置文件application.properties

    #服務名稱
    spring.application.name=consumer-hystrix
    #端口號
    server.port=8505
    #在注冊中心中進行注冊
    eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
    

    2.3 Application入口

    package com.cntaiping.tpa.hystrix;
    
    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 HystrixApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HystrixApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    

    2.4 服務消費類

    在hello方法上加上@HystrixCommand注解。該注解對該方法創建了熔斷器的功能,并指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字符串

    package com.cntaiping.tpa.hystrix.service;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    @Service
    public class HelloService {
        @Autowired
        RestTemplate restTemplate;
    
        @HystrixCommand(fallbackMethod = "error")
        public String hello(String name) {
            return restTemplate.getForObject("http://producer/get?name="+name, String.class);
        }
    
        public String error(String name) {
            return "hi,"+name+",sorry,error!";
        }
    }
    
    package com.cntaiping.tpa.hystrix.controller;
    
    import com.cntaiping.tpa.hystrix.service.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        @Autowired
        HelloService helloService;
    
        @GetMapping(value = "/hello")
        public String hello(@RequestParam String name) {
            return helloService.hello( name );
        }
    }
    
    

    3、運行效果

    (1)注冊中心
    在這里插入圖片描述
    (2)consumer-hystrix模塊
    http://localhost:8505/hello?name=chengyq

    在這里插入圖片描述
    (3)測試熔斷

    將服務提供方producer的兩個實例進程(8700和8701)關閉,再次運行http://localhost:8505/hello?name=chengyq

    hi,chengyq,sorry,error!
    

    在這里插入圖片描述
    這就說明當producer模塊不可用的時候,consumer-hystrix調用producer的API接口時,會執行快速失敗,直接返回一組字符串,而不是等待響應超時,這很好的控制了容器的線程阻塞。

    版權聲明:本文為chengyuqiang原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/chengyuqiang/article/details/90715126

    智能推薦

    SpringCloud2.x Greenwich版本搭建:(九) Nacos做為配置中心

    1.提供者中添加依賴 2.新建bootstrap.yml 配置了Nacos config server的地址,這里相關信息與Nacos中的dataId 的格式是對應的 3.控制層 4.Nacos客戶端添加配置文件 5.刪除提供者中的application.properties,啟動提供者測試 6.Nacos客戶端修改配置 7.發布成功再次訪問http://localhost:8701/u...

    SpringCloud2.x Greenwich版本搭建:(八) Nacos做為注冊中心

    1.下載Nacos: https://github.com/alibaba/nacos/releases 2.運行bin目錄下的腳本 我下載的是zip版本,在liunx可以下載tar版本的。 輸入http://localhost:8848/nacos/#/login   賬號密碼默認為nacos 3.創建springboot項目 4.創建提供者Modulen項目nacos-pr...

    SpringCloud 2.x學習筆記:20、Nacos的數據持久化(MySQL)

    1、Nacos集群的基礎需求 參考:http://blog.didispace.com/spring-cloud-alibaba-4/ 在搭建Nacos高可用集群之前,我們需要先修改Nacos的數據持久化配置為MySQL存儲。 如果啟動多個默認配置下的Nacos節點,數據存儲是存在一致性問題的。 為了解決這個問題,Nacos采用了集中式存儲的方式來支持集群化部署(目前只要支持MySQL的存儲)。與...

    SpringCloud 2.x學習筆記:21、Nacos集群模式部署

    1、Nacos集群模式部署 官方參考文檔 https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html 1.1 編輯集群配置文件 分別在三個節點上編輯集群配置文件 1 2 3 4 5 6 1.2 啟動集群節點 sh startup.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 ...

    SpringCloud學習筆記(四)- SpringCloud Hystrix

    Hystrix 服務容錯保護 SpringCloud 在遠程服務調用時, 可能因為網絡原因或是依賴自身服務問題出現調用故障或延遲,這些故障直接導致調用方對外服務也出現延遲,若此時調用方服務不斷增加,這樣就會因為等待或延遲出現人員積壓,最終導致服務崩潰。 針對上述問題, SpringCloud Hystrix 實現了斷路器、線程隔離等一系列服務保護功能,從而對延遲和故障提供了強大的容錯能力。 Hys...

    猜你喜歡

    Spring Cloud 2.x學習筆記:2、feign改進(Greenwich版本)

    1、Feign簡介 Feign 整合了ribbon,具有負載均衡的能力;Feign 采用的是基于接口的注解 2、新建模塊 新建一個服務消費者模塊consumer-feign,代碼結構如下圖所示。 2.1 pom文件 2.2 配置文件application.yml 2.3 啟動類 2.4 服務消費類 (1)Service層 定義一個feign接口,通過@ FeignClient(“服務名...

    SpringCloud 2.x Eureka安全配置

    為什么80%的碼農都做不了架構師?>>>    springcloud升級到2.x后Eureka安全配置與1.x有部分變動,具體配置如下: 1、加入依賴: 2、配置application.yml,自定義用戶名與密碼: 3、新版本的security默認開啟csrf了,這里我們先關掉它,新建一個配置類: 4、瀏覽器訪問,輸入剛才設置的用戶名與密碼: 4、訪問成功:...

    Hystrix——SpringCloud

    Hystrix——SpringCloud 轉載于 方志朋的專欄 https://www.fangzhipeng.com/springcloud/2018/08/04/sc-f4-hystrix.html 故障傳播 微服務架構中,根據業務來拆分成一個個的服務,服務和服務之間可以相互調用(RPC) 在Spring Cloud可以用RestTemplate+Ribbon和Feig...

    SpringCloud——Hystrix

    1、Hystrix熔斷器 進行微服務開發的時候,基礎服務故障會導致多級故障,進而最后導致整個系統都無法繼續使用這種現象叫做雪崩現象。服務雪崩現象是因為服務提供者的不可用導致服務消費者的不可用,并且將這種問題不斷放大的結果積少成多。 于是使用Hystrix隔離出現問題的微服務。防止出現級聯故障,同時提供失敗回退機制,讓系統可以從微服務中進行恢復。 設計目標: 回退任務優雅降級 等待中快速失敗并且迅速...

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    精品国产乱码久久久久久蜜桃不卡