使用spring会话redis时性能不佳

o0lyfsai  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(534)

我使用springboot+springsessionredis来存储http会话。
pom.xml文件:

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

令人惊讶的是,与jdbc会话相比,redis安装程序的性能很差。我已经检查了调用外部系统更新/读取会话的日志/登录请求:
jdbc公司:

16:37:53.821 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [UPDATE SPRING_SESSION SET SESSION_ID = ?, LAST_ACCESS_TIME = ?, MAX_INACTIVE_INTERVAL = ?, EXPIRY_TIME = ?, PRINCIPAL_NAME = ? WHERE PRIMARY_ID = ?]
16:37:53.862 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [INSERT INTO SPRING_SESSION_ATTRIBUTES(SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) SELECT PRIMARY_ID, ?, ? FROM SPRING_SESSION WHERE SESSION_ID = ?]
16:37:53.862 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [INSERT INTO SPRING_SESSION_ATTRIBUTES(SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) SELECT PRIMARY_ID, ?, ? FROM SPRING_SESSION WHERE SESSION_ID = ?]
16:37:53.993 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT S.PRIMARY_ID, S.SESSION_ID, S.CREATION_TIME, S.LAST_ACCESS_TIME, S.MAX_INACTIVE_INTERVAL, SA.ATTRIBUTE_NAME, SA.ATTRIBUTE_BYTES FROM SPRING_SESSION S LEFT OUTER JOIN SPRING_SESSION_ATTRIBUTES SA ON S.PRIMARY_ID = SA.SESSION_PRIMARY_ID WHERE S.SESSION_ID = ?]
16:37:53.993 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT S.PRIMARY_ID, S.SESSION_ID, S.CREATION_TIME, S.LAST_ACCESS_TIME, S.MAX_INACTIVE_INTERVAL, SA.ATTRIBUTE_NAME, SA.ATTRIBUTE_BYTES FROM SPRING_SESSION S LEFT OUTER JOIN SPRING_SESSION_ATTRIBUTES SA ON S.PRIMARY_ID = SA.SESSION_PRIMARY_ID WHERE S.SESSION_ID = ?]
16:37:54.047 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT S.PRIMARY_ID, S.SESSION_ID, S.CREATION_TIME, S.LAST_ACCESS_TIME, S.MAX_INACTIVE_INTERVAL, SA.ATTRIBUTE_NAME, SA.ATTRIBUTE_BYTES FROM SPRING_SESSION S LEFT OUTER JOIN SPRING_SESSION_ATTRIBUTES SA ON S.PRIMARY_ID = SA.SESSION_PRIMARY_ID WHERE S.SESSION_ID = ?]
16:37:54.047 DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT S.PRIMARY_ID, S.SESSION_ID, S.CREATION_TIME, S.LAST_ACCESS_TIME, S.MAX_INACTIVE_INTERVAL, SA.ATTRIBUTE_NAME, SA.ATTRIBUTE_BYTES FROM SPRING_SESSION S LEFT OUTER JOIN SPRING_SESSION_ATTRIBUTES SA ON S.PRIMARY_ID = SA.SESSION_PRIMARY_ID WHERE S.SESSION_ID = ?]

redis公司:

15:58:02.039 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=HMSET, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.161 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=SADD, output=IntegerOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.193 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=SADD, output=IntegerOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.226 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PEXPIRE, output=BooleanOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.246 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=APPEND, output=IntegerOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.277 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PEXPIRE, output=BooleanOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.309 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PEXPIRE, output=BooleanOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.346 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PUBLISH, output=IntegerOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.362 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=HMSET, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.487 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=SADD, output=IntegerOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.565 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PEXPIRE, output=BooleanOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.596 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=APPEND, output=IntegerOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.627 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PEXPIRE, output=BooleanOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
15:58:02.647 DEBUG io.lettuce.core.RedisChannelHandler - dispatching command AsyncCommand [type=PEXPIRE, output=BooleanOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]

因此,使用jdbc时,我们有7次对数据库的调用,耗时约220毫秒,而使用redis时,有14次调用耗时约600毫秒。数据库和redis都托管在extarnal服务器上,应用程序运行在localhost上。
问题是:是否预期redis的性能不如jdbc?或者有没有办法限制对redis的调用数量,或者至少在同一个连接中运行它们,而不是每次调用都打开一个新的连接?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题