mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	ip:AreaUtils、IPUtils 优化
This commit is contained in:
		| @@ -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); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|   | |||||||
| @@ -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搜索 |      * 初始化 SEARCHER | ||||||
|      * 启动加载到内存中 |  | ||||||
|      */ |  | ||||||
|     private static 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; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 查询IP对应的地区ID,格式应为127.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); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 查询IP对应的地区ID,格式参考{@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); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 查询IP对应的地区,格式应为127.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) { | ||||||
|   | |||||||
| @@ -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()); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV