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。


©著作权归作者所有:来自51CTO博客作者mob604756f1e4c7的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 互联网大佬纷纷搞养殖,这是为哪般~~~
  2. Apple M1芯片软件安装异常的解决方法
  3. 利用Azure虚拟机安装Dynamics 365 Customer Engagement之十一:SQ
  4. 入门版本 MacBook Air 带给我无限惊喜
  5. Iconset:免费的 SVG 图标资源管理软件
  6. iPhone如何修改手机定位?
  7. Golang笔记之基本数据类型
  8. “Affinity Publisher”让排版更简单,轻松完成你的设计大作
  9. IntelliJ IDEA 激活码2021,激活码2020 永久破解教程 适用于2020.3

随机推荐

  1. Android Notification 通知
  2. android 开发问题集,android问题总结,and
  3. Activity详解——Activity的xml配置
  4. ImageView的scaletype属性
  5. Android 基础知识 学习总结
  6. Android基本布局-FrameLayout
  7. Android学习之练笔---计算器的实现
  8. Mac下启动AndroidStudio失败
  9. Android 读取元素中的数据
  10. Android下使用JDOM访问XML文件