spring boot gateway 集成熔断器 hystrix

spring boot gateway 集成熔断器 hystrix

1. pom依赖

父pom dependencyManagement

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>
                    org.springframework.cloud
                </groupId>
                <artifactId>
                    spring-cloud-dependencies
                </artifactId>
                <version>
                    Greenwich.RELEASE
                </version>
                <type>
                    pom
                </type>
                <scope>
                    import
                </scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

gateway pom

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</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-netflix-hystrix</artifactId>
        </dependency>

2. application.properties

server.port=8083
spring.cloud.gateway.routes[0].id=host_route
# lb:// + spring cloud中的服务名, 表示在spring cloud中查找服务
spring.cloud.gateway.routes[0].uri=lb://cloud2
# 请求地址前缀为to_cloud2,则被转发至uri配置的服务
spring.cloud.gateway.routes[0].predicates[0]=Path=/to_cloud2/**
# 去除一个前缀 这里为to_cloud
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[0].filters[1].name=Hystrix
spring.cloud.gateway.routes[0].filters[1].args.name=defaultfallback
# 发生错误时调用的地址
spring.cloud.gateway.routes[0].filters[1].args.fallbackUri=forward:/defaultfallback
hystrix.command.default.execution.isolation.strategy=SEMAPHORE
# 超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.shareSecurityContext=true
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/

hystrix.command.default.metrics.rollingStats.timeInMilliseconds=50000
# 前提条件,一定时间内发起一定数量的请求。  也就是5秒钟内(这个5秒对应下面的滚动窗口长度)至少请求1次,熔断器才发挥起作用。  默认20
hystrix.command.default.circuitBreaker.requestVolumeThreshold=1
# 错误百分比。达到或超过这个百分比,熔断器打开。  比如:5秒内有4个请求,2个请求超时或者失败,就会自动开启熔断
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50

熔断器详细配置参考 https://blog.csdn.net/chenxyz707/article/details/80913725

3.主类

@SpringBootApplication
@EnableEurekaClient
@RestController
public class Application {

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

    @RequestMapping("defaultfallback")
    public ResponseEntity fail() {

        return ResponseEntity.ok("fail......");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值