天天看點

SpringCloud學習筆記8——初級篇之服務降級

5.使用全局服務降級@DefaultProperties(defaultFallback = “”)注解解耦

可以看到fallback方法是一對一的,如果每個方法都跟一個與之比對的fallback方法,會造成備援,而且代碼會膨脹,于是引出一個@DefaultProperties給出預設的方法,這樣沒有指定的方法預設會調用@DefaultProperties中指定的方法,而指定fallback方法的還是會調用指定方法,不會調用預設方法。這樣,通用的和獨享的各自分開

①修改80的controller

SpringCloud學習筆記8——初級篇之服務降級
@HystrixCommand
    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        int a = 1/0;
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    /**
     * 預設方法
     * @return
     */
    public String defaultFallbackMethod(){
        return "執行了預設方法";
    }
           

②測試

通過測試結果可以看到,通路ok執行預設方法,而通路timeout執行的是指定的方法。

SpringCloud學習筆記8——初級篇之服務降級

6.通配服務降級FeignFallback

SpringCloud學習筆記8——初級篇之服務降級

①建立實作類實作PaymentHystrixService接口

package com.hry.springcloud.service;

import org.springframework.stereotype.Component;

@Component
public class PaymentFallbackService implements PaymentHystrixService {
    @Override
    public String paymentInfo_OK(Integer id) {
        return "********* PaymentFallbackService   paymentInfo_OK *********";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "********* PaymentFallbackService   paymentInfo_TimeOut *********";
    }
}

           
SpringCloud學習筆記8——初級篇之服務降級

②修改PaymentHystrixService接口

在注解中添加屬性

SpringCloud學習筆記8——初級篇之服務降級

③恢複controller中的ok

為了示範當機效果,我們把上一節修改的ok再改回去,把添加的内容注釋掉。 類上的全局降級注解先不用管不會影響到ok了。

SpringCloud學習筆記8——初級篇之服務降級

④測試

首先通路ok,能正常調通

SpringCloud學習筆記8——初級篇之服務降級

關閉8001,模拟當機情況,通路ok,此時調用了我們實作類中的方法。

SpringCloud學習筆記8——初級篇之服務降級

7.服務熔斷

總結與回顧

SpringCloud學習筆記8——初級篇之服務降級
SpringCloud學習筆記8——初級篇之服務降級

①服務熔斷知識點

SpringCloud學習筆記8——初級篇之服務降級

②修改8001service

新增

/*----------------服務熔斷----------------*/
    public String paymentCricuitBreaker(Integer id);

    public String paymentCircuitBreaker_fallback(Integer id);
           
/*----------------服務熔斷----------------*/
    
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),   //是否開啟斷路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),  //請求次數(請求容量門檻值)
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),    //時間視窗期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60") //失敗率達到多少後跳閘
    })
    @Override
    public String paymentCricuitBreaker(Integer id) {
        if (id < 0){
            throw new RuntimeException("***id 不能為負數");
        }
        String simpleUUID = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t"+"調用成功,流水号:"+simpleUUID;
    }

    @Override
    public String paymentCircuitBreaker_fallback(Integer id) {
        return "id 不能負數,請稍後再試,id :" +id;
    }
           
SpringCloud學習筆記8——初級篇之服務降級

③修改controller

添加

/*----------------服務熔斷----------------*/

    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String result = paymentService.paymentCricuitBreaker(id);
        log.info("*****result: "+result);
        return result;
    }
           

⑤測試

提高失敗,然後正常通路發現正常通路的也出錯了

SpringCloud學習筆記8——初級篇之服務降級
SpringCloud學習筆記8——初級篇之服務降級

⑧總結

SpringCloud學習筆記8——初級篇之服務降級

斷路器什麼情況下起作用

SpringCloud學習筆記8——初級篇之服務降級

開啟或者關閉的條件

SpringCloud學習筆記8——初級篇之服務降級

斷路器打開之後

SpringCloud學習筆記8——初級篇之服務降級

⑧服務限流

關于服務限流之後的Alibaba sentinel會詳細示範。

8.Hystrix Dashboard

①建立module

SpringCloud學習筆記8——初級篇之服務降級

②編寫pom檔案

<dependencies>

        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <!--引入自定義的api通用包  可以使用公用的entities-->
        <dependency>
            <groupId>com.hry.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--熱部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
           

③修改yml檔案

server:
  port: 9001
           

④建立主啟動類

package com.hry.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashBoardMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashBoardMain9001.class, args);
    }
}

           

⑤修改8001

(1)確定8001的pom檔案中有actuator坐标
SpringCloud學習筆記8——初級篇之服務降級
<!--監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
           
(2)在主啟動類有@EnableCircuitBreaker注解
SpringCloud學習筆記8——初級篇之服務降級
(3)主啟動添加
/**
     * 解決springboot更新後的坑    不配置通路會報錯Unable to connect to Command Metric Stream.
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
           

⑤測試

啟動9001 通路

SpringCloud學習筆記8——初級篇之服務降級
SpringCloud學習筆記8——初級篇之服務降級

在這裡能看到circuit的開關狀态。

SpringCloud學習筆記8——初級篇之服務降級
SpringCloud學習筆記8——初級篇之服務降級

9.總結

官網的圖

SpringCloud學習筆記8——初級篇之服務降級

繼續閱讀