parent
cd2c83564c
commit
8501e3f8e7
@ -0,0 +1,45 @@
|
||||
package com.xxl.job.admin.core.id;
|
||||
|
||||
import com.xxl.job.admin.core.id.service.MachineService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Component
|
||||
public class GenerateId {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(GenerateId.class);
|
||||
|
||||
private SnowflakeIdWorker idWorker = null;
|
||||
|
||||
@Autowired
|
||||
private MachineService machineService;
|
||||
|
||||
private Integer machineId = -1;
|
||||
|
||||
public Long getId() {
|
||||
return idWorker.nextId();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void getIdBefore() {
|
||||
//只需要第一次调用 对idworker进行初始化
|
||||
machineId = machineService.getInitMachineId();
|
||||
idWorker = new SnowflakeIdWorker(machineId);
|
||||
}
|
||||
|
||||
public Integer getMachineId(){
|
||||
return this.machineId;
|
||||
}
|
||||
|
||||
public void setMachineId(Integer machineId){
|
||||
this.machineId = machineId;
|
||||
}
|
||||
|
||||
public void setIdWorker(SnowflakeIdWorker snowflakeIdWorker){
|
||||
this.idWorker = snowflakeIdWorker;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.xxl.job.admin.core.id;
|
||||
|
||||
import com.xxl.job.admin.core.id.service.MachineService;
|
||||
import com.xxl.job.admin.core.model.XxlJobMachine;
|
||||
import com.xxl.job.admin.core.util.MachineUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
@EnableScheduling
|
||||
public class HeartBeat {
|
||||
|
||||
@Autowired
|
||||
private MachineService machineService;
|
||||
|
||||
@Scheduled(fixedDelay = 10000)
|
||||
public void checkMachineSurvive(){
|
||||
String machineIp = MachineUtils.get();
|
||||
XxlJobMachine xxlJobMachine = machineService.selectByMachineIp(machineIp);
|
||||
if(xxlJobMachine != null){
|
||||
machineService.update(machineIp,new Date());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.xxl.job.admin.core.id.service;
|
||||
|
||||
import com.xxl.job.admin.core.model.XxlJobMachine;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public interface MachineService {
|
||||
|
||||
void save(XxlJobMachine xxlJobMachine);
|
||||
|
||||
void update(String machineIp, Date heartLastTime);
|
||||
|
||||
XxlJobMachine selectByMachineIp(String machineIp);
|
||||
|
||||
Integer selectMaxMachineId();
|
||||
|
||||
Integer getInitMachineId();
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.xxl.job.admin.core.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class XxlJobMachine {
|
||||
|
||||
/**
|
||||
* 主机IP
|
||||
*/
|
||||
private String machineIp;
|
||||
|
||||
/**
|
||||
* 主机IP对应的机器码
|
||||
*/
|
||||
private Integer machineId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date addTime;
|
||||
|
||||
/**
|
||||
* 最后一次心跳时间
|
||||
*/
|
||||
private Date heartLastTime;
|
||||
|
||||
public Date getHeartLastTime() {
|
||||
return heartLastTime;
|
||||
}
|
||||
|
||||
public void setHeartLastTime(Date heartLastTime) {
|
||||
this.heartLastTime = heartLastTime;
|
||||
}
|
||||
|
||||
public Integer getMachineId() {
|
||||
return machineId;
|
||||
}
|
||||
|
||||
public void setMachineId(Integer machineId) {
|
||||
this.machineId = machineId;
|
||||
}
|
||||
|
||||
public Date getAddTime() {
|
||||
return addTime;
|
||||
}
|
||||
|
||||
public void setAddTime(Date addTime) {
|
||||
this.addTime = addTime;
|
||||
}
|
||||
|
||||
public String getMachineIp() {
|
||||
return machineIp;
|
||||
}
|
||||
|
||||
public void setMachineIp(String machineIp) {
|
||||
this.machineIp = machineIp;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.xxl.job.admin.dao;
|
||||
|
||||
import com.xxl.job.admin.core.model.XxlJobMachine;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Mapper
|
||||
public interface XxlJobMachineDao {
|
||||
|
||||
void save(@Param("xxlJobMachine")XxlJobMachine xxlJobMachine);
|
||||
|
||||
void update(@Param("machineIp") String machineIp, @Param("heartLastTime") Date heartLastTime);
|
||||
|
||||
XxlJobMachine selectByHostIp(String machineIp);
|
||||
|
||||
Integer selectMaxMachineId();
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.xxl.job.admin.dao.XxlJobMachineDao">
|
||||
|
||||
<resultMap id="XxlJobMachine" type="com.xxl.job.admin.core.model.XxlJobMachine" >
|
||||
<result column="machine_ip" property="machineIp" />
|
||||
<result column="machine_id" property="machineId" />
|
||||
<result column="add_time" property="addTime" />
|
||||
<result column="heart_last_time" property="heartLastTime" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
machine_ip,machine_id,add_time,heart_last_time
|
||||
</sql>
|
||||
|
||||
<select id="selectByHostIp" resultMap="XxlJobMachine">
|
||||
SELECT machine_ip,machine_id,add_time,heart_last_time
|
||||
FROM XXL_JOB_MACHINE
|
||||
WHERE machine_ip = #{machineIp}
|
||||
</select>
|
||||
|
||||
<insert id="save" parameterType="com.xxl.job.admin.core.model.XxlJobMachine" >
|
||||
INSERT INTO xxl_job_machine ( machine_ip, machine_id, add_time, heart_last_time)
|
||||
VALUES ( #{xxlJobMachine.machineIp}, #{xxlJobMachine.machineId}, #{xxlJobMachine.addTime,jdbcType=TIMESTAMP}, #{xxlJobMachine.heartLastTime,jdbcType=TIMESTAMP})
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="object" >
|
||||
UPDATE xxl_job_machine
|
||||
SET heart_last_time = #{heartLastTime,jdbcType=TIMESTAMP}
|
||||
WHERE machine_ip = #{machineIp}
|
||||
</update>
|
||||
|
||||
<select id="selectMaxMachineId" resultType="Integer">
|
||||
SELECT MAX(machine_id) FROM xxl_job_machine
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
Reference in new issue