Spring Boot的Gateway服务Session信息保存在Redis展示
1、添加依赖
xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
</dependencies>
2、配置Redis连接
yaml
spring:
redis:
host: localhost
port: 6379
3、创建过滤器,拦截所有请求并将session封装好存储到Redis中
java
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.UUID;
@Component
public class RedisSessionFilter implements GlobalFilter, Ordered {
private final RedisClient redisClient;
@Autowired
public RedisSessionFilter(RedisClient redisClient) {
this.redisClient = redisClient;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
Route route = (Route) exchange.getAttributes().get(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
String session_id = null;
HttpCookie cookie = request.getCookies().getFirst("session_id");
if (cookie != null) {
session_id = cookie.getValue();
}
if (session_id == null || !redisClient.connect().sync().exists(session_id)) {
session_id = UUID.randomUUID().toString();
}
RedisSession session = new RedisSession(session_id, redisClient);
exchange.getAttributes().put("session", session);
return chain.filter(exchange).then(Mono.fromRunnable(() -> session.save(redisClient)));
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
4、创建RedisSession类,封装Session数据
java
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
public class RedisSession {
private final String session_id;
private final StatefulRedisConnection<String, String> connection;
public RedisSession(String session_id, RedisClient redisClient) {
this.session_id = session_id;
this.connection = redisClient.connect();
}
public void save(RedisClient redisClient) {
connection.sync().set(session_id, "session data");
connection.close();
}
}
注:在实际应用中,需要根据需求修改RedisSession类的属性和方法。
5、Redis配置
在application.yaml文件中添加Redis相关的配置信息:
yaml
spring:
redis:
host: localhost
port: 6379
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
6、启动Gateway服务
启动Gateway服务后,就可以在访问各个微服务的过程中将Session信息保存在Redis中了。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhggcaae
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01