如何搭建路由模块-gateway
- SpringCloud的网关组件可以用gateway或zuul,最早使用的是zuul,后面spring自己出了gateway
- 网关主要功能:限流(流量控制);重试(请求失败时重试,慎用);跨域(前后端不在同一个域);路由(转发请求);鉴权(登录校验,签名校验) 等…
搭建:
- 新建Maven子项目gateway
在gateway文件下的pom.xml引进组件
<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>
</dependencies>
- 路由转发
将外部请求转发到实际的业务模块进行处理。
在application.properties中添加路由转发。
目的:将9001地址对外隐藏,暴露9000地址,访问的是:localhost:9000/system/**
,实际处理是:localhost:9001/system/**
在配置文件中加server.servlet.context-path=/system
,那么以后要访问这个模块都要访问地址都要加上/system
成功!