网关作为流量的入口,常用功能包括路由转发、权限校验、限流控制等。而 springcloud gateway作为 SpringCloud 官方推出的第二代网关框架,取代了 Zuul 网关。
网关提供 API 全托管服务,丰富的 API 管理功能,辅助企业管理大规模的 API,以降低管理成本和安全风险,包括协议适配、协议转发、安全策略、防刷、流量、监控日志等功能。
Spring Cloud Gateway 旨在提供一种简单而有效的方式来对 API 进行路由,并为他们提供切面,例如:安全性,监控/指标 和弹性等
Spring Cloud Gateway 特性:
- 基于 Spring framework 5、Project Reactor 和 Spring Boot 2.0
- 能够匹配任何请求属性的路由。
- 谓词和过滤器特定于路由。
- 断路器集成。
- Spring Cloud DiscoveryClient 集成
- 易于编写谓词和过滤器
- 请求速率限制
- 路径重写
详情请参考官网添加链接描述
3.2注册到nacos4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.atguigu.gulimall gulimall-gateway 0.0.1-SNAPSHOT gulimall-gateway 谷粒商城-API网关 1.8 Hoxton.SR7 com.atguigu.gulimall gulimall-common 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-gateway org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
启动类上加**@EnableDiscoveryClient**,编辑配置文件application.properties
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.application.name=gulimall-gateway server.port=883.3.在nacos创建项目命名空间与配置
编辑bootstrap.properties
#bootstrap.properties优先于application.properties加载 #服务名 spring.application.name=gulimall-gateway #注册中心地址 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 #配置命名空间(默认是public空间,只能写唯一id不能写命名空间名称) spring.cloud.nacos.config.namespace=e0d53e15-022c-466b-812f-c9e580552da04.启动项目错误分析与解决
原因:引入gulimall-common有mybatis-plus需要数据源相关信息
解决方案:在启动类上排除数据源



