zoukankan      html  css  js  c++  java
  • feign结合Hystrix使用

    一导入依赖

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
    
            <!--hystrix依赖,主要是用  @HystrixCommand -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
    
            <!--服务注册-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!--服务调用-->
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>

    在配置文件中添加hystrix配置

    #开启熔断机制
    feign.hystrix.enabled=true
    # 设置hystrix超时时间,默认1000ms
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000

    三在service-edu的client包里面创建熔断器的实现类

    package com.atguigu.eduservice.client;
    
    import com.atguigu.commonutils.R;
    import org.springframework.stereotype.Component;
    
    @Component
    public class VodFileDegradeFeignClient implements VodeClient {
        //删除一个视频,出错之后会执行
        @Override
        public R removeAlyVideo(String id) {
            return R.error().message("视频出错了" );
        }
    }

    四修改VodClient接口的注解

    package com.atguigu.eduservice.client;
    
    import com.atguigu.commonutils.R;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    @Component
    @FeignClient(name = "service-vod",fallback = VodFileDegradeFeignClient.class)
    public interface VodeClient {
    
        @DeleteMapping("/eduvod/video/removeAlyVideo/{id}")
        public R removeAlyVideo(@PathVariable("id") String id);
    }
  • 相关阅读:
    JVM学习笔记(三)——类加载机制
    JVM学习笔记(二)——垃圾收集器和内存分配策略
    JVM学习笔记(一) ——Java虚拟机内存结构
    KMP算法及next数组优化
    C Primer Plus课后编程习题
    【小白出错日记】C语言篇
    C语言学习重点提纲
    编译原理学习-形式语言 乔姆斯基文法
    3.11上午课程重点
    FPGA学习-PS2接口
  • 原文地址:https://www.cnblogs.com/lzq210288246/p/13304228.html
Copyright © 2011-2022 走看看