Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
clx-performance
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
姜武杰
clx-performance
Commits
0f70ddb3
提交
0f70ddb3
authored
10月 30, 2023
作者:
liruixin
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'origin/v5.7_break_contract_reverse_20231020' into…
Merge remote-tracking branch 'origin/v5.7_break_contract_reverse_20231020' into v5.7_break_contract_reverse_20231020
上级
b6d1b015
28f0d42d
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
167 行增加
和
0 行删除
+167
-0
IdGenerateSnowFlake.java
...va/com/clx/performance/component/IdGenerateSnowFlake.java
+165
-0
GoodsOrderController.java
...m/clx/performance/controller/pc/GoodsOrderController.java
+2
-0
没有找到文件。
performance-web/src/main/java/com/clx/performance/component/IdGenerateSnowFlake.java
0 → 100644
浏览文件 @
0f70ddb3
package
com
.
clx
.
performance
.
component
;
import
com.alibaba.cloud.nacos.NacosDiscoveryProperties
;
import
com.clx.performance.constant.RedisConstants
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Component
;
import
java.util.Map
;
import
java.util.concurrent.atomic.AtomicLong
;
/**
* 雪花算法生成唯一标示
* datacenterId:根据服务的ip地址区分,当前最多支持32太机器
* channelConfTypeId: 用于区分不同渠道的消息标示 用了6位的二进制位标示
* 从17位~23位 可看当前消息类型
* channelConfType : 1:充值 2:外呼 3:push
*/
@Component
public
class
IdGenerateSnowFlake
implements
InitializingBean
{
@Autowired
private
NacosDiscoveryProperties
nacosDiscoveryProperties
;
@Autowired
private
RedisTemplate
<
String
,
String
>
redisTemplate
;
//时间 41位
private
static
long
lastTime
=
System
.
currentTimeMillis
();
//数据中心ID 5位(默认0-31) 通过nacos获取服务ip注册redis统计 %32
private
long
datacenterId
=
0
;
private
long
datacenterIdShift
=
4
;
private
long
channelConfTypeId
=
0
;
private
long
channelConfTypeIdShirt
=
6
;
// 机房机器ID 5位(默认0-31)
// private long workerId = 0;
// private long workerIdShift = 5;
//随机数 12位(默认0~4095)
private
AtomicLong
random
=
new
AtomicLong
();
private
long
randomShift
=
12
;
//随机数的最大值
private
long
maxRandom
=
(
long
)
Math
.
pow
(
2
,
randomShift
);
// public SnowFlake(long workerIdShift, long datacenterIdShift){
// if (workerIdShift < 0 ||
// datacenterIdShift < 0 ||
// workerIdShift + datacenterIdShift > 22) {
// throw new IllegalArgumentException("参数不匹配");
// }
// this.workerIdShift = workerIdShift;
// this.datacenterIdShift = datacenterIdShift;
// this.randomShift = 22 - datacenterIdShift - workerIdShift;
// this.maxRandom = (long) Math.pow(2, randomShift);
// }
//获取雪花的ID
private
long
getId
(
Long
channelConfTypeId
)
{
return
lastTime
<<
(
channelConfTypeIdShirt
+
datacenterIdShift
+
randomShift
)
|
channelConfTypeId
<<
(
datacenterIdShift
+
randomShift
)
|
datacenterId
<<
randomShift
|
random
.
get
();
}
//生成一个新的ID
public
synchronized
long
nextId
(
Long
channelConfTypeId
)
{
long
now
=
System
.
currentTimeMillis
();
//如果当前时间和上一次时间不在同一毫秒内,直接返回
if
(
now
>
lastTime
)
{
lastTime
=
now
;
random
.
set
(
0
);
return
getId
(
channelConfTypeId
);
}
//将最后的随机数,进行+1操作
if
(
random
.
incrementAndGet
()
<
maxRandom
)
{
return
getId
(
channelConfTypeId
);
}
//自选等待下一毫秒
while
(
now
<=
lastTime
)
{
now
=
System
.
currentTimeMillis
();
}
lastTime
=
now
;
random
.
set
(
0
);
return
getId
(
channelConfTypeId
);
}
public
synchronized
String
nextIdToString
(
Long
channelConfTypeId
)
{
long
now
=
System
.
currentTimeMillis
();
//如果当前时间和上一次时间不在同一毫秒内,直接返回
if
(
now
>
lastTime
)
{
lastTime
=
now
;
random
.
set
(
0
);
return
getId
(
channelConfTypeId
)
+
""
;
}
//将最后的随机数,进行+1操作
if
(
random
.
incrementAndGet
()
<
maxRandom
)
{
return
getId
(
channelConfTypeId
)
+
""
;
}
//自选等待下一毫秒
while
(
now
<=
lastTime
)
{
now
=
System
.
currentTimeMillis
();
}
lastTime
=
now
;
random
.
set
(
0
);
return
getId
(
channelConfTypeId
)
+
""
;
}
/**
* 获取channelType
* @param n
* @return
*/
public
Integer
getChannelType
(
Long
n
){
String
binaryString
=
Long
.
toBinaryString
(
n
);
String
substring
=
binaryString
.
substring
(
41
,
47
);
return
Integer
.
parseInt
(
substring
,
2
);
}
//测试
public
static
void
main
(
String
[]
args
)
{
IdGenerateSnowFlake
msgSnowFlake
=
new
IdGenerateSnowFlake
();
String
s1
=
msgSnowFlake
.
nextIdToString
(
0L
);
String
s2
=
msgSnowFlake
.
nextIdToString
(
1L
);
String
s3
=
msgSnowFlake
.
nextIdToString
(
2L
);
String
s4
=
msgSnowFlake
.
nextIdToString
(
3L
);
String
s5
=
msgSnowFlake
.
nextIdToString
(
4L
);
Integer
channelType
=
msgSnowFlake
.
getChannelType
(
Long
.
valueOf
(
s1
));
Integer
channelType1
=
msgSnowFlake
.
getChannelType
(
Long
.
valueOf
(
s2
));
Integer
channelType2
=
msgSnowFlake
.
getChannelType
(
Long
.
valueOf
(
s3
));
Integer
channelType3
=
msgSnowFlake
.
getChannelType
(
Long
.
valueOf
(
s4
));
Integer
channelType4
=
msgSnowFlake
.
getChannelType
(
Long
.
valueOf
(
s5
));
System
.
out
.
println
(
channelType1
);
}
/**
* 根据不同的ip注册机器ID,用于保证ID全局唯一
* @throws Exception
*/
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
//Map<Object, Object> msgSnowFlake = redisTemplate.opsForHash().entries(RedisConstants.ID_SNOWFLAKE);
String
ip
=
nacosDiscoveryProperties
.
getIp
();
//datacenterId = msgSnowFlake.size() % 32;
//redisTemplate.opsForHash().put(RedisConstants.ID_SNOWFLAKE, ip, String.valueOf(msgSnowFlake.size() + 1));
}
}
\ No newline at end of file
performance-web/src/main/java/com/clx/performance/controller/pc/GoodsOrderController.java
浏览文件 @
0f70ddb3
...
@@ -22,6 +22,7 @@ import com.msl.common.result.Result;
...
@@ -22,6 +22,7 @@ import com.msl.common.result.Result;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang.exception.ExceptionUtils
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RedissonClient
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
...
@@ -87,6 +88,7 @@ public class GoodsOrderController {
...
@@ -87,6 +88,7 @@ public class GoodsOrderController {
goodsOrderStrategyContext
.
strategyContext
.
get
(
truckDemand
).
saveGoodsOrder
(
orderGoodsParams
,
orderInfo
,
now
,
rabbitTemplate
);
goodsOrderStrategyContext
.
strategyContext
.
get
(
truckDemand
).
saveGoodsOrder
(
orderGoodsParams
,
orderInfo
,
now
,
rabbitTemplate
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
ExceptionUtils
.
getStackTrace
(
e
));
throw
new
ServiceSystemException
(
PerformanceResultEnum
.
HTTP_ERROR
,
e
.
getMessage
());
throw
new
ServiceSystemException
(
PerformanceResultEnum
.
HTTP_ERROR
,
e
.
getMessage
());
}
finally
{
}
finally
{
try
{
try
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论