3三、Spring Cloud网关Gateway

Spring Cloud Gateway是由spring官方基于Spring5.0、Spring Boot2.x、Project Reactor等技术开发的网关,目的是代替原先版本中的Spring Cloud Netfilx Zuul,目前Netfilx已经开源了Zuul2.0,但Spring 没有考虑集成,而是推出了本身开发的Spring Cloud GateWay。该项目提供了一个构建在Spring Ecosystem之上的API网关,旨在提供一种简单而有效的途径来发送API,并向他们提供交叉关注点,例如:安全性,监控、埋点,限流等。(具体能够查看官网http://spring.io/projects/spring-cloud-gateway)
Spring Cloud Gateway 工做原理图:
3三、Spring Cloud网关Gateway
一、新建项目sc-gateway,对应的pom.xml文件以下spring

<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>

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

3三、Spring Cloud网关Gateway
二、新建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.ymlspringboot

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类app

Spring Cloud Gateway提供了两种配置路由规则的方式:
方式1、经过@Bean自定义RouteLocatormaven

@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文件配置ide

spring:
    cloud:
        gateway:
            routes:
            - id: baidu
                uri: http://www.baidu.com /
                predicates:
                - Path=/ baidu

四、启动项目,并验证
3三、Spring Cloud网关Gateway
访问http://127.0.0.1:8600/jianshu转发到https://www.jianshu.com/spring-boot

访问http://127.0.0.1:8600/baidu转发到https://www.baidu.com/ui

访问http://127.0.0.1:8600/sina转发到https://www.sina.com.cn/url

相关文章
相关标签/搜索