mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-23 17:55:44 +08:00
'pageadd'
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.xboe.module.idconfig;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.xboe.common.utils.IDGenerator;
|
||||
import com.xboe.common.utils.MD5Util;
|
||||
import com.xboe.core.SysConstant;
|
||||
import com.xboe.module.idconfig.entity.IPMapping;
|
||||
import com.xboe.module.idconfig.service.IIPMappingService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* ip映射自动配置
|
||||
* @author seastar
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class IdGeneratorAutoConfig {
|
||||
|
||||
@Resource
|
||||
IIPMappingService ipMappingService;
|
||||
|
||||
public void init() throws Exception{
|
||||
|
||||
String ip=getLinuxLocalIp();
|
||||
String appName=SysConstant.getConfigValue("spring.application.name");
|
||||
if(StringUtils.isBlank(appName)) {
|
||||
appName=IDGenerator.randomString(8);//随机生成8位字符串
|
||||
}
|
||||
log.info("读取的应用名称:"+appName);
|
||||
String md5=MD5Util.MD5Encode(ip+appName);
|
||||
IPMapping ipm = ipMappingService.get(md5);
|
||||
|
||||
|
||||
Integer workServerId=null;
|
||||
Integer dataCenterId=null;
|
||||
if(ipm!=null) {
|
||||
workServerId=ipm.getWorkNum();
|
||||
dataCenterId=ipm.getDcNum();
|
||||
}else {
|
||||
log.warn("无IP【"+ip+"】的配置的workNum和DataCenterNum,系统自动生成随机数");
|
||||
workServerId=RandomUtils.nextInt(0,31);
|
||||
dataCenterId=RandomUtils.nextInt(0,31);
|
||||
ipm=new IPMapping();
|
||||
ipm.setId(md5);
|
||||
ipm.setIp(ip);
|
||||
ipm.setDcNum(dataCenterId);
|
||||
ipm.setWorkNum(workServerId);
|
||||
ipm.setAppName(appName);
|
||||
ipMappingService.save(ipm);
|
||||
}
|
||||
IDGenerator.init(workServerId,dataCenterId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String getLinuxLocalIp() throws SocketException {
|
||||
String ip = "";
|
||||
try {
|
||||
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
|
||||
NetworkInterface intf = en.nextElement();
|
||||
//String name = intf.getName();
|
||||
//if (!name.contains("docker") && !name.contains("lo")){
|
||||
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
|
||||
InetAddress inetAddress = enumIpAddr.nextElement();
|
||||
if (!inetAddress.isLoopbackAddress()) {
|
||||
String ipaddress = inetAddress.getHostAddress().toString();
|
||||
if (!ipaddress.contains(":") && !ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
|
||||
ip = ipaddress;
|
||||
//log.error("获取的ip:"+ipaddress);
|
||||
}
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
}catch (SocketException ex) {
|
||||
ip = "127.0.0.1";
|
||||
//log.error("获取ip地址异常",ex);
|
||||
}
|
||||
//log.error("获取的实际的ip:"+ip);
|
||||
return ip;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.xboe.module.idconfig.dao;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.xboe.core.orm.BaseDao;
|
||||
import com.xboe.module.idconfig.entity.IPMapping;
|
||||
|
||||
@Repository
|
||||
public class IPMappingDao extends BaseDao<IPMapping>{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.xboe.module.idconfig.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import com.xboe.core.SysConstant;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = SysConstant.TABLE_PRE + "ipapp")
|
||||
public class IPMapping {
|
||||
|
||||
@Id
|
||||
@Column(name = "id", length = 32)
|
||||
private String id;
|
||||
|
||||
@Column(name = "ip", length = 40)
|
||||
private String ip;
|
||||
|
||||
@Column(name = "app_name", length = 50)
|
||||
private String appName;
|
||||
|
||||
@Column(name = "work_num", length = 2)
|
||||
private Integer workNum;
|
||||
|
||||
@Column(name = "dc_num", length = 2)
|
||||
private Integer dcNum;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.xboe.module.idconfig.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.xboe.module.idconfig.entity.IPMapping;
|
||||
|
||||
public interface IIPMappingService {
|
||||
|
||||
IPMapping get(String id);
|
||||
|
||||
void save(IPMapping ipm);
|
||||
|
||||
List<IPMapping> getAll();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.xboe.module.idconfig.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.xboe.module.idconfig.dao.IPMappingDao;
|
||||
import com.xboe.module.idconfig.entity.IPMapping;
|
||||
import com.xboe.module.idconfig.service.IIPMappingService;
|
||||
|
||||
@Service
|
||||
public class IPMappingServiceImpl implements IIPMappingService{
|
||||
|
||||
@Autowired
|
||||
IPMappingDao dao;
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void save(IPMapping ipm) {
|
||||
dao.saveOrUpdate(ipm);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public List<IPMapping> getAll() {
|
||||
return dao.getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPMapping get(String id) {
|
||||
return dao.get(id);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user