Commit 56a61889 by hweeeeeei

1.优化雪花算法ID生成,从redis取自增id作为workerId;

2.增加Id生成单元测试类;
parent ff7ce827
package io.geekidea.springbootplus.test;
import com.wecloud.utils.SnowflakeUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Id生成 单元测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class IdTest {
@Test
public void test() {
Long id = SnowflakeUtil.getId();
}
}
......@@ -4,7 +4,7 @@ import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.service.ImConversationMembersService;
import com.wecloud.im.service.ImConversationService;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -4,7 +4,7 @@ import cn.hutool.core.codec.Base64;
import com.wecloud.im.entity.ImIosApns;
import com.wecloud.im.service.ImIosApnsService;
import com.wecloud.im.ws.sender.IosPush;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -19,9 +19,8 @@ import com.wecloud.im.service.ImClientService;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......
......@@ -16,7 +16,7 @@ import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......
......@@ -42,7 +42,7 @@ import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import io.geekidea.springbootplus.framework.shiro.util.JwtUtil;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
......
......@@ -21,7 +21,7 @@ import com.wecloud.im.ws.sender.ChannelSender;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
......
......@@ -24,7 +24,7 @@ import com.wecloud.im.ws.strategy.AbstractImCmdStrategy;
import com.wecloud.utils.JsonUtils;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
......
......@@ -25,7 +25,7 @@ import com.wecloud.im.ws.strategy.AbstractImCmdStrategy;
import com.wecloud.utils.JsonUtils;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
......
......@@ -9,7 +9,6 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.sql.Time;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
......@@ -47,6 +46,20 @@ public class RedisUtils {
redisTemplate.opsForValue().set(key, value, timeout);
}
/**
* 递增
*
* @param key 键
* @delta 要增加几(大于0)
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 删除Key
*
......
......@@ -24,7 +24,7 @@ import com.wecloud.rtc.service.MangerRtcCacheService;
import com.wecloud.rtc.service.RtcService;
import com.wecloud.rtc.service.WsRtcWrite;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import com.wecloud.utils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......
package com.wecloud.utils;
import cn.hutool.core.lang.Snowflake;
import com.wecloud.im.ws.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* 雪花算法 获取id工具类
*
* @author
*/
@Component
public class SnowflakeUtil {
private static SnowflakeUtil snowflakeUtil;
/**
* workerId, dataCenterId动态获取
* 12位序列号部分,支持同一毫秒内同一个节点可以生成4096个ID, 在目前一段不用做成动态获取服务器ID
*/
private static volatile Snowflake SNOWFLAKE = null;
@Autowired
private RedisUtils redisUtils;
/**
* 多线程中加synchronized 保证不会获取重复id
*
* @return
*/
public static Long getId() {
if (SNOWFLAKE == null) {
synchronized (SnowflakeUtil.class) {
if (SNOWFLAKE == null) {
// workerId通过redis获取
long workerId = snowflakeUtil.redisUtils.incr("workerId", 1);
// redisUtils不需要用了,释放回收掉
snowflakeUtil.redisUtils = null;
SNOWFLAKE = new Snowflake(workerId, 1L);
}
}
}
return SNOWFLAKE.nextId();
}
/**
* 静态方法里调用spring注入的方法
*/
@PostConstruct
public void init() {
snowflakeUtil = this;
snowflakeUtil.redisUtils = this.redisUtils;
}
}
package io.geekidea.springbootplus.framework.shiro.util;
import cn.hutool.core.lang.Snowflake;
/**
* 雪花算法 获取id工具类
*
* @author
*/
public class SnowflakeUtil {
private static final Snowflake SNOWFLAKE = new Snowflake(1L, 1L);
/**
* 多线程中加synchronized 保证不会获取重复id
*
* @return
*/
public static synchronized Long getId() {
return SNOWFLAKE.nextId();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment