SpringCloud Gateway 熔断路由器的过滤器

一:熔断路由器的过滤器

1.1 Spring Cloud Gateway 也可以利用 Hystrix 的熔断特性,在流量过大时进行服务降级。首先给项目添加上spring-cloud-starter-netflix-hystrix 依赖。

1.2 修改 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>com.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-gateway-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-gateway-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

增加了spring-cloud-starter-netflix-hystrix 的依赖

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

1.3 修改 application-predicate-path.yml 文件,需要添加 Redis 地址和限流的相关配置,代码如下:

server:
  port: 8769

#---         #三个横线表示再创建一个配置文件
spring:
  #profiles: predicate-path #配置文件名 和 spring.profiles.active 相对应
  #配置程序名为eureka-gateway-client
  application:
    name: eureka-gateway-client
  cloud:
    #设置路由规则
    gateway:
      discovery:
        locator:
          #是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
          #默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
          enabled: true
          ##表示将请求路径的服务名配置改成小写  因为服务注册的时候,向注册中心注册时将服务名转成大写的了
          lower-case-service-id: true
      routes:
      #我们自定义的路由 ID,保持唯一性
      - id: predicate_path
        #代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
        uri: lb://eureka-client
        #uri: http://localhost:8762
        #断言
        predicates:
        #表示将以/HiController开头的请求转发到uri为lb://eureka-client的地址上
        #转发地址格式为 uri/HiController/**
        - Path=/HiController/**
        filters:
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback
        #Hystrix 作为名称生成 HystrixCommand 对象来进行熔断管理
        #fallbackUri: forward:/fallback fallback 时要会调的路径,当调用 Hystrix 的 fallback 被调用时,请求将转发到/fallback URI。

       
logging:
  level:
    org.springframework.cloud.gateway: debug

eureka:
  client:
    #服务注册地址
    serviceUrl:
      #注意: Eureka Server 的注册地址
      #将服务提供者注册到三个Eureka Server中去
      #defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
      #defaultZone: http://peer1:8001/eureka/
      defaultZone: http://localhost:8761/eureka/




增加了这部分的代码
在这里插入图片描述
1.4 启动类EurekaGatewayClientApplication 的代码如下:。

package com.example.eurekagatewayclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;


//开启服务注册于发现
@EnableDiscoveryClient
@SpringBootApplication
//开启Hystrix 的熔断器功能
@EnableHystrix
public class EurekaGatewayClientApplication {

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

}

1.5 新建一个FallbackController 类,作为回调使用。代码如下:

package com.example.eurekagatewayclient.fallback;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FallbackController {

    @GetMapping("/fallback")
    public String fallback() {
        return "服务暂时不可用";
    }

}
发布了50 篇原创文章 · 获赞 75 · 访问量 8648

猜你喜欢

转载自blog.csdn.net/weixin_40991408/article/details/104168364