【欣賞中華武術】SpringCloud 2.x之網關Spring Cloud Gateway

Spring Cloud Gateway是由spring官方基於Spring5.0Spring Boot2.xProject Reactor等技術開發的網關,目的是代替原先版本中的Spring Cloud Netfilx Zuul,目前Netfilx已經開源了Zuul2.0,但Spring 沒有考慮集成,而是推出了本身開發的Spring Cloud GateWay。該項目提供了一個構建在Spring Ecosystem之上的API網關,旨在提供一種簡單而有效的途徑來發送API,並向他們提供交叉關注點,例如:安全性,監控、埋點,限流等。(具體能夠查看官網http://spring.io/projects/spring-cloud-gatewaygit

SpringCloud Gateway 工做原理圖:spring

圖片


一、        新建項目sc-gateway,對應的pom.xml文件以下apache

 

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">


   <modelVersion>4.0.0</modelVersion>



   <groupId>spring-cloud</groupId>

   <artifactId>sc-gateway</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>



   <name>sc-gateway</name>

   <url>http://maven.apache.org</url>



   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>



   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>



      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>



   <dependencies>

      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-gateway</artifactId>

      </dependency>



   </dependencies>

</project>


能夠看到spring cloud gateway是從spring cloud 2.x後纔有的安全


 

圖片

二、        新建springboot啓動類
springboot

 

package sc.gateway;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication

public class GatewayApplication {


public static void main(String[]args) {

       SpringApplication.run(GatewayApplication.class, args);

    }

}

三、        編寫配置文件application.ymlapp

 

server:

  port: 8600



spring:

  application:

    name: sc-gateway

  cloud:

    gateway:

      routes:

      - id: baidu

        uri:http://www.baidu.com/

        predicates:

          - Path=/baidu/**

      - id: jianshu

        uri:http://www.jianshu.com/

        predicates:

          - Path=/jianshu/**

備註:gateway的配置項參考org.springframework.cloud.gateway.config.GatewayProperties
maven

 

Spring Cloud Gateway提供了兩種配置路由規則的方式:ide

方式1、經過@Bean自定義RouteLocatorspring-boot

@Bean

public RouteLocator customRouteLocator(RouteLocatorBuilder builder){

    return builder.routes()

                 //basic proxy

                 .route("baidu", r -> r.path("/baidu ")

                 .uri("http://www.baidu.com.cn/")

            ).build();

}

 

方式2、經過屬於文件或者yml文件配置ui

spring:

  cloud:

    gateway:

      routes:

      - id: baidu

        uri: http://www.baidu.com/

        predicates:

        - Path=/ baidu

 

四、        啓動項目,並驗證

圖片

訪問http://127.0.0.1:8600/jianshu轉發到https://www.jianshu.com/

訪問http://127.0.0.1:8600/baidu轉發到https://www.baidu.com/

訪問http://127.0.0.1:8600/sina轉發到https://www.sina.com.cn/

 

源碼:

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-gateway

       在講解zuul時,有讀者說沒有過濾器的網關是沒有靈魂的。接下來找個時間說說網關的過濾器filter。

相關文章
相關標籤/搜索