1. 概述
GeoIP 有收费版本 GeoIP2 和 GeoLite2 ,GeoLite2 为免费版本。
2. 安装GeoLite2数据库文件
- 下载RPM包安装
# centos版本
wget https://github.com/maxmind/geoipupdate/releases/download/v4.2.2/geoipupdate_4.2.2_linux_amd64.rpm
# windows版本
# wget https://github.com/maxmind/geoipupdate/releases/download/v4.2.2/geoipupdate_4.2.2_windows_amd64.zip
rpm -ivh geoipupdate_4.2.2_linux_amd64.rpm
- 通过yum来安装
yum install -y geoipupdate
- 修改GeoIP更新配置文件
vim /etc/GeoIP.conf
AccountID 0
LicenseKey 000000000000
EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country
或者到 https://www.maxmind.com/en/my_license_key 申请 AccountID 和 LicenseKey。
3. 更新GeoLite2数据库
具体帮助可参考 man geoipupdate
- 手动更新
geoipupdate -v
- 自动更新(利用crontab)
# top of crontab
MAILTO=your@email.com
30 2 * * 3#1 /usr/local/bin/geoipupdate
# end of crontab
在每个月第一个星期三自动执行 geoipupdate 命令,执行时间在凌晨2点30分。因为Maxmind是每月的第一个星期二更新IP库,所以我们选择延迟一点,避免时差引起误差。
- 更新的数据库文件的存放位置
可以使用 geoipupdate -d /path/to/db_file 指定数据库文件的存放位置。如果不指定则默认将数据库文件存放于 /usr/share/GeoIP。此外使用 -f 参数可以指定配置文件的位置。
4. 使用Java查询IP的地理信息
可参考官方MaxMind官方API:
- http://maxmind.github.io/GeoIP2-java/doc/v2.13.0/
- https://github.com/maxmind/GeoIP2-java
- http://maxmind.github.io/GeoIP2-java/
一个例子
maven导入jar包
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.13.0</version><

本文详细介绍了GeoIP2和GeoLite2的安装、配置与使用方法,包括数据库文件的下载与更新,以及如何在Java环境中查询IP的地理位置信息。同时,文章还深入探讨了不同地图坐标系的区别及其在中国的应用规范。
最低0.47元/天 解锁文章
1472

被折叠的 条评论
为什么被折叠?



