SpringCloud 2.x学习笔记:12、Spring Cloud Gateway简单例子(Greenwich版本)-程序员宅基地

技术标签: SpringCloud 2.x学习笔记  gateway  

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

<?xml version="1.0" encoding="UTF-8"?>
<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.cntaiping.tpa</groupId>
    <artifactId>gateway</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <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>Greenwich.SR1</spring-cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>eureka-server</module>
        <module>simple-geteway</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.cntaiping.tpa</groupId>
    <artifactId>simple-geteway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>simple-geteway</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!--因为spring cloud gateway是基于webflux的,不要导入spring-boot-start-web-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

</project>

添加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"
}

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chengyuqiang/article/details/92981857

智能推荐

JVM实战学习——排查java程序 磁盘IO占用过高、CPU占用过高(pidstat)_pidstat查看哪个线程io高-程序员宅基地

文章浏览阅读4.7k次,点赞3次,收藏11次。JVM实战学习——排查java程序 磁盘IO占用过高、CPU占用过高、内存占用过高(pidstat)一、排查cpu高占用1.示例代码使用以下代码,启动的服务会产生cpu资源大量占用的情况1)占有大量CPU资源2)启动类2.查询项目进程1)使用 jps 命令查询项目启动的进程[root]# jps15539 jar ## 其中15539就是项目进程1349 -- process information unavailable15673 Jps2)或者使用 ps -ef |_pidstat查看哪个线程io高

TIOBE 1 月编程语言排行榜:C 语言再度「C 位」出道!_编程语言排行榜tiobe-程序员宅基地

文章浏览阅读1.8w次,点赞88次,收藏103次。整理 | 屠敏出品 | CSDN(ID:CSDNnews)在 2020 年初雪来临之际,TIOBE 官方在最新发布的 1 月编程语言榜单中为我们最终揭开了「 2019 年度编程语言」的神秘面纱,然意料之外情理之中,获此殊荣的并非是风风火火吵闹了一年又一年的 Python,而是一位低调的老兵——C 语言。Python 惜败,C成为 2019 年度编程语言曾几何时,凭借着“人生..._编程语言排行榜tiobe

Rocket MQ(四)Topic,Topic分片和Queue-程序员宅基地

文章浏览阅读1.1w次,点赞11次,收藏59次。Queue是RocketMQ中的另一个重要概念。在对该概念进行分析介绍前,我们先来看一张图:从本质上来说,RocketMQ中的Queue是数据分片的产物。为了更好地理解Queue的定义,我们还需要引入一个新的概念:Topic分片。在分布式数据库和分布式缓存领域,分片概念已经有了清晰的定义。同理,对于RocketMQ,一个Topic可以分布在各个Broker上,我们可以把一个Topic分布在一个Broker上的子集定义为一个Topic分片。对应上图,TopicA有3个Topic分片,分布在Broker

彻底解决Compiling for iOS xxx, but module ‘xxx‘ has a minimum deployment target of iOS xxx 错误_compiling for ios 9.0, but module 'reactiveswift' -程序员宅基地

文章浏览阅读1.1w次。target ios版本和第三方库ios版本问题问题描述解决方法查看iphone iPad target的最低ios版本修改pod里第三方库问题描述这几天编辑xcode偶尔会发现这个错误,但是有时候重新编译一下错误就消失了,今天彻底解决一下这个错误错误提示:Compiling for iOS 10.0, but module ‘SwiftyJSON’ has a minimum deployment target of iOS 12.0: /Users/tdw/Library/Developer/Xc_compiling for ios 9.0, but module 'reactiveswift' has a minimum deployment t

Android NDK thread 回收crash, signal 4 (SIGILL), code 1 (ILL_ILLOPC) , fault addr 地址-程序员宅基地

文章浏览阅读2.8k次。Android NDK thread 回收crash, signal 4 (SIGILL), code 1 (ILL_ILLOPC) , fault addr 地址在NDK c++ 使用pthread_create 创建线程,不管是可分离线程还是非可分离的线程,在回收的时候都有这个问题,ndk-stack 定位到的问题行发现一切正常,卡了好几天,网上查了好久,看到了一篇帖子https://w...

NOI题库答案 (1.7 字符串基础)(1-20)-程序员宅基地

文章浏览阅读1.9w次,点赞20次,收藏64次。 01:统计数字字符个数描述输入一行字符,统计出其中数字字符的个数。输入一行字符串,总长度不超过255。输出输出为1行,输出字符串里面数字字符的个数。样例输入Peking University is set up at 1898.样例输出4#include&lt;bits/stdc++.h&gt;using namespace std;in...

随便推点

Centos7设置1920x1080分辨率_centos7调整屏幕分辨率-程序员宅基地

文章浏览阅读9.5k次,点赞6次,收藏29次。Centos7设置分辨率_centos7调整屏幕分辨率

编译与链接的问题 gcc -fPIC -shared_symbol `g_hall_mode' can not be used when making a-程序员宅基地

文章浏览阅读2.8k次。地址无关代码,在64位下编译动态库的时候,经常会遇到下面的错误/usr/bin/ld: /tmp/ccQ1dkqh.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC提示说需要-fPIC编译,然后在链接_symbol `g_hall_mode' can not be used when making a shared object; recompile

SpringCloud之高可用的分布式配置中心(Spring Cloud Config)(七)-程序员宅基地

文章浏览阅读39次。当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:准备工作继续使用上一篇文章的工程,创建一个eureka-server工程,用作服务注册中心。在其pom.xml文件引入Eureka的起步依赖spring-cloud-starter-netflix- eureka-server,代码如下:<?xml version=...

c语言中.和->区别,c语言中"->"和"."的区别-程序员宅基地

文章浏览阅读3.5k次,点赞7次,收藏21次。对于c语言中"->"和"."的区别总结如下:1、A.B则A为对象或者结构体;2、A->B则A为指针,->是成员提取,A->B是提取A中的成员B,A只能是指向类、结构、联合的指针;3、(*a).b 等价于 a->b。"."一般情况下读作"的”; “->”一般读作"指向的结构体的"。也就是说在结构中,运算符->是运算符*和运算符.的结合4、“->”是指向..._->和.

ubuntu20.04安装ROS2 详细教程-程序员宅基地

文章浏览阅读2.1w次,点赞27次,收藏218次。ubuntu20.04安装ROS2_ubuntu20.04安装ros2

人工智能之华为云5G基站有AI,智能处理流量“潮汐”-程序员宅基地

文章浏览阅读1.9k次,点赞3次,收藏16次。一、5G 基站能否智能“省电”?① 能耗和能效随着中国 5G 基站部署规模的扩大,5G 基站能耗惊人的说法甚嚣尘上,众口铄金,5G 基站似乎坐实“电老虎”的尴尬地位。如下是一张某运营商的内部流出照片,从中可以看出,5G AAU 和 4G RRU 的满载功耗相差极为悬殊,不得不承认 5G 的能耗确实远高于 4G:在移动的节能技术白皮书中,也明确地写着:“2019 年初 5G 基站功耗约为 4G 基站的 3~4 倍,高功耗是运营商大规模部署 5G 的棘手问题”:联通也在其白皮书中写道:“5