Spring Cloud——基于Dubbo的分布式Session解决方案
Posted Starzkg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud——基于Dubbo的分布式Session解决方案相关的知识,希望对你有一定的参考价值。
环境配置
spring boot 2.6.3
spring cloud 2021.0.1
spring cloud alibaba 2021.0.1.0
nacos server 2.0.4
dubbo 2.7.15
解决方案
源代码:https://gitee.com/myzstu/auth
package club.zstuca.myzstu.auth.service.impl;
import club.zstuca.myzstu.session.service.ISessionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author shentuzhigang
* @date 2022/3/17 22:59
*/
@Service
@DubboService(application = "auth", version = "1.0.0", protocol = "jackson")
public class SessionServiceImpl implements ISessionService
private final FindByIndexNameSessionRepository<? extends Session> sessionRepository;
public SessionServiceImpl(FindByIndexNameSessionRepository<? extends Session> sessionRepository)
this.sessionRepository = sessionRepository;
@Override
public String createSession()
Session session = sessionRepository.createSession();
return session.getId();
@Override
public void save(String session)
@Override
public String findById(String id)
Session session = sessionRepository.findById(id);
if(session == null)
return null;
return session.getId();
@Override
public void deleteById(String id)
sessionRepository.deleteById(id);
@Override
public Map<String, String> findByIndexNameAndIndexValue(String indexName, String indexValue)
Map<String, ? extends Session> sessionMap = sessionRepository.findByIndexNameAndIndexValue(indexName, indexValue);
Map<String, String> map = new HashMap<>();
sessionMap.forEach((key, value) ->
map.put(key, value.getId());
);
return map;
@Override
public String changeSessionId(String id)
Session session = sessionRepository.findById(id);
return session.changeSessionId();
@Override
public <T> T getAttribute(String id, String attributeName)
Session session = sessionRepository.findById(id);
return session.getAttribute(attributeName);
@Override
public Set<String> getAttributeNames(String id)
Session session = sessionRepository.findById(id);
return session.getAttributeNames();
@Override
public void setAttribute(String id, String attributeName, Object attributeValue)
Session session = sessionRepository.findById(id);
session.setAttribute(attributeName, attributeValue);
@Override
public void removeAttribute(String id, String attributeName)
Session session = sessionRepository.findById(id);
session.removeAttribute(attributeName);
@Override
public Instant getCreationTime(String id)
Session session = sessionRepository.findById(id);
return session.getCreationTime();
@Override
public void setLastAccessedTime(String id, Instant lastAccessedTime)
Session session = sessionRepository.findById(id);
session.setLastAccessedTime(lastAccessedTime);
@Override
public Instant getLastAccessedTime(String id)
Session session = sessionRepository.findById(id);
return session.getLastAccessedTime();
@Override
public void setMaxInactiveInterval(String id, Duration interval)
Session session = sessionRepository.findById(id);
session.setMaxInactiveInterval(interval);
@Override
public Duration getMaxInactiveInterval(String id)
Session session = sessionRepository.findById(id);
return session.getMaxInactiveInterval();
@Override
public boolean isExpired(String id)
Session session = sessionRepository.findById(id);
return session.isExpired();
package club.zstuca.myzstu.session;
import club.zstuca.myzstu.session.service.ISessionService;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Set;
/**
* @author shentuzhigang
* @date 2022/3/19 13:00
*/
public class RpcIndexedSessionRepository implements FindByIndexNameSessionRepository<RpcIndexedSessionRepository.RpcSession>
private final ISessionService iSessionService;
public RpcIndexedSessionRepository(ISessionService iSessionService)
this.iSessionService = iSessionService;
@Override
public Map<String, RpcSession> findByIndexNameAndIndexValue(String indexName, String indexValue)
return findByIndexNameAndIndexValue(indexName, indexValue);
@Override
public RpcSession createSession()
return new RpcSession(iSessionService.createSession());
@Override
public void save(RpcSession session)
iSessionService.save(session.getId());
@Override
public RpcSession findById(String id)
String sessionId = iSessionService.findById(id);
if (sessionId == null)
return null;
return new RpcSession(sessionId);
@Override
public void deleteById(String id)
iSessionService.deleteById(id);
/**
* An internal @link Session implementation used by this @link SessionRepository.
*/
public final class RpcSession implements Session, Serializable
private static final long serialVersionUID = 1L;
private String id;
public RpcSession(String id)
this.id = id;
@Override
public String getId()
return this.id;
@Override
public String changeSessionId()
return RpcIndexedSessionRepository.this.iSessionService.changeSessionId(this.id);
@Override
public <T> T getAttribute(String attributeName)
return RpcIndexedSessionRepository.this.iSessionService.getAttribute(this.id, attributeName);
@Override
public Set<String> getAttributeNames()
return RpcIndexedSessionRepository.this.iSessionService.getAttributeNames(this.id);
@Override
public void setAttribute(String attributeName, Object attributeValue)
RpcIndexedSessionRepository.this.iSessionService.setAttribute(this.id, attributeName, attributeValue);
@Override
public void removeAttribute(String attributeName)
RpcIndexedSessionRepository.this.iSessionService.removeAttribute(this.id, attributeName);
@Override
public Instant getCreationTime()
return RpcIndexedSessionRepository.this.iSessionService.getCreationTime(this.id);
@Override
public void setLastAccessedTime(Instant lastAccessedTime)
RpcIndexedSessionRepository.this.iSessionService.setLastAccessedTime(this.id, lastAccessedTime);
@Override
public Instant getLastAccessedTime()
return RpcIndexedSessionRepository.this.iSessionService.getLastAccessedTime(this.id);
@Override
public void setMaxInactiveInterval(Duration interval)
RpcIndexedSessionRepository.this.iSessionService.setMaxInactiveInterval(this.id, interval);
@Override
public Duration getMaxInactiveInterval()
return RpcIndexedSessionRepository.this.iSessionService.getMaxInactiveInterval(this.id);
@Override
public boolean isExpired()
return RpcIndexedSessionRepository.this.iSessionService.isExpired(this.id);
参考文章
以上是关于Spring Cloud——基于Dubbo的分布式Session解决方案的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud——基于Dubbo的分布式Session解决方案