【欣赏中华武术】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-gateway

SpringCloud Gateway 工作原理图:

图片


1、        新建项目sc-gateway,对应的pom.xml文件如下

 

<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后才有的


 

图片

2、        新建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);

    }

}

3、        编写配置文件application.yml

 

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

 

Spring Cloud Gateway提供了两种配置路由规则的方式:

方式一、通过@Bean自定义RouteLocator

@Bean

public RouteLocator customRouteLocator(RouteLocatorBuilder builder){

    return builder.routes()

                 //basic proxy

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

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

            ).build();

}

 

方式二、通过属于文件或者yml文件配置

spring:

  cloud:

    gateway:

      routes:

      - id: baidu

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

        predicates:

        - Path=/ baidu

 

4、        启动项目,并验证

图片

访问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。


猜你喜欢

转载自blog.51cto.com/15127574/2667898