替换redis操作类
This commit is contained in:
parent
eaaf2581ab
commit
b04c65a949
56
book-api/src/main/java/com/imooc/config/RedisConfig.java
Normal file
56
book-api/src/main/java/com/imooc/config/RedisConfig.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package com.imooc.config;
|
||||||
|
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
|
import org.redisson.spring.data.connection.RedissonConnectionFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(RedissonClient.class)
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
private static final RedisSerializer<String> STRING_SERIALIZER = new StringRedisSerializer();
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
RedissonConnectionFactory redisConnectionFactory(RedissonClient redisson) {
|
||||||
|
log.info("------------->>>配置自定义RedissonConnectionFactory");
|
||||||
|
return new RedissonConnectionFactory(redisson);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
log.info("------------->>>配置自定义RedisTemplate");
|
||||||
|
return buildRedisTemplate(redisConnectionFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置redisTemplate
|
||||||
|
*
|
||||||
|
* @param redisConnectionFactory
|
||||||
|
* @param enableTransactionSupport
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private RedisTemplate<String, Object> buildRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
// 配置redisTemplate
|
||||||
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||||
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
// key序列化
|
||||||
|
redisTemplate.setKeySerializer(STRING_SERIALIZER);
|
||||||
|
// value序列化
|
||||||
|
redisTemplate.setValueSerializer(STRING_SERIALIZER);
|
||||||
|
// Hash key序列化
|
||||||
|
redisTemplate.setHashKeySerializer(STRING_SERIALIZER);
|
||||||
|
// Hash value序列化
|
||||||
|
redisTemplate.setHashValueSerializer(STRING_SERIALIZER);
|
||||||
|
// 初始化RedisTemplate
|
||||||
|
redisTemplate.afterPropertiesSet();
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
}
|
@ -18,17 +18,19 @@ spring:
|
|||||||
max-lifetime: 540000 # 连接池的最大生命时长(毫秒),超时则会被释放(retired)
|
max-lifetime: 540000 # 连接池的最大生命时长(毫秒),超时则会被释放(retired)
|
||||||
connection-test-query: SELECT 1
|
connection-test-query: SELECT 1
|
||||||
redis:
|
redis:
|
||||||
host: 82.156.121.2
|
# host: 82.156.121.2
|
||||||
port: 26379
|
# port: 26379
|
||||||
password: e4ea0caebfd2
|
# password: e4ea0caebfd2
|
||||||
database: 1 # 使用的数据库编号
|
# database: 1 # 使用的数据库编号
|
||||||
jedis:
|
# jedis:
|
||||||
pool:
|
# pool:
|
||||||
max-idle: 50 # 最大空闲连接
|
# max-idle: 50 # 最大空闲连接
|
||||||
max-active: 200 # 连接池最大连接数
|
# max-active: 200 # 连接池最大连接数
|
||||||
max-wait: 5000 # 连接池最大阻塞等待时间, -1表示没有限制
|
# max-wait: 5000 # 连接池最大阻塞等待时间, -1表示没有限制
|
||||||
min-idle: 4 # 最小空闲连接
|
# min-idle: 4 # 最小空闲连接
|
||||||
timeout: 50000
|
# timeout: 50000
|
||||||
|
redisson:
|
||||||
|
file: classpath:redisson.yml
|
||||||
data:
|
data:
|
||||||
mongodb:
|
mongodb:
|
||||||
# uri: mongodb://root:root@192.168.1.202:27017
|
# uri: mongodb://root:root@192.168.1.202:27017
|
||||||
|
19
book-api/src/main/resources/redisson.yml
Normal file
19
book-api/src/main/resources/redisson.yml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
singleServerConfig:
|
||||||
|
address: redis://82.156.121.2:26379
|
||||||
|
password: e4ea0caebfd2
|
||||||
|
database: 1
|
||||||
|
idleConnectionTimeout: 10000
|
||||||
|
connectTimeout: 10000
|
||||||
|
timeout: 3000
|
||||||
|
retryAttempts: 3
|
||||||
|
retryInterval: 1500
|
||||||
|
clientName: null
|
||||||
|
subscriptionConnectionMinimumIdleSize: 1
|
||||||
|
subscriptionConnectionPoolSize: 10
|
||||||
|
subscriptionsPerConnection: 5
|
||||||
|
connectionMinimumIdleSize: 4
|
||||||
|
connectionPoolSize: 20
|
||||||
|
threads: 0
|
||||||
|
nettyThreads: 0
|
||||||
|
codec: !<org.redisson.codec.JsonJacksonCodec> {}
|
||||||
|
transportMode: NIO
|
@ -41,10 +41,20 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 引入 redis 依赖 -->
|
<!-- 引入 redis 依赖 -->
|
||||||
<dependency>
|
<!--<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
</dependency>
|
</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.redisson</groupId>
|
||||||
|
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||||
|
<version>3.16.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-pool2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 引入 RabbitMQ 依赖 -->
|
<!-- 引入 RabbitMQ 依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -22,8 +22,10 @@ import com.imooc.vo.UsersVO;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.n3r.idworker.Sid;
|
import org.n3r.idworker.Sid;
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -55,8 +57,11 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserMemberMapper userMemberMapper;
|
private UserMemberMapper userMemberMapper;
|
||||||
|
|
||||||
@Autowired
|
//@Autowired
|
||||||
public RedisOperator redis;
|
//public RedisOperator redis;
|
||||||
|
|
||||||
|
@Resource(name = "redisTemplate")
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Sid sid;
|
private Sid sid;
|
||||||
@ -268,7 +273,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
return Optional.ofNullable(userMemberMapper.getUserByMemberId(memberId))//
|
return Optional.ofNullable(userMemberMapper.getUserByMemberId(memberId))//
|
||||||
.map(user -> {
|
.map(user -> {
|
||||||
String uToken = UUID.randomUUID().toString();
|
String uToken = UUID.randomUUID().toString();
|
||||||
redis.set(REDIS_USER_TOKEN + ":" + user.getId(), uToken);
|
//redis.set(REDIS_USER_TOKEN + ":" + user.getId(), uToken);
|
||||||
|
redisTemplate.opsForValue().set(REDIS_USER_TOKEN + ":" + user.getId(), uToken);
|
||||||
// 返回给前端
|
// 返回给前端
|
||||||
UsersVO usersVO = new UsersVO();
|
UsersVO usersVO = new UsersVO();
|
||||||
BeanUtils.copyProperties(user, usersVO);
|
BeanUtils.copyProperties(user, usersVO);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user