Hystrix断路器配置

一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>03-open-feign</artifactId>
        <version>1.0-SNAPSHOT</version><!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>

    <dependencies>
        <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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--Spring Cloud 熔断器起步依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

二、开启、配置断路器application.yml (在提供端使用)

spring:
  application:
    name: userserver03
eureka:
  client:
    service-url: #eureka服务端地址
      defaultZone: http://127.0.0.1:10010/eureka
    fetch-registry: true  # 应用是否可以去拉取服务列表到本地
    register-with-eureka: true # 可以不往eureka服务端注册
    registry-fetch-interval-seconds: 10  #间隔10秒去拉取,时间越短脏读越少,性能消耗大
server:
  port: 10012
feign:
  hystrix:
    enabled: true

ribbon:
  http:
    client:
    enabled: true #开启ribbon超时管理
  ReadTimeout: 3000 #请求超时时间
  ConnectTimeout: 3000 #连接超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000  #超时时间
        timeout:
          enabled: true #开启熔断
      circuitBreaker:
        requestVolumeThreshold: 10  #熔断触发的最小个数,即在一定的时间窗口内请求达到一定的次数,默认20
        errorThresholdPercentage: 50  #失败率达到多少百分比后熔断 默认值:50
        sleepWindowInMilliseconds: 5000  #熔断多长时间后,尝试放一次请求进来,默认5秒
      metrics:
        rollingStats:
          timeInMilliseconds: 5000  #时间窗口,默认10s

三、与openfeign结合使用

1、提供端接口

2、创建消费端与提供端的feign接口

3、开启openfeign客户端,实现断路方案

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.example.demo.feign"})
public class UserServer03 {

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

}

 4、消费端接口

import com.example.demo.feign.UserOrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserOrderFeign userOrderFeign;

    @GetMapping
    public String toUserOrder(Integer time){
        String s = userOrderFeign.toOrder(time);
        return s;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值