1、修改脚本,去除脚本中的自增

2、脚本中添加唯一索引
3、数据库记录的内容优化为:IP:端口
pull/19/head
shanhy 5 years ago
parent 3d9252115b
commit 71cd96944c

@ -111,10 +111,11 @@ CREATE TABLE `xxl_job_lock` (
CREATE TABLE `xxl_job_machine` ( CREATE TABLE `xxl_job_machine` (
`machine_ip` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主机IP', `machine_ip` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主机IP',
`machine_id` int(11) NOT NULL COMMENT '主机IP对应的机器码', `machine_id` int(4) NOT NULL COMMENT '主机IP对应的机器码',
`add_time` datetime NOT NULL COMMENT '创建时间', `add_time` datetime NOT NULL COMMENT '创建时间',
`heart_last_time` datetime NOT NULL COMMENT '最后一次心跳时间', `heart_last_time` datetime NOT NULL COMMENT '最后一次心跳时间',
PRIMARY KEY (`machine_ip`) USING BTREE PRIMARY KEY (`machine_ip`) USING BTREE,
UNIQUE INDEX `index_machine_id`(`machine_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

@ -270,7 +270,7 @@ CREATE UNIQUE INDEX I_USERNAME
DROP TABLE XXL_JOB_MACHINE; DROP TABLE XXL_JOB_MACHINE;
CREATE TABLE XXL_JOB_MACHINE ( CREATE TABLE XXL_JOB_MACHINE (
MACHINE_IP NVARCHAR2(16) NOT NULL , MACHINE_IP NVARCHAR2(16) NOT NULL ,
MACHINE_ID NUMBER(11) NOT NULL , MACHINE_ID NUMBER(4) NOT NULL ,
ADD_TIME DATE NOT NULL , ADD_TIME DATE NOT NULL ,
HEART_LAST_TIME DATE NOT NULL HEART_LAST_TIME DATE NOT NULL
); );
@ -284,6 +284,9 @@ COMMENT ON COLUMN XXL_JOB_MACHINE.HEART_LAST_TIME IS '最后一次心跳时间';
-- ---------------------------- -- ----------------------------
ALTER TABLE XXL_JOB_MACHINE ADD CONSTRAINT SYS_C0026684 PRIMARY KEY (MACHINE_IP); ALTER TABLE XXL_JOB_MACHINE ADD CONSTRAINT SYS_C0026684 PRIMARY KEY (MACHINE_IP);
CREATE UNIQUE INDEX I_MACHINE_ID
ON XXL_JOB_MACHINE (MACHINE_ID ASC);
-- ---------------------------- -- ----------------------------
-- Checks structure for table XXL_JOB_MACHINE -- Checks structure for table XXL_JOB_MACHINE
-- ---------------------------- -- ----------------------------

@ -1,22 +1,26 @@
package com.xxl.job.admin.controller; package com.xxl.job.admin.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xxl.job.admin.controller.annotation.PermissionLimit; import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.core.conf.XxlJobAdminConfig; import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.AdminBiz; import com.xxl.job.core.biz.AdminBiz;
import com.xxl.job.core.biz.model.HandleCallbackParam; import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.biz.model.RegistryParam; import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.GsonTool; import com.xxl.job.core.util.GsonTool;
import com.xxl.job.core.util.XxlJobRemotingUtil; import com.xxl.job.core.util.XxlJobRemotingUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/** /**
* Created by xuxueli on 17/5/10. * Created by xuxueli on 17/5/10.
@ -28,6 +32,9 @@ public class JobApiController {
@Resource @Resource
private AdminBiz adminBiz; private AdminBiz adminBiz;
@Resource
private XxlJobService xxlJobService;
/** /**
* api * api
* *
@ -63,6 +70,12 @@ public class JobApiController {
} else if ("registryRemove".equals(uri)) { } else if ("registryRemove".equals(uri)) {
RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class); RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
return adminBiz.registryRemove(registryParam); return adminBiz.registryRemove(registryParam);
} else if ("jobInfoAdd".equals(uri)) {
XxlJobInfo jobInfo = GsonTool.fromJson(data, XxlJobInfo.class);
return xxlJobService.add(jobInfo);
} else if ("jobInfoRemove".equals(uri)) {
XxlJobInfo jobInfo = GsonTool.fromJson(data, XxlJobInfo.class);
return xxlJobService.remove(jobInfo.getId());
} else { } else {
return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping("+ uri +") not found."); return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping("+ uri +") not found.");
} }

@ -3,6 +3,7 @@ package com.xxl.job.admin.core.id;
import java.util.Date; import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -15,15 +16,18 @@ import com.xxl.job.admin.core.util.MachineUtils;
@EnableScheduling @EnableScheduling
public class HeartBeat { public class HeartBeat {
@Value("${server.port}")
private String serverPort;
@Autowired @Autowired
private MachineService machineService; private MachineService machineService;
@Scheduled(fixedDelay = 10000) @Scheduled(fixedDelay = 10000)
public void checkMachineSurvive() { public void checkMachineSurvive() {
String machineIp = MachineUtils.getIP(); String machineIpStr = MachineUtils.getIPAndPort(serverPort);
XxlJobMachine xxlJobMachine = machineService.selectByMachineIp(machineIp); XxlJobMachine xxlJobMachine = machineService.selectByMachineIp(machineIpStr);
if (xxlJobMachine != null) { if (xxlJobMachine != null) {
machineService.update(machineIp, new Date()); machineService.update(machineIpStr, new Date());
} }
} }
} }

@ -7,6 +7,7 @@ import com.xxl.job.admin.dao.XxlJobMachineDao;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -18,6 +19,9 @@ public class MachineServiceImpl implements MachineService {
private final Logger logger = LoggerFactory.getLogger(MachineServiceImpl.class); private final Logger logger = LoggerFactory.getLogger(MachineServiceImpl.class);
@Value("${server.port}")
private String serverPort;
@Autowired @Autowired
private XxlJobMachineDao xxlJobMachineDao; private XxlJobMachineDao xxlJobMachineDao;
@ -43,37 +47,43 @@ public class MachineServiceImpl implements MachineService {
@Override @Override
public Integer getInitMachineId() { public Integer getInitMachineId() {
XxlJobMachine xxlJobMachine = selectByMachineIp(MachineUtils.getIP()); String ipStr = MachineUtils.getIPAndPort(serverPort);
XxlJobMachine xxlJobMachine = selectByMachineIp(ipStr);
Date nowDate = new Date(); Date nowDate = new Date();
int machineId = -1; int machineId = -1;
if(xxlJobMachine != null){ if(xxlJobMachine != null){
update(MachineUtils.getIP(),nowDate); update(ipStr,nowDate);
machineId = xxlJobMachine.getMachineId(); machineId = xxlJobMachine.getMachineId();
}else{ }else{
xxlJobMachine = new XxlJobMachine(); xxlJobMachine = new XxlJobMachine();
xxlJobMachine.setMachineIp(MachineUtils.getIP()); xxlJobMachine.setMachineIp(ipStr);
xxlJobMachine.setAddTime(nowDate); xxlJobMachine.setAddTime(nowDate);
xxlJobMachine.setHeartLastTime(nowDate); xxlJobMachine.setHeartLastTime(nowDate);
Random random = new Random(); Random random = new Random();
boolean flag = false;
for(int i = 0; i < 100; i++){ for(int i = 0; i < 100; i++){
try { try {
Integer value = selectMaxMachineId(); Integer value = selectMaxMachineId();
machineId = value == null ? 1 : value+1; machineId = value == null ? 1 : value+1;
xxlJobMachine.setMachineId(machineId); xxlJobMachine.setMachineId(machineId);
save(xxlJobMachine); save(xxlJobMachine);
flag = true;
break; break;
} catch (DuplicateKeyException e) { } catch (DuplicateKeyException e) {
try { try {
Thread.sleep(random.nextInt(1000)+1); Thread.sleep(random.nextInt(2000)+1);
} catch (InterruptedException interruptedException) { } catch (InterruptedException interruptedException) {
logger.error("sleep error,cause",interruptedException); logger.error("sleep error,cause",interruptedException);
} }
logger.error("retry >>>>>>>>>>>>> "); logger.error("retry >>>>>>>>>>>>> ");
} catch (Exception e){ } catch (Exception e){
logger.error("save error >>>>>>,system exit,cause",e); logger.error("save error >>>>>>,system exit,cause",e);
System.exit(0);
} }
} }
if(!flag) {
logger.error("多次获取machineId失败退出程序");
System.exit(0);
}
} }
return machineId; return machineId;
} }

@ -19,6 +19,10 @@ public class MachineUtils {
private static String machineIp = null; private static String machineIp = null;
public static String getIPAndPort(String port) {
return getIP().concat(":").concat(port);
}
public static String getIP() { public static String getIP() {
if (machineIp == null) { if (machineIp == null) {
String ipv4 = getInet4Address(); String ipv4 = getInet4Address();

Loading…
Cancel
Save