背景
业务上有生成凭证编码的需求(根据一定的业务规则,生成业务上的唯一标识,一般都是“业务标记+流水号”的形式)。之前设计时考虑到数据量不大,用户也不多,就在数据库里搞了一张流水号的表,然后用乐观锁,保证每次读出来+1后再写回数据库。万万没想到,每秒30的并发,就有好多失败,认命之后只好屈服于redis。这个也是个经验教训,实现方式不能凭感觉猜,还是要有更多的考虑好一点。
实现
思路
核心就是redis 的 INCR 命令。然后redis没有做持久化的话,会存在数据丢失的情况。怎么办呢,做法是根据 业务标记 去业务数据表中查最新的一条,截取流水号+1,set到Redis中,作为key新的起始值。
代码实现
/**
* 生成流水号
* @param uniqueKey 唯一key;
* businessCode = uniqueKey + 流水号
* @return
*/
public Long generatorSerialNumber(String uniqueKey) {
//1. redis中已存在, +1返回
boolean existInRedis = commonRedisCache.isExist(uniqueKey);
if (existInRedis) {
return commonRedisCache.increment(uniqueKey, 1L);
}
//2. redis中不存在
//2.1 获取当前DB里最大流水号(去业务数据表查最新的编码(根据id倒序取第一个))
long curSerialNumber = 0L;
/* 模拟DB中查出的业务编码 */
String businessCode = "user002";
if (StringUtils.isNotEmpty(businessCode)) {
curSerialNumber = Long.parseLong(businessCode.replace("user", ""));
}
//2.2 将当前流水码+1, set到redis中并返回
commonRedisCache.set(uniqueKey, curSerialNumber + 1, 24 * 60 * 60 * 1000L);
return curSerialNumber + 1;
}
import org.springframework.stereotype.Service;
/**
* redis 操作通用类
*
* @author 8102
* @date 2018/7/5
*/
@Service
public class CommonRedisCache extends RedisBaseServiceImpl<String, String, Object> {
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* redis 基础服务实现类
*
* @author 8102
* @date 2018/6/19
*/
@Component
@Getter
@SuppressWarnings("unchecked")
@Slf4j
public class RedisBaseServiceImpl<K, HK, V> implements RedisBaseService<K, HK, V> {
/**
* 主键key
*/
protected K key;
@Autowired
private RedisTemplate redisTemplate;
@Override
public V get(K key) {
if (key != null) {
log.debug("redis get value key : {}", key);
return (V) redisTemplate.opsForValue().get(key);
} else {
log.warn("redis get value key can't be null");
return null;
}
}
@Override
public void set(K key, V value, Long expireTime) {
if (key != null) {
log.debug("redis set for key:{} | value:{} ", key, value);
redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
} else {
log.warn("redis set key can't be null");
}
}
@Override
public boolean isExist() {
if (getKey() == null) {
log.warn("redis isExist key can't be null");
return false;
}
return redisTemplate.hasKey(getKey());
}
@Override
public boolean isExist(K key) {
if (key == null) {
log.warn("redis isExist key can't be null");
return false;
}
return redisTemplate.hasKey(key);
}
@Override
public void delete() {
if (getKey() == null) {
log.warn("redis delete key can't be null");
} else {
redisTemplate.delete(getKey());
}
}
@Override
public void delete(K key) {
log.info("删除key开始:{}", JSON.toJSONString(key));
if (key == null) {
log.warn("redis delete key can't be null");
} else {
byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
RedisConnection connection = null;
try {
connection = redisTemplate.getConnectionFactory().getConnection();
connection.del(keyByte);
} catch (Exception e) {
log.error("redis delete key error:", e);
} finally{
if(connection != null){
connection.close();
}
}
}
log.info("删除key成功:{}", JSON.toJSONString(key));
}
@Override
public void expire(Long times) {
if (getKey() == null) {
log.warn("redis expire key can't be null");
} else {
log.debug("redis expire key:{} times:{}", getKey(), times);
redisTemplate.expire(getKey(), times, TimeUnit.SECONDS);
}
}
@Override
public Long increment(K key, Long value) {
if (key == null) {
log.warn("redis increment key can't be null");
return null;
} else {
log.debug("redis increment key:{} for value:{} ", key, value);
return redisTemplate.opsForValue().increment(key, value);
}
}
@Override
public boolean setNx(K key, V value) {
boolean result = false;
log.info("redis setNx 开始 key:{} for value:{} ", key, value);
if (key != null && value != null) {
log.debug("redis setNx key:{} for value:{} ", key, value);
byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
byte[] valueByte = redisTemplate.getValueSerializer().serialize(value);
RedisConnection connection = null;
try {
connection = redisTemplate.getConnectionFactory().getConnection();
result = connection.setNX(keyByte, valueByte);
} catch (Exception e) {
log.error("redis setNx error:", e);
} finally{
if(connection != null){
connection.close();
}
}
} else {
log.warn("there are some problems to redis setNx key:{} for value:{} ", key, value);
}
log.info("redis setNx 结束 key:{} for value:{},result:{} ", key, value, result);
return result;
}
/**
*
* setNx方法
* 此方法有坑,setNx和expire不是原子操作
* @param key
* @param value
* @param seconds
* @return
*/
@Override
public boolean setNx(K key, V value, Long seconds) {
if (key != null && value != null && seconds != null) {
log.debug("redis setNx key:{} for value:{} seconds:{} ", key, value, seconds);
byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
byte[] valueByte = redisTemplate.getValueSerializer().serialize(value);
Boolean setNX = false;
RedisConnection connection = null;
try {
connection = redisTemplate.getConnectionFactory().getConnection();
setNX = connection.setNX(keyByte, valueByte);
connection.expire(keyByte, seconds);
} catch (Exception e) {
log.error("redis setNx error:", e);
} finally{
if(connection != null){
connection.close();
}
}
return setNX;
} else {
log.warn("there are some problems to redis setNx key:{} for value:{} ", key, value);
}
return false;
}
@Override
public V hashGet(HK key) {
if (key == null || getKey() == null) {
log.warn("redis hashGet for key:{} hkey:{} can't be null", getKey(), key);
return null;
}
log.debug("redis hashGet for key:{} hkey:{} ", getKey(), key);
return (V) redisTemplate.opsForHash().get(getKey(), key);
}
@Override
public void hashSet(HK key, V value) {
if (getKey() == null || key == null || value == null) {
log.warn("redis hashSet for key:{} hkey:{} value:{} can't be null", getKey(), key, value);
} else {
log.debug("redis hashSet for key:{} hkey:{} value:{} ", getKey(), key, value);
redisTemplate.opsForHash().put(getKey(), key, value);
}
}
@Override
public List<V> hashMultiGet(List<HK> keys) {
if (getKey() == null || CollectionUtils.isEmpty(keys)) {
log.warn("redis hashMultiGet keys or Key can't be null ");
return new ArrayList<>();
}
log.debug("redis hashMultiGet for Key:{} keys:{}", getKey(), JSONObject.toJSONString(keys));
List<V> list = (List<V>) redisTemplate.opsForHash().multiGet(getKey(), keys);
removeNull(list);
return list;
}
@Override
public void hashSet(Map<HK, V> map) {
if (getKey() == null || CollectionUtils.isEmpty(map)) {
log.warn("redis hashSet map or key cant be null or empty ");
} else {
log.debug("redis hashSet for key:{} map:{} ", getKey(), JSONObject.toJSONString(map));
redisTemplate.opsForHash().putAll(getKey(), map);
}
}
@Override
public List<V> hashValuesGetAll() {
if (getKey() == null) {
log.warn("redis hashValuesGetAll key can't be null");
return null;
} else {
log.debug("redis hashValuesGetAll for key:{} ", getKey());
List<V> list = (List<V>) redisTemplate.opsForHash().values(getKey());
removeNull(list);
return (List<V>) redisTemplate.opsForHash().values(getKey());
}
}
/**
* 移除 返回类型中的null 元素
*
* @param list
*/
private void removeNull(List list) {
if (!CollectionUtils.isEmpty(list)) {
// 移出null 元素
list.removeAll(Collections.singleton(null));
}
}
}
import java.util.List;
import java.util.Map;
/**
* redis 基础服务
*
* @author 8102
* @date 2018/6/19
*/
public interface RedisBaseService<K, HK, V> {
//**************************** key - value ***************************************
/**
* 通过key 获取value
*
* @param key
* @return
*/
V get(K key);
/**
* 设置 key-value 缓存
*
* @param key
* @param value
*/
void set(K key, V value, Long expireTime);
/**
* 判断键是否存在
*
* @return
*/
boolean isExist();
/**
* 判断键是否存在
*
* @return
*/
boolean isExist(K key);
/**
* 删除键
*
* @return
*/
void delete();
/**
* 删除键
*
* @return
*/
void delete(K key);
/**
* 设置过期时间 秒
*/
void expire(Long times);
/**
* 设置自增
*
* @param key
* @param value
* @return
*/
Long increment(K key, Long value);
/**
* 设置监控值 若存在不设置,若不存在将value 设置进去
*
* @param key
* @param value
* @return
*/
boolean setNx(K key, V value);
/**
* 设置监控值 若存在不设置,若不存在将value 设置进去
* 增加过期时间
* @param key
* @param value
* @param seconds
* @return
*/
boolean setNx(K key, V value, Long seconds);
//*************************** hash key - value ***************************************
/**
* hash 通过key 获取value
*
* @param key
* @return
*/
V hashGet(HK key);
/**
* 设置 hash key-value 缓存
*
* @param key
* @param value
*/
void hashSet(HK key, V value);
/**
* 获取指定hashKey 里面所有 value
*
* @return
*/
List<V> hashMultiGet(List<HK> keys);
/**
* 批量设置 hash key-value 缓存
*
* @param map
*/
void hashSet(Map<HK, V> map);
/**
* 获取所有 hash key-value 缓存
*/
List<V> hashValuesGetAll();
}
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置
*
* @8102 2020/3/19
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.isCacheEnabled}")
private boolean isCacheEnabled;
@Override
@Bean
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
};
}
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
return rcm;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.17.RELEASE</version>
</dependency>
配置
spring:
redis:
pool.max-active: 8
pool.max-wait: -1
pool.max-idle: 8
pool.min-idle: 0
timeout: 0
isCacheEnabled: true