探秘SpringCloud系列《第五篇章:使用Hystrix断路刑堂》

本文介绍如何在SpringCloud项目中实现Hystrix熔断器,通过具体示例展示了配置过程及代码实现,包括依赖添加、超时时间设置、熔断处理类编写等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎来到SpringCloud的江湖,在本章中,我们将向大家传授如何创建SpringCloud的父子项目架构。
知识无止境,故事有好坏,文章纯属虚构,欢迎大家吐槽。
行走江湖,没点伎俩傍身怎么能行。本章牵扯到的技术以及工具如下:
Intellij Idea 2018.1
JDK 8
MAVEN 3.2.2
SpringBoot 1.5.13.RELEASE
Spring-Cloud Edgware.SR3
Fegin声明式调用
Hystrix熔断器

本文基于探秘SpringCloud系列《第三篇章:使用Fegin声明式服务调用》一文继续延伸。

本文中部分代码会有删减,详情请参考GitHub源码

配置Hystrix

  1. 修改Spring-Cloud-Edgware的pom.xml文件,添加Hystrix熔断器的依赖,修改后如下:
    <!--熔断机制-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
  1. 配置user-service的配置文件,开启hystrix断路器,并配置超时时间
server:
  port: 8000
spring:
  application:
    name: user-service

#注册服务到eureka-server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/

####配置请求超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000
            
###开启Hystrix断路器
feign:
  hystrix:
    enabled: true

Hystrix的使用

修改pub-service

编写pub-service的测试类:TestHystrixController.java

package com.maple.pub.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/testHystrix")
public class TestHystrixController {

    @GetMapping("/busyPub")
    public String busyPub(String speak){
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        System.out.println(sdf.format(new Date()) + "【小二收到招呼】:" + speak);
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "不好意思客官,让您久等了...";
    }
}
修改user-service

修改UserGoPubController.java

package com.maple.user.controller;

import com.maple.user.fegin.PubServiceFegin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用户去酒馆消费Controller
 */
@RestController
@RequestMapping("/userGoPub")
public class UserGoPubController {

    private final PubServiceFegin pubServiceFegin;

    @Autowired
    public UserGoPubController(PubServiceFegin pubServiceFegin) {
        this.pubServiceFegin = pubServiceFegin;
    }

    /**
     * 张三去了一个火爆的酒馆,演示Hystrix熔断器
     */
    @GetMapping("/goBusyPub")
    public String goBusyPub(){
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        System.out.println("张三来到了一个非常火爆的酒馆,人多的地方,就肯定好喝");
        String speak = "小二,来两坛上等的二锅头,两斤牛肉,一只烤羊腿";
        System.out.println(sdf.format(new Date()) + "【发送消息】:" + speak);
        String result = pubServiceFegin.busyPub(speak);
        System.out.println(sdf.format(new Date()) + "【收到小二回话】:" + result);
        System.out.println("张三进入了漫长的等待期...");
        return result;
    }
}

添加PubServiceError.java的熔断处理类

package com.maple.user.fegin.fallback;

import com.maple.user.fegin.PubServiceFegin;
import org.springframework.stereotype.Component;

@Component
public class PubServiceError implements PubServiceFegin {

    @Override
    public String busyPub(String speak) {
        return "啊哦,小二太忙了,程序进入到断路器...";
    }
}

修改PubServiceFegin.java

package com.maple.user.fegin;

import com.maple.user.fegin.fallback.PubServiceError;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "pub-service", fallback = PubServiceError.class)
public interface PubServiceFegin {

    /**
     * fegin调用pub-service,酒馆比较繁忙,休眠15s后应答
     * @param speak 招呼小二
     * @return 小二返回的话语
     */
    @GetMapping("/testHystrix/busyPub")
    String busyPub(@RequestParam("speak") String speak);
}

测试Hystrix

我们依次启动eureka-server、user-service、pub-service

浏览器Get请求:http://127.0.0.1:8000/userGoPub/goBusyPub

查看结果:

在这里插入图片描述

本章到此结束。后续文章会陆续更新,文档会同步在CSDN和GitHub保持同步更新。

CSDN:https://blog.csdn.net/qq_34988304/category_8820134.html

Github文档:https://github.com/hack-feng/Java-Notes/tree/master/src/note/SpringCloud

GitHub源码:https://github.com/hack-feng/Spring-Cloud-Edgware.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑小枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值