Spring Cloud Gateway替代zuul作为API网关(一)
本文链接:https://blog.csdn.net/russle/article/details/80962066
本文非常简要介绍如何使用Spring Cloud Gateway 作为API 网关(不是使用zuul作为网关),关于Spring Cloud Gateway和zuul的性能比较本文不再赘述,基本可以肯定Spring Cloud Finchley版本的gateway比zuul 1.x系列的性能和功能整体要好。
特别提醒:Spring Cloud Finchley版本中,即使你引入了spring-cloud-starter-netflix-zuul,也不是2.0版本的zuul
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
我们的介绍分为3个部分, 第一,pom文件,第二项目的基本架构,第三源码和截图。
第一,pom文件
因为使用Eureka作为服务注册和发现,因此在pom中引入了eureka,各位可根据自己的实际情况修改。
<?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.yq</groupId>
<artifactId>GatewayDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.33</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二,项目结构
总共有3个项目,
第一个项目是eureka 注册中心,非常简单,基本就一个Application类。 端口7700
第二个项目是User service,也非常简单提供两个rest api,为了简略不连接数据,直接在内存中生成一组数据。端口6601
第三个项目就是我们的网关。端口6604
目前项目中集成websocket服务配置,本文暂不介绍可直接忽略。
第三,项目代码和运行截图
网关的主代码
package com.yq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@SpringCloudApplication
public class APIGatewayApplication {
private static final Logger logger = LoggerFactory.getLogger(APIGatewayApplication.class);
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/baidu")
.uri("http://baidu.com:80/")
)
.route("websocket_route", r -> r.path("/apitopic1/**")
.uri("ws://127.0.0.1:6605"))
.route(r -> r.path("/userapi3/**")
.filters(f -> f.addResponseHeader("X-AnotherHeader", "testapi3"))
.uri("lb://user-service/")
)
.build();
}
public static void main(String[] args) {
SpringApplication.run(APIGatewayApplication.class, args);
logger.info(" Start APIGatewayApplication Done");
}
}
网关的配置文件application.yml
server:
port: 6604
#服务名
spring:
application:
name: gateway-service
cloud:
gateway:
filter:
remove-non-proxy-headers:
headers:
- dummy
routes:
- id: apiuser
# 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
uri: lb://user-service
predicates:
# 重点!转发该路径!,/userapi/**,
- Path=/userapi/**
# http://localhost:6601/userapi/user/users/2, 必须加上StripPrefix=1,否则访问服务时会带上userapi
#而不是我们期望的去掉userapi,只保留**部分
filters:
- StripPrefix=1
- id: api2user
uri: lb://user-service
predicates:
- Path=/userapi2/**
filters:
- StripPrefix=1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7700/eureka/
我们简要分析分析一下配置文件
id: apiuser
# 重点!/info必须使用http进行转发,lb代表从注册中心获取服务
uri: lb://user-service
predicates:
# 重点!转发该路径!,/userapi/,
- Path=/userapi/
# http://localhost:6601/userapi/user/users/2, 必须加上StripPrefix=1,否则访问服务时会带上userapi
#而不是我们期望的去掉userapi,只保留**部分
filters:
- StripPrefix=1
配置了一个路由apiuser, 当路径( - Path=/userapi/**),就转发到服务(lb://user-service),同时把路径中的userapi这部分去掉(- StripPrefix=1)。
运行效果图
直接访问User service
http://localhost:6601/user/users/2
通过网关访问user service
http://localhost:6604/userapi/user/users/2
参考文档:
1, http://cloud.spring.io/spring-cloud-static/Finchley/single/spring-cloud.html#_spring_cloud_gateway
2, https://github.com/spring-cloud-samples/spring-cloud-gateway-sample
————————————————
版权声明:本文为CSDN博主「russle」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/russle/article/details/80962066
Spring Cloud Gateway替代zuul作为API网关(一)相关推荐
- Spring Cloud入门教程 - Zuul实现API网关和请求过滤
简介 Zuul是Spring Cloud提供的api网关和过滤组件,它提供如下功能: 认证 过滤 压力测试 Canary测试 动态路由 服务迁移 负载均衡 安全 静态请求处理 动态流量管理 在本教程中 ...
- spring cloud 学习(6) - zuul 微服务网关
微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服务前端再封装一层,类似下 ...
- 青柠开车Spring Cloud(六) —— Spring Cloud Gateway与zuul使用对比
青柠开车Spring cloud(一) -- 生态系统以及在企业项目中的基础架构图 (1-7),有时间可以看看 项目源码github地址 补充 Gateway简介 快速入门 Gateway 项 ...
- Spring Cloud Gateway(十):网关过滤器工厂 GatewayFilterFactory
本文基于 spring cloud gateway 2.0.1 1.GatewayFilterFactory 简介 路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应. 路径过滤器的范 ...
- Alibaba Sentinel对接Spring Cloud Gateway关于不显示API管理及请求链路的坑附带解决方案
在调试Sentinel对接Scg网关的时候,出现了一个比较坑的现象,就是网关菜单,不显示请求链路和API管理的菜单项,这个问题主要是由于,Sentinel的dashboard前端sidebar.js的 ...
- Spring Cloud —— Gateway 服务网关
导航 一.什么是服务网关 二.业界常见网关组件 三.Spring Cloud Gateway 四.Gateway 快速入门 4.1 创建 gateway 服务 4.2 添加 gateway 依赖和 n ...
- Spring Cloud Gateway 入门
认识 Spring Cloud Gateway Spring Cloud Gateway 是一款基于 Spring 5,Project Reactor 以及 Spring Boot 2 构建的 API ...
- 网关Spring Cloud Gateway科普
点击上方"朱小厮的博客",选择"设为星标" 后台回复"加群"获取公众号专属群聊入口 欢迎跳转到本文的原文链接:https://honeypp ...
- SpringCloud-07-新一代路由网关Spring Cloud Gateway
服务网关Gateway 1. 概述简介 1. SpringCloud Gateway是什么? 服务网关还可以用Zuul网关,但是Zuul网关由于一些维护问题,所以这里我们学习Gateway网关,Spr ...
- springcloud(十一):服务网关 Spring Cloud GateWay 入门
Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...
最新文章
- SCI-HUB客户端(文献神器V6.0)——下载文献如此简单
- linux回到初始的命令,Linux基础总结简单常用命令
- 静态路由实验 +http+dns_华为静态路由配置实验
- 【Android】Android 8种对话框(Dialog)
- BeetleX.Redis基础操作和对象格式
- isdigit函数python_Python判断字符串是否为数字的方法isdecimal 、isdigit、isnumeric及坑...
- 以太坊PoA共识引擎算法介绍(1)
- 理解First-Class Functions
- 学号 20165329 《Java程序设计》第4周学习总结
- Java多线程_生产者消费者模式1
- Apache Struts 2的命名空间实战
- wps中将文档输出为pdf_罗永浩点赞WPS超级会员功能:转化PDF是神操作
- 五、瞰景Smart3D空三解算
- 中职计算机教师发言范文话,中职班主任发言稿范文(精选4篇)
- mysql网站倒计时代码_最简单的一个网页倒计时代码 时间到期后会显示出提醒内容 收藏版...
- Python requests timeout 分析
- Keras学习教程七
- At least one JAR was scanned for TLDs yet contained no TLDs.
- css module模组化
- 人工神经网络分析方法,人工神经网络算法步骤
热门文章
- Oracle 创建表空间借鉴 保留,占版权留言告知
- handler机制和异步更新UI页面
- linux下多线程断点下载工具-axel
- 制作ext4 img的两种方式
- linux内核之进程管理详解
- android组件化数据生命周期,Android组件化开发实践(七):开发常见问题及解决方案...
- wordpress获取home_wordpress各种获取路径和URl地址的函数总结
- java自学经历分享
- Camera 初始化(Preview) 二(HAL3的流程)
- 内核抢占机制(preempt)