131、springcloud-hystrix熔断降级

今天的第二篇文章,下午16:27写的,spring-cloud-hystrix熔断降级

之前写过高并发下的nginx的性能优化,熔断降级这个词则是在高并发的时候

由于访问量比较高,超出了其中某一个服务的承载上限时,为了使后续的请求

能够被成功处理,而不导致整个系统宕掉,而采取的保护服务的措施。

下面来讲解spring-cloud-hystrix的使用方法

1、导入spring-cloud-hystrix的相关依赖maven包

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

2、在主类的启动方法上面添加@springcloudApplication标签,提供对 hystrix的支持

package pafc.cloud.feignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringCloudApplication
@EnableFeignClients(basePackages = "pafc.cloud.feignclient.service")
public class FeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignClientApplication.class, args);
    }

}

3、在相应的controller的处理http请求的方法上面,添加@hystrixCommand标签,提供对hystrix熔断的支持

      并且,自定义相应的异常处理方法

package pafc.cloud.feignclient.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pafc.cloud.feignclient.service.OrderService;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping("/orderIdQuery")
public class IndexController {

    @Autowired
    public OrderService orderService;

    @ResponseBody
    @HystrixCommand(fallbackMethod = "saveOrderFail")
    @RequestMapping(value = "/createOrder", method = RequestMethod.GET)
    public Object allGoods() throws InterruptedException {
//        TimeUnit.SECONDS.sleep(10);
        Map<String, Object> data = new HashMap<>();
        data.put("code", 0);
        data.put("data", orderService.getOrderById("0"));
        return data;
    }

    /**
     * 进行熔断降级
     * @return
     */
    private Object saveOrderFail() {
        Map<String, Object> msg = new HashMap<>();
        msg.put("code", -1);
        msg.put("msg", "抢购人数太多,您被挤出来了,稍等重试");
        return msg;
    }

}

4、在springboot的配置文件application.yml文件中添加对hystrix的超时时间的设定,这里设置为500毫秒,如果请求没有响应,就进行熔断,并且调用熔断的异常方法进行处理,和返回。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8181/eureka
spring:
  application:
    name: eureka-client-orders-feign
server:
  port: 8188
ribbon:
  eureka:
    enabled: true

# 修改调用超时时间
feign:
  client:
    config:
      default:
        connectTimeout: 500
        readTimeout: 500

5、将eureka-client-goods服务sleep(10)秒,测试hystrix的熔断情况

 

当使用订单服务去调用商品服务的时候

设置商品服务睡眠10秒

在订单服务的接口上设置hystrix的超时时间为0.2秒

结果如上图所示,hystrix组件成功地进行了熔断。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值