网关搭建 GateWay
最早时Spring cloud 使用的是 Zuul ,后来Spring 自己研发了GateWay。
具体Zuul 和 GateWay的区别
网关的主要功能
1.限流(限制流量)。2. 重试(请求失败,重新请求)。3.跨域(前后端可以在不同的域中)。
4.路由(请求转发)。5.鉴权(登录校验,签名校验)等。
在pom.xml中导入spring-cloud-starter-gateway 网关包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
然后在 application.yaml 文件中配置文件
spring:
application:
name: GateWay
cloud:
gateway:
routes:#如果想设置多个路由可以将下面内容复制多份,然后指定新的项目
- id: System #指定一个路由名为 System 的项目
uri: http://127.0.0.1:9001 #设置URL,因为System 项目端口设置的是9001所以uri的端口也是9001
predicates:
- Path=/System/** #设置访问路径
** .properties ** 写法
spring.cloud.gateway.routes[0].id=System
spring.cloud.gateway.routes[0].uri=http://127.0.0.1:9001
spring.cloud.gateway.routes[0].predicates[0].Path=/System/**
指定 System 项目后 还需要将 System目录下的 application.yaml 重新设置添加一个固定路径
server:
port: 9001
servlet:
context-path: /System