API 主流网关有NGINX、ZUUL、Spring Cloud Gateway、Linkerd等;Spring Cloud Gateway构建于 Spring 5+,基于 Spring Boot 2.x 响应式的、非阻塞式的 API。同时,它支持 websockets,和 Spring 框架紧密集成,用来代替服务网关Zuul,开发体验相对来讲十分不错。html
Spring Cloud Gateway 是 Spring Cloud 微服务平台的一个子项目,属于 Spring 开源社区,依赖名叫:spring-cloud-starter-gateway。 Zuul 是 Netflix 公司的开源项目,Spring Cloud 在 Netflix 项目中也已经集成了 Zuul,依赖名叫:spring-cloud-starter-netflix-zuul。git
API 网关出现的缘由是微服务架构的出现,不一样的微服务通常会有不一样的网络地址,而外部客户端可能须要调用多个服务的接口才能完成一个业务需求,若是让客户端直接与各个微服务通讯,会有如下的问题:github
以上这些问题能够借助 API 网关解决。API 网关是介于客户端和服务器端之间的中间层,全部的外部请求都会先通过 API 网关这一层。也就是说,API 的实现方面更多的考虑业务逻辑,而安全、性能、监控能够交由 API 网关来作,这样既提升业务灵活性又不缺安全性。web
Spring Cloud Gateway 示例源码spring
Spring Cloud Gateway 网关路由有两种配置方式:后端
这两种方式是等价的,建议使用 yml 方式进配置。跨域
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--Spring Cloud Gateway 是使用 netty+webflux 实现所以不须要再引入 web 模块--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
server: port: 8080 spring: cloud: gateway: routes: - id: easy_route # 咱们自定义的路由 ID,保持惟一 uri: https://github.com # 目标服务地址 predicates: # 路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 - Path=/smltq/spring-boot-demo
上面这段配置的意思是,配置了一个 id 为 easy_route 的路由规则,当访问地址 http://localhost:8080/smltq/spring-boot-demo时会自动转发到地址:https://github.com/smltq/spring-boot-demo。浏览器
在浏览器访问进行测试,当咱们访问 http://localhost:8080/smltq/spring-boot-demo 时会展现以下页面:安全
原文出处:https://www.cnblogs.com/tqlin/p/11377869.html服务器