7

实现一个简单的java版本高性能获取ip地址所属国家工具

 2 years ago
source link: https://lichuanyang.top/posts/36780/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

实现一个简单的java版本高性能获取ip地址所属国家工具

发表于 2021-10-12

| 0

| 阅读次数: 12

一个非常简单且性能优秀的的工具,用于获取某个ip地址的所属国家。

ip地址库的数据可以从http://download.ip2location.com/lite/ 获取。可以写一个简单的定时任务,定期去拉最新的数据。由于ip地址的更新不是非常频繁,因此一个月左右更新一次就足够了。

```
...
16781312,JP
16785408,CN
16793600,JP
...
```

数据示例如上,每行数据是一个ip地址的段,包括段的起始地址和对应国家的代号。原始数据实际上还包含地址段的结束点,不过实际上我们拿到的数据都是连续的数据段,因此我们完全可以省略掉结束地址,以节约内存。

实现逻辑很简单,将数据全部导入到一个数组中,然后写一个二分查找就可以了。

项目整体使用springboot实现,可以直接运行springboot, 也可使用docker运行。

使用thymeleaf实现了一个极简版的页面。

访问 http://ip-country.lichuanyang.top/ , 简单体验工具效果。

项目源码在https://github.com/lcy362/ip-country, 如果觉得还不错,麻烦给一个star (^▽^)。

原文地址: http://lichuanyang.top/posts/36780/

wechat
订阅公众号

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK