SpringCloud 2.x学习笔记:12、Spring Cloud Gateway简单例子(Greenwich版本)

1、Spring Cloud Gateway介绍

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。
Spring Cloud Gateway构建于Spring生态系统之上,包括Spring5,SpringBoot2等。它的目标是提供简单、有效的方式路由的API

Spring Cloud Gateway不能在传统的Servlet容器中工作。

请参考官方教程:
https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#_spring_cloud_gateway

2、简单例子演示

(1)父级pom



    4.0.0
    com.cntaiping.tpa
    gateway
    pom
    1.0-SNAPSHOT

    
        UTF-8
        UTF-8
        1.8
        Greenwich.SR1
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
        
    

    
        eureka-server
        simple-geteway
    

    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

(2)pom.xml



    4.0.0
    com.cntaiping.tpa
    simple-geteway
    0.0.1-SNAPSHOT
    jar
    simple-geteway
    Demo project for Spring Boot

    
        com.cntaiping.tpa
        gateway
        1.0-SNAPSHOT
    

    
    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
    



添加spring-cloud-starter-gateway依赖。请注意,这里千万不能有spring-boot-starter-web(父级pom.xml也没有spring-boot-starter-web),它们两个不能同时存在。
(3)application.properties

server.port= 7010

(4)Application类

package com.cntaiping.tpa.simplegeteway;

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;

@SpringBootApplication
public class SimpleGetewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(SimpleGetewayApplication.class, args);
    }

    /**
     * gateway中使用RouteLocator的Bean进行路由转发,将请求进行处理,最后转发到目标的下游服务。
     * 本例中将请求转发到http://httpbin.org:80这个地址上
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                //适用于lambda表达式的接口称之为函数型接口(只有一个抽象方法)
                //含参Lambda表达式:(x) -> x.f()
                //添加一个route让请求“/get”请求都转发到“http://httpbin.org/get”
                .route(p -> p.path("/get")
                             .filters(f -> f.addRequestHeader("flag", "HelloWorld"))
                             .uri("http://httpbin.org:80"))
                .build();
    }
}

说明:

httpbin.org 这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。

3、运行效果

http://localhost:7010/get

{
  "args": {}, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "zh-CN,zh;q=0.9", 
    "Flag": "HelloWorld", 
    "Forwarded": "proto=http;host=\"localhost:7010\";for=\"0:0:0:0:0:0:0:1:55191\"", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", 
    "X-Forwarded-Host": "localhost:7010"
  }, 
  "origin": "0:0:0:0:0:0:0:1, 180.169.108.92, ::1", 
  "url": "https://localhost:7010/get"
}

SpringCloud 2.x学习笔记:12、Spring Cloud Gateway简单例子(Greenwich版本)_第1张图片

你可能感兴趣的:(SpringCloud,2.x学习笔记)