天天看點

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/




           

增加了這部分的代碼

SpringCloud Gateway 熔斷路由器的過濾器

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 "服務暫時不可用";
    }

}
           

繼續閱讀