ip:AreaUtils、IPUtils 优化

pull/2/head
YunaiV 2022-12-13 20:19:54 +08:00
parent 4a2cb33cd7
commit 40ce481e2e
3 changed files with 56 additions and 68 deletions

View File

@ -6,6 +6,7 @@ import cn.hutool.core.text.csv.CsvUtil;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.ip.core.Area; import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum; import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -17,19 +18,20 @@ import java.util.Map;
* *
* @author * @author
*/ */
@Slf4j
public class AreaUtils { public class AreaUtils {
/**
* SEARCHER
*/
@SuppressWarnings("InstantiationOfUtilityClass")
private final static AreaUtils INSTANCE = new AreaUtils();
private static Map<Integer, Area> areas; private static Map<Integer, Area> areas;
static { private AreaUtils() {
// 懒加载,使用时初始化 long now = System.currentTimeMillis();
init();
}
/**
*
*/
private static synchronized void init() {
areas = new HashMap<>(); areas = new HashMap<>();
areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0, areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0,
null, new ArrayList<>())); null, new ArrayList<>()));
@ -51,6 +53,7 @@ public class AreaUtils {
area.setParent(parent); area.setParent(parent);
parent.getChildren().add(area); parent.getChildren().add(area);
} }
log.info("启动加载 AreaUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
} }
/** /**

View File

@ -1,82 +1,74 @@
package cn.iocoder.yudao.framework.ip.core.utils; package cn.iocoder.yudao.framework.ip.core.utils;
import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.hutool.core.io.resource.ResourceUtil;
import cn.iocoder.yudao.framework.ip.core.Area; import cn.iocoder.yudao.framework.ip.core.Area;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher; import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException; import java.io.IOException;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/** /**
* IP * IP
* <p>
* ip2region.xdb<a href="https://gitee.com/zhijiantianya/ip2region"/>
* region
* *
* @author * IP ip2region.xdb <a href="https://gitee.com/zhijiantianya/ip2region"/>
*
* @author wanglhup
*/ */
@Slf4j @Slf4j
public class IPUtils { public class IPUtils {
/**
* ip
*
*/
private static Searcher SEARCHER;
/** /**
* SEARCHER * SEARCHER
*/ */
@SuppressWarnings("InstantiationOfUtilityClass")
private final static IPUtils INSTANCE = new IPUtils(); private final static IPUtils INSTANCE = new IPUtils();
/**
* IP
*/
private static Searcher SEARCHER;
/** /**
* *
*/ */
private IPUtils() { private IPUtils() {
String dbPath = "ip2region.xdb";
dbPath = IPUtils.class.getClassLoader().getResource(dbPath).getPath();
try { try {
SEARCHER = Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath)); long now = System.currentTimeMillis();
byte[] bytes = ResourceUtil.readBytes("ip2region.xdb");
SEARCHER = Searcher.newWithBuffer(bytes);
log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
} catch (IOException e) { } catch (IOException e) {
// 加载xdb文件异常,不影响启动 log.error("启动加载 IPUtils 失败", e);
log.error("启动加载IP SEARCH异常", e);
SEARCHER = null;
} }
} }
/** /**
* IPID127.0.0.1 * IP
* @param ip ip *
* @param ip IP 127.0.0.1
* @return id * @return id
*/ */
@SneakyThrows
public static Integer getAreaId(String ip) { public static Integer getAreaId(String ip) {
try {
return Integer.parseInt(SEARCHER.search(ip)); return Integer.parseInt(SEARCHER.search(ip));
} catch (Exception e) {
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
} }
/** /**
* IPID{@link Searcher#checkIP(String)} * IP
* @param ip ip *
* @return id * @param ip IP {@link Searcher#checkIP(String)}
* @return
*/ */
@SneakyThrows
public static Integer getAreaId(long ip) { public static Integer getAreaId(long ip) {
try {
return Integer.parseInt(SEARCHER.search(ip)); return Integer.parseInt(SEARCHER.search(ip));
} catch (Exception e) {
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
} }
/** /**
* IP127.0.0.1 * IP
* @param ip ip *
* @param ip IP 127.0.0.1
* @return * @return
*/ */
public static Area getArea(String ip) { public static Area getArea(String ip) {
@ -84,8 +76,9 @@ public class IPUtils {
} }
/** /**
* IP{@link Searcher#checkIP(String)} * IP
* @param ip ip *
* @param ip IP {@link Searcher#checkIP(String)}
* @return * @return
*/ */
public static Area getArea(long ip) { public static Area getArea(long ip) {

View File

@ -9,47 +9,39 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* {@link IPUtils} * {@link IPUtils}
*
* @author wanglhup
*/ */
class IPUtilsTest { public class IPUtilsTest {
@Test @Test
void getAreaId() { public void testGetAreaId_string() {
// 120.202.4.0|120.202.4.255|420600 // 120.202.4.0|120.202.4.255|420600
Integer areaId = IPUtils.getAreaId("120.202.4.50"); Integer areaId = IPUtils.getAreaId("120.202.4.50");
assertEquals(420600, areaId); assertEquals(420600, areaId);
} }
@Test @Test
void testGetAreaId() { public void testGetAreaId_long() throws Exception {
// 120.203.123.0|120.203.133.255|360900 // 120.203.123.0|120.203.133.255|360900
long ip = 0L; long ip = Searcher.checkIP("120.203.123.250");
try {
ip = Searcher.checkIP("120.203.123.250");
} catch (Exception e) {
// ignore
}
Integer areaId = IPUtils.getAreaId(ip); Integer areaId = IPUtils.getAreaId(ip);
assertEquals(360900, areaId); assertEquals(360900, areaId);
} }
@Test @Test
void getArea() { public void testGetArea_string() {
// 120.202.4.0|120.202.4.255|420600 // 120.202.4.0|120.202.4.255|420600
Area area = IPUtils.getArea("120.202.4.50"); Area area = IPUtils.getArea("120.202.4.50");
assertEquals("襄阳市", area.getName()); assertEquals("襄阳市", area.getName());
} }
@Test @Test
void testGetArea() { public void testGetArea_long() throws Exception {
// 120.203.123.0|120.203.133.255|360900 // 120.203.123.0|120.203.133.255|360900
long ip = 0L; long ip = Searcher.checkIP("120.203.123.252");
try {
ip = Searcher.checkIP("120.203.123.252");
} catch (Exception e) {
// ignore
}
Area area = IPUtils.getArea(ip); Area area = IPUtils.getArea(ip);
assertEquals("宜春市", area.getName()); assertEquals("宜春市", area.getName());
}
} }
}