连接redis
package com.firefly.performance.core.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Slf4j
@Configuration
public class RedisConfig {
// Auth Redis 配置信息
@Value("${spring.redis.auth.database}")
private Integer authDatabase;
@Value("${spring.redis.auth.host}")
private String authHost;
@Value("${spring.redis.auth.port}")
private Integer authPort;
@Value("${spring.redis.auth.password}")
private String authPassword;
// MPass Redis 配置信息
@Value("${spring.redis.mpass.database}")
private Integer mpassDatabase;
@Value("${spring.redis.mpass.host:}")
private String mpassHost;
@Value("${spring.redis.mpass.port:}")
private Integer mpassPort;
@Value("${spring.redis.mpass.password:}")
private String mpassPassword;
/**
* redisStandaloneConfiguration 单机版配置
*
* @param host
* @param port
* @param password
* @param database
* @return
*/
public RedisTemplate<String, String> build(String host, int port, String password, int database) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port);
if (StringUtils.isNotBlank(password)) {
redisStandaloneConfiguration.setPassword(password);
}
redisStandaloneConfiguration.setDatabase(database);
LettucePoolingClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(5000l))
.poolConfig(new GenericObjectPoolConfig())
.build();
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
//必须初始化实例
connectionFactory.afterPropertiesSet();
//如果要使pool参数生效,一定要关闭shareNativeConnection
//lettuceConnectionFactory.setShareNativeConnection(false);
RedisTemplate<String, String> commonRedisTemplate = new StringRedisTemplate();
commonRedisTemplate.setConnectionFactory(connectionFactory);
// key、hash的key 采用 String序列化方式 【可自行配置】
commonRedisTemplate.setKeySerializer(new StringRedisSerializer());
commonRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
// value、hash的value 采用 String 序列化方式
commonRedisTemplate.setValueSerializer(new StringRedisSerializer());
commonRedisTemplate.setHashValueSerializer(new StringRedisSerializer());
commonRedisTemplate.afterPropertiesSet();
return commonRedisTemplate;
}
@Bean(name = "redisAuthTemplate")
public RedisTemplate<String, String> redisAuthTemplate() {
return build(authHost, authPort, authPassword, authDatabase);
}
@Bean(name = "redisMpassTemplate")
public RedisTemplate<String, String> redisMpassTemplate() {
return build(mpassHost, mpassPort, mpassPassword, mpassDatabase);
}
}
调用
package com.firefly.performance.core.common;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Component;
@Component
public class FFTokenUtil {
private static final String TOKEN_PREFIX = "pig_oauth:access:";
private static final String BEARER_PREFIX = "Bearer ";
private static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();
private static final JdkSerializationRedisSerializer OBJECT_SERIALIZER = new JdkSerializationRedisSerializer();
public static final String APP_ACCESS_TOKEN = "access_token_";
@Autowired
@Qualifier("redisAuthTemplate")
private RedisTemplate<String, String> redisAuthTemplate;
@Autowired
@Qualifier("redisMpassTemplate")
private RedisTemplate<String, String> redisMpassTemplate;
/**
* 通过token去redis取userId
*/
public Integer getPcUserIdByToken(String token) {
if (StringUtils.isEmpty(token)){
return null;
}
byte[] key = STRING_SERIALIZER.serialize(TOKEN_PREFIX + token.replaceFirst(BEARER_PREFIX,""));
byte[] bytes = redisAuthTemplate.execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key);
}
});
OAuth2AccessToken accessToken = (OAuth2AccessToken) OBJECT_SERIALIZER.deserialize(bytes);
return accessToken != null ? (Integer) accessToken.getAdditionalInformation().get("userId") : null;
}
public String getAppUserIdByToken(String token) {
if (StringUtils.isEmpty(token)){
return null;
}
return redisMpassTemplate.opsForValue().get(APP_ACCESS_TOKEN + token.replaceFirst(BEARER_PREFIX,""));
}
}