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鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。java

二、Spring Cloud Gateway入門案例
2.1 建立maven工程,pom.xml文件react

複製代碼

<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>com.lynch</groupId>
    <artifactId>spring-cloud-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <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-gateway</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>
            
        </dependencies>
    </dependencyManagement>

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

複製代碼

 

2.2 Spring Cloud Gateway主程序web

複製代碼

package com.lynch;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.gateway.route.RouteLocator;import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;import org.springframework.context.annotation.Bean;/**
 * Spring Cloud Gateway主程序
 *
 * @author Lynch */@SpringBootApplicationpublic class Application {
    @Bean    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {        return builder.routes()                // basic proxy
                .route(r -> r.path("/baidu")
                .uri("http://baidu.com:80/"))
                .build();
    }    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

複製代碼

 

2.3 編寫application.yml文件spring

複製代碼

server:
  port: 8080spring:
  application:
    name: spring-cloud-gateway
  cloud:
    gateway:
      routes:      - id: xujin_route
        uri: http://www.xujin.org:80/        predicates:        - Path=/xujin
 
logging:
  level:
    org.springframework.cloud.gateway: TRACE
    org.springframework.http.server.reactive: DEBUG
    org.springframework.web.reactive: DEBUG
    reactor.ipc.netty: DEBUG

複製代碼

 

2.4 基本代理路由配置等同寫法
Spring Cloud Gateway提供了兩種配置路由規則的方法apache

第一:經過@Bean自定義RouteLocator安全

複製代碼

    @Bean    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {        return builder.routes()                // basic proxy
                .route(r -> r.path("/baidu")
                .uri("http://baidu.com:80/"))
                .build();
    }

複製代碼

 

第二:經過屬於文件或者yml文件配置架構

複製代碼

spring:
  cloud:
    gateway:
      routes:      - id: xujin_route
        uri: http://www.xujin.org:80/        predicates:        - Path=/xujin

複製代碼

 

2.5 運行測試
訪問http://localhost:8080/baidu,路由轉發到http://www.baidu.com
訪問http://localhost:8080/xujin,路由轉發到http://xujin.orgymlapp

相關文章
相關標籤/搜索