Java调用MaxMind GeoIP库查询IP地理信息

本文详细介绍了GeoIP2和GeoLite2的安装、配置与使用方法,包括数据库文件的下载与更新,以及如何在Java环境中查询IP的地理位置信息。同时,文章还深入探讨了不同地图坐标系的区别及其在中国的应用规范。
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><
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值