package org.n3r.idworker.strategy;
import org.n3r.idworker.WorkerIdStrategy;
import org.n3r.idworker.utils.HttpReq;
import org.n3r.idworker.utils.Ip;
import org.n3r.idworker.utils.Props;
import org.n3r.idworker.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Properties;
import java.util.Random;
public class DefaultWorkerIdStrategy implements WorkerIdStrategy {
static long workerIdBits = 10L;
static long maxWorkerId = -1L ^ (-1L << workerIdBits);
static Random random = new SecureRandom();
public static final WorkerIdStrategy instance = new DefaultWorkerIdStrategy();
private final Properties props =
Props.tryProperties("idworker-client.properties", Utils.DOT_IDWORKERS);
private final String idWorkerServerUrl =
props.getProperty("server.address", "http://id.worker.server:18001");
String userName = System.getProperty("user.name");
String ipDotUsername = Ip.ip + "." + userName;
String ipudotlock = ipDotUsername + ".lock.";
int workerIdIndex = ipudotlock.length();
long workerId;
FileLock fileLock;
Logger logger = LoggerFactory.getLogger(DefaultWorkerIdStrategy.class);
private boolean inited;
private void init() {
workerId = findAvailWorkerId();
if (workerId >= 0) {
destroyFileLockWhenShutdown();
startSyncThread();
} else {
syncWithWorkerIdServer();
workerId = findAvailWorkerId();
if (workerId < 0) workerId = increaseWithWorkerIdServer();
}
if (workerId < 0) workerId = tryToCreateOnIp();
if (workerId < 0) {
logger.warn("DANGEROUS!!! Try to use random worker id.");
workerId = tryToRandomOnIp(); // Try avoiding! it could cause duplicated
}
if (workerId < 0) {
logger.warn("the world may be ended!");
throw new RuntimeException("the world may be ended");
}
}
private void destroyFileLockWhenShutdown() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
fileLock.destroy();
}
});
}
private void startSyncThread() {
new Thread() {
@Override
public void run() {
syncWithWorkerIdServer();
}
}.start();
}
private long increaseWithWorkerIdServer() {
String incId = HttpReq.get(idWorkerServerUrl)
.req("/inc")
.param("ipu", ipDotUsername)
.exec();
if (incId == null || incId.trim().isEmpty()) return -1L;
long lid = Long.parseLong(incId);
return checkAvail(lid);
}
private long tryToCreateOnIp() {
long wid = Ip.lip & maxWorkerId;
return checkAvail(wid);
}
private long tryToRandomOnIp() {
long avaiWorkerId = -1L;
long tryTimes = -1;
while (avaiWorkerId < 0 && ++tryTimes < maxWorkerId) {
long wid = Ip.lip & random.nextInt((int) maxWorkerId);
avaiWorkerId = checkAvail(wid);
}
return avaiWorkerId;
}
private long checkAvail(long wid) {
long availWorkerId = -1L;
try {
File idWorkerHome = Utils.createIdWorkerHome();
new File(idWorkerHome, ipudotlock + String.format("%04d", wid)).createNewFile();
availWorkerId = findAvailWorkerId();
} catch (IOException e) {
logger.warn("checkAvail error", e);
}
return availWorkerId;
}
private void syncWithWorkerIdServer() {
String syncIds = HttpReq.get(idWorkerServerUrl).req("/sync")
.param("ipu", ipDotUsername).param("ids", buildWorkerIdsOfCurrentIp())
.exec();
if (syncIds == null || syncIds.trim().isEmpty()) return;
String[] syncIdsArr = syncIds.split(",");
File idWorkerHome = Utils.createIdWorkerHome();
for (String syncId : syncIdsArr) {
try {
new File(idWorkerHome, ipudotlock + syncId).createNewFile();
} catch (IOException e) {
logger.warn("create workerid lock file error", e);
}
}
}
private String buildWorkerIdsOfCurrentIp() {
StringBuilder sb = new StringBuilder();
File idWorkerHome = Utils.createIdWorkerHome();
for (File lockFile : idWorkerHome.listFiles()) {
// check the format like 10.142.1.151.lock.0001
if (!lockFile.getName().startsWith(ipudotlock)) continue;
String workerId = lockFile.getName().substring(workerIdIndex);
if (!workerId.matches("\\d\\d\\d\\d")) continue;
if (sb.length() > 0) sb.append(',');
sb.append(workerId);
}
return sb.toString();
}
/**
* Find the local available worker id.
*
* @return -1 when N/A
*/
private long findAvailWorkerId() {
File idWorkerHome = Utils.createIdWorkerHome();
for (File lockFile : idWorkerHome.listFiles()) {
// check the format like 10.142.1.151.lock.0001
if (!lockFile.getName().startsWith(ipudotlock)) continue;
String workerId = lockFile.getName().substring(workerIdIndex);
if (!workerId.matches("\\d\\d\\d\\d")) continue;
FileLock fileLock = new FileLock(lockFile);
if (!fileLock.tryLock()) {
fileLock.destroy();
continue;
}
this.fileLock = fileLock;
return Long.parseLong(workerId);
}
return -1;
}
@Override
public void initialize() {
if (inited) return;
init();
this.inited = true;
}
@Override
public long availableWorkerId() {
return workerId;
}
@Override
public void release() {
if (fileLock != null) fileLock.destroy();
inited = false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java毕业设计 基于SpringBoot+Mybatis的物业管理系统的设计与实现+详细文档+全部资料(优秀项目).zip
共155个文件
java:69个
gif:26个
html:24个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 8 浏览量
2024-04-17
10:50:16
上传
评论
收藏 980KB ZIP 举报
温馨提示
【资源说明】 java毕业设计 基于SpringBoot+Mybatis的物业管理系统的设计与实现+详细文档+全部资料(优秀项目).zipjava毕业设计 基于SpringBoot+Mybatis的物业管理系统的设计与实现+详细文档+全部资料(优秀项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕设项目、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 3、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
java毕业设计 基于SpringBoot+Mybatis的物业管理系统的设计与实现+详细文档+全部资料(优秀项目).zip (155个子文件)
layout.css 12KB
adminStyle.css 10KB
login.css 5KB
icon_trash.gif 1KB
icon_trash.gif 1KB
icon_account.gif 603B
icon_account.gif 603B
icon_edit.gif 581B
icon_edit.gif 581B
icon_drop.gif 579B
icon_drop.gif 579B
icon_view.gif 576B
icon_view.gif 576B
icon_search.gif 396B
icon_search.gif 396B
icon_title.gif 214B
icon_title.gif 214B
arrow_right.gif 155B
arrow_right.gif 155B
arrow_left.gif 153B
arrow_left.gif 153B
no.gif 138B
no.gif 138B
yes.gif 131B
yes.gif 131B
menu_plus.gif 67B
menu_plus.gif 67B
menu_minus.gif 64B
menu_minus.gif 64B
.gitignore 268B
message_list.html 5KB
cost_list.html 5KB
building_list.html 4KB
parking_list.html 4KB
owner_add.html 3KB
user_upd.html 3KB
building_upd.html 3KB
user_list.html 3KB
owner_list.html 3KB
menu.html 3KB
parking_upd.html 3KB
login.html 2KB
owner_upd.html 2KB
register.html 2KB
repassword.html 2KB
main.html 2KB
message_add.html 2KB
revise_password.html 2KB
error.html 1KB
cost_add.html 1KB
user_add.html 1KB
top.html 1KB
bar.html 836B
index.html 799B
maven-wrapper.jar 46KB
DefaultWorkerIdStrategy.java 6KB
JSONResult.java 5KB
DefaultRandomCodeStrategy.java 5KB
OwnerContoller.java 4KB
FileLock.java 4KB
MessageContoller.java 4KB
HttpReq.java 4KB
All.java 3KB
Utils.java 3KB
Serializes.java 3KB
MessageServiceImpl.java 3KB
AllServiceImpl.java 3KB
CostContoller.java 3KB
CostServiceImpl.java 3KB
BuildingContoller.java 3KB
UserContoller.java 3KB
indexController.java 3KB
ParkingContoller.java 3KB
IdWorker.java 3KB
BuildingServiceImpl.java 2KB
LoginInterceptor.java 2KB
Props.java 2KB
UserServiceImpl.java 2KB
IPv4Utils.java 2KB
OwnerServiceImpl.java 2KB
ParkingServiceImpl.java 2KB
JsonUtils.java 2KB
Building.java 2KB
Message.java 2KB
Sid.java 2KB
Cost.java 2KB
Ip.java 2KB
User.java 1KB
MyMapper.java 1KB
AllExceptionHandler.java 1KB
LoginController.java 1KB
Parking.java 1KB
AsyncTask.java 1KB
Owner.java 1KB
GeneratorDisplay.java 1KB
DayPrefixRandomCodeStrategy.java 1001B
Code.java 972B
AllTable.java 951B
WebMvcConfigurer.java 883B
AllMapperCustom.java 850B
共 155 条
- 1
- 2
资源评论
不走小道
- 粉丝: 3234
- 资源: 5113
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功