zoukankan      html  css  js  c++  java
  • SpringCloud2.0 Hystrix Feign 基于Feign实现断路器

    原文:https://www.cnblogs.com/songlu/p/9968953.html

    1、启动【服务中心】集群,工程名:springcloud-eureka-server

    2、启动【服务提供者】集群,工程名:springcloud-eureka-client

    3、启动【服务消费者】,工程名:springcloud-eureka-feign

    4、未加入熔断机制,【服务提供者】出现问题,对【服务消费者】的影响

    4.1、停掉【服务提供者】集群中的其中一个服务。本例:停掉端口为 52602 这个服务。

    4.2、打开浏览器,访问 http://localhost:52620/feignInfo,多次刷新该地址

    调用 52601、52603 服务是正常的,但是调用52602服务的时候,出现了阻塞等待,并最终返回了红框内的错误信息。

    假如这是正式的生产环境,访问量很大的情况下,那么就会有很多请求阻塞。这会造成【服务消费者】服务器内存消耗陡增,导致应用崩溃。如果有其他应用需要【服务消费者】返回资源信息,那么调用【服务消费者】的应用也会出现阻塞。这就导致了连锁反应,也就是所谓的雪崩。

    所以,为了避免这种悲剧发生。顺应而生的出现了熔断保护机制。即:访问不通时,要及时作出响应,而不是等待至超时。

    5、修改【服务消费者】,加入熔断机制

    5.1、打开工程:springcloud-eureka-feign

    5.2、修改工程 pom.xml 文件,追加 Hystrix 依赖,添加如下内容:

    1
    2
    3
    4
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    5.3、修改工程启动类,添加注解 @EnableHystrix

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package com.miniooc.eurekafeign;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.hystrix.EnableHystrix;
    import org.springframework.cloud.openfeign.EnableFeignClients;
     
    /**
     * EurekaFeignApplication
     * 应用程序启动类,程序入口
     *
     * @author 宋陆
     * @version 1.0.0
     */
    @EnableHystrix // Feign默认是开启,这个注解可以不加的
    @EnableDiscoveryClient // 启用 Eureka 服务发现
    @EnableFeignClients // 启用 Feign
    @SpringBootApplication
    public class EurekaFeignApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(EurekaFeignApplication.class, args);
        }
     
    }

    5.4、修改【服务消费者】服务类, EurekaFeignService,追加 fallback 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package com.miniooc.eurekafeign.service;
     
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.RequestMapping;
     
    /**
     * EurekaFeignService
     * 服务消费者,调用服务提供者提供的服务,实现业务
     *
     * @author 宋陆
     * @version 1.0.0
     */
    @FeignClient(value = "EUREKA-CLIENT", fallback = EurekaFeignServiceFailure.class) // 调用的服务的名称
    public interface EurekaFeignService {
     
        @RequestMapping(value = "/info")
        String getInfo();
     
    }

    5.5、新增【服务消费者】服务调用失败,回调处理类,EurekaFeignServiceFailure

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package com.miniooc.eurekafeign.service;
     
    import org.springframework.stereotype.Service;
     
    /**
     * EurekaFeignServiceFailure
     * 服务消费者,调用服务提供者提供的服务失败,回调处理类
     *
     * @author 宋陆
     * @version 1.0.0
     */
    @Service
    public class EurekaFeignServiceFailure implements EurekaFeignService {
     
        @Override
        public String getInfo() {
            String message = "网络繁忙,请稍后再试-_-。PS:服务消费者自己提供的信息";
            return message;
        }
     
    }

    5.6、修改工程配置文件 application.yml, 开启 hystrix,追加如下配置

    1
    2
    3
    feign:
      hystrix:
        enabled: true

    6、加入熔断机制后,【服务提供者】出现问题,对【服务消费者】的影响

    6.1、重启【服务提供者】集群,重启【服务消费者】

    6.2、停掉【服务提供者】集群中的其中一个服务。本例:停掉端口为 52602 这个服务。

    6.3、打开浏览器,访问 http://localhost:52620/feignInfo,多次刷新该地址

    调用 52601、52603 服务是正常的,调用52602服务的时候,没有出现阻塞等待,而是返回了【服务消费者】自己提供的返回信息

    【服务消费者】加入熔断机制结束

  • 相关阅读:
    与众不同 windows phone (50)
    与众不同 windows phone (49)
    重新想象 Windows 8.1 Store Apps (93)
    重新想象 Windows 8.1 Store Apps 系列文章索引
    重新想象 Windows 8.1 Store Apps (92)
    重新想象 Windows 8.1 Store Apps (91)
    重新想象 Windows 8.1 Store Apps (90)
    重新想象 Windows 8.1 Store Apps (89)
    重新想象 Windows 8.1 Store Apps (88)
    重新想象 Windows 8.1 Store Apps (87)
  • 原文地址:https://www.cnblogs.com/shihaiming/p/11351460.html
Copyright © 2011-2022 走看看