mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-23 09:46:50 +08:00
'pageadd'
This commit is contained in:
103
modules/boe-module-idconfig/pom.xml
Normal file
103
modules/boe-module-idconfig/pom.xml
Normal file
@@ -0,0 +1,103 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.xboe</groupId>
|
||||
<artifactId>module</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<!--<relativePath>../..</relativePath> -->
|
||||
</parent>
|
||||
<artifactId>xboe-module-idconfig</artifactId>
|
||||
<name>xboe-module-idconfig</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>id生成的相关规则配置</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.xboe</groupId>
|
||||
<artifactId>xboe-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 以下几项和jdk有关 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jdk8</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-parameter-names</artifactId>
|
||||
</dependency>
|
||||
<!-- log -->
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-to-slf4j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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