首页 > 编程语言 > 详细

SpringCloud------熔断器Hystrix的使用

时间:2020-03-13 17:33:34      阅读:73      评论:0      收藏:0      [点我收藏+]

1.GitHub地址

https://github.com/Netflix/Hystrix

https://github.com/Netflix/Hystrix/wiki

 

官方文档

https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.2.RELEASE/reference/html/#circuit-breaker-spring-cloud-circuit-breaker-with-hystrix

 

2.为什么要用Hystrix?

在一个分布式系统里,一个服务依赖多个服务,可能存在某个服务调用失败,

比如超时,异常等,如何能保证在一个依赖出问题的情况下,不会导致整体服务失败,

通过Hystrix就可以解决

 

3.功能

提供熔断,隔离,Fallback,cache,监控等功能

 

4.熔断后怎么处理?

出现错误后,可以Fallback错误的处理信息

 

5.代码实现

1)添加依赖

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

<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>
</dependency>

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

 

2)启动类添加@EnableCircuitBreaker

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@MapperScan("cn.ytheng.order_service")
@EnableFeignClients
@EnableCircuitBreaker
public class OrderServiceApplication {

    public static void main(String[] args) {

        SpringApplication.run(OrderServiceApplication.class, args);
    }

}

 

3)添加feign,为了测试Product服务挂掉的情况

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * 商品服务客户端
 * product-service: 调用服务名称,即spring.application.name
 *
 */
@FeignClient(name = "product-service")
public interface ProductClient {

    @GetMapping("/api/v1/product/find")
    String getById(@RequestParam("id") int id);

}

 

4)添加controller

package cn.theng.order_service.controller;

import cn.theng.order_service.service.ProductClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/order")
public class ProductOrderController {

    @Autowired
    private ProductClient productClient;

    @PostMapping("/test2")
    @HystrixCommand(fallbackMethod = "getProductFail")
    public Object test2(@RequestParam("product_id") int productId) {

        //如果把Product服务关掉,这里就会报错,然后调下面的方法
        String product = productClient.getById(productId);

        return "success";
    }

    //方法签名一定要与上面方法保持一致
    public Object getProductFail(int productId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", -1);
        map.put("msg", "目前排队人数过多,请稍后再试...");
        return map;
    }
}

 

5)修改application.yml配置

server:
  port: 8781
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: order-service

#设置调用服务超时时间
#product-service为服务名称,也可以设置为默认值default
feign:
  client:
    config:
      product-service:
        connectTimeout: 5000
        readTimeout: 11000

 

6.访问地址

技术分享图片

 

SpringCloud------熔断器Hystrix的使用

原文:https://www.cnblogs.com/tianhengblogs/p/12487495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!