栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud dubbo(dubbo springcloud选择)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud dubbo(dubbo springcloud选择)

环境配置

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 sessionRepository;

    public SessionServiceImpl(FindByIndexNameSessionRepository 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序列化解决方案

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775608.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号