Spring Cloud Gateway 入門案例

♂???木發表於2019-03-12

前言


本文主要講解實現Spring Cloud Gateway路由轉發功能

概念


什麼是Spring Cloud Gateway?

  Spring Cloud Gateway是Spring官方基於Spring 5.0,Spring Boot 2.0和Project Reactor等技術開發的閘道器,Spring Cloud Gateway
旨在為微服務架構提供一種簡單而有效的統一的API路由管理方式。Spring Cloud Gateway作為Spring Cloud生態系中的閘道器,目標是替代Netflix
ZUUL,其不僅提供統一的路由方式,並且基於Filter鏈的方式提供了閘道器基本的功能,例如:安全,監控/埋點,和限流等
複製程式碼

案例


實現路由轉發功能

1、新建springBoot專案
依賴包引入:服務註冊包nacos、服務呼叫包Feign、Gateway包

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.miniwan</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
</project>

複製程式碼

2、將閘道器服務註冊到nacos
a、編寫application.yml檔案,配置服務註冊中心地址

server:
  port: 9001
spring:
  application:
    name: service-agateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
複製程式碼

b、主程式客戶端標記@EnableDiscoveryClient

@EnableDiscoveryClient
@SpringBootApplication
public class AgatewayApplication {

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

}
複製程式碼

3、實現路由轉發
a、application.yml開啟閘道器功能並配置路由轉發

server:
  port: 9001
spring:
  application:
    name: service-agateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    #將此服務設定為閘道器
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      #路由名稱
      - id: consumer_route
      #跳轉路由
        uri: http://www.scnunanshen.online/
      #斷言,設定攔截條件,
        predicates:
        - Path=/discovery

#注意事項一
#上面-Path=/discovery 設定方法生效路由只有http://localhost:9001/discovery
#http://localhost:9001/discovery/xxx...   不能被攔截
#http://localhost:9001/service-consumer/discovery 不能被攔截
#當然也可以將路由設定為-Path=/discovery/**,此時http://localhost:9001/discovery/xxx...也能被攔截

#注意事項二
#此處spring.cloud.gateway.locator.enabled需要設定為true 閘道器轉發功能才能生效
複製程式碼

上面設定:http://localhost:9001/discovery跳轉到http://www.scnunanshen.online/

相關文章