解决方案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;
@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 findByIndexNameAndIndexValue(String indexName, String indexValue) {
Map sessionMap = sessionRepository.findByIndexNameAndIndexValue(indexName, indexValue);
Map 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 getAttribute(String id, String attributeName) {
Session session = sessionRepository.findById(id);
return session.getAttribute(attributeName);
}
@Override
public Set 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; public class RpcIndexedSessionRepository implements FindByIndexNameSessionRepository参考文章{ private final ISessionService iSessionService; public RpcIndexedSessionRepository(ISessionService iSessionService) { this.iSessionService = iSessionService; } @Override public Map 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); } 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 getAttribute(String attributeName) { return RpcIndexedSessionRepository.this.iSessionService.getAttribute(this.id, attributeName); } @Override public Set 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); } } }
Dubbo——Dubbo协议整合Jackson序列化解决方案



