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......");
    }
}

发布了43 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43866295/article/details/93876562