天天看点

SpringCloud学习笔记(十一、断路器Hystric

断路器

断路器是什么?

之前做的,有数据服务和视图服务,视图服务要访问数据服务,如果数据服务挂掉的话,那么肯定得报500,会出现一个错误页面,这个让用户看到不合适啊,得找个东西给他糊弄一下,卖个萌、耍个贱什么的,不行直接给他个爱的魔力转圈圈画面。

当然,这个不是主要的。

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

为了解决这个问题,业界提出了断路器模型。

SpringCloud学习笔记(十一、断路器Hystric

较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。

SpringCloud学习笔记(十一、断路器Hystric

断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

改造项目

还是改造 product-view-service-feign.

pom.xml

添加spring-cloud-starter-netflix-hystrix 以支持断路器。

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>edu.hpu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product-view-service-feign</artifactId>


    <dependencies>
        <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-openfeign</artifactId>  <!--对feign方式的支持-->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId> <!--用于访问路径:/actuator/bus-refresh-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>   <!--用于支持RabbitMQ-->
        </dependency>

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


</project>
      

客户端

ProductClientFeign,客户端的注解中注解中加上fallback的指定类,表示如果访问的 PRODUCT-DATA-SERVICE 不可用的话,就调用 ProductClientFeignHystrix 来进行反馈信息。

package edu.hpu.springcloud.client;

import edu.hpu.springcloud.pojo.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient(value = "PRODUCT-DATA-SERVICE",fallback = ProductClientFeignHystrix.class)
public interface ProductClientFeign {
    @GetMapping("/products")
    public List<Product> listProducts();
}      

ProductClientFeignHystrix类

ProductClientFeignHystrix类实现ProductClientFeign接口。

package edu.hpu.springcloud.client;

import edu.hpu.springcloud.pojo.Product;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class ProductClientFeignHystrix implements ProductClientFeign{

    @Override
    public List<Product> listProducts() {
        List<Product> result=new ArrayList<>();
        result.add(new Product(0,"呵呵,你还想白服务",0));
        return result;
    }
}      

配置

application.yml,在配置中启动断路器。

spring:
  application:
    name:  product-view-service-feign
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5
  zipkin:
    base-url: http://localhost:9411

feign.hystrix.enabled: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"      

启动访问

依次启动注册中心、配置服务器、视图微服务,不用启动数据微服务。

访问地址:

http://localhost:8012/products
SpringCloud学习笔记(十一、断路器Hystric

参考:

【1】、

http://how2j.cn/k/springcloud/springcloud-hystrix/2042.html#nowhere

【2】、

https://www.fangzhipeng.com/springcloud/2018/08/04/sc-f4-hystrix.html