一、gateway配置
server:
port: 10081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: true
fetch-registry: true
spring:
application:
name: gateway
cloud:
gateway:
mvc:
routes:
- id: base
uri: http://localhost:8081
predicates:
- Path=/base/**
filters:
- StripPrefix=1
本文的http://localhost:8081直接访问的是index.html网页,当配置网关后,输入的网址为localhost:10081/base,网关通过predicates,找到相关uri,组成http://localhost:8081
/base/发送请求,这个链接是访问不到index.html的,这时就用到了filters: - StripPrefix=1,将请求中的第一个路径去掉,也就变成了http://localhost:8081,这样就能访问到index.html
下方链接介绍了404相关解决办法:
Spring Cloud Gateway 路由不生效显示 404 页面是怎么回事?应该怎么办? - 知乎
二、redis配置
1、在网关和相关模块下引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2、启动类添加
@EnableRedisHttpSession @Bean

3、application配置
spring:
application:
name: base
session:
redis:
repository-type: default
data:
redis:
host: localhost
port: 6379
4、测试
base1的test类创建session

base2的test类获取session并打印

启动注册中心regcenter、网关gateway、base1、base2
输入localhost:10081/base,

Cannot get Jedis connection---------忘开redis了,输入命令,redis-server,启动redis

成功
输入base2网址,点击test,后台就会打印sessionid。
![]()
redis配置完成。
SpringCloudGateway配置与Redis集成实例
文章详细描述了如何配置SpringCloudGateway进行路由映射,包括Eureka服务发现和路由规则设置,以及如何整合Redis以实现会话管理,解决了404问题。最后演示了Redis的启动和使用过程。
1万+





