天天看点

Java调用MaxMind GeoIP库查询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      
  • 通过yum来安装
yum install      
  • 修改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
[email protected]

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>
</dependency>      
package demo;

import java.io.File;
import java.net.InetAddress;

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.Subdivision;

public class GeoipTest {
  public static void main(String[] args) throws Exception {
    // GeoIP2-City 数据库文件mmdb
    File database = new File("D:\\Tools\\GeoIP\\db\\GeoLite2-City.mmdb");

    // 创建 DatabaseReader对象
    DatabaseReader reader = new DatabaseReader.Builder(database).build();

    // 设置地址是使用IPv4还是IPv6
//    System.setProperty("java.net.preferIPv4Stack", "true");
    System.setProperty("java.net.preferIPv6Addresses", "true");

    // 设置IP地址
//    InetAddress ipAddress = InetAddress.getByName("39.130.56.106");
    InetAddress ipAddress = InetAddress.getByName("240C::6666");

    // 获取查询结果
    CityResponse response = reader.city(ipAddress);

    // 获取国家信息
    Country country = response.getCountry();

    System.out.println("国家code:" + country.getIsoCode());
    System.out.println("国家:" + country.getNames().get("zh-CN"));

    // 获取省份
    Subdivision subdivision = response.getMostSpecificSubdivision();

    System.out.println("省份code:" + subdivision.getIsoCode());
    System.out.println("省份:" + subdivision.getNames().get("zh-CN"));

    // 城市
    City city = response.getCity();

    System.out.println("城市code:" + city.getGeoNameId());
    System.out.println("城市:" + city.getName());

    // 获取城市
    Location location = response.getLocation();
    // 这里获得的经纬度是WGS84坐标系统下的坐标
    System.out.println("经度:" + location.getLatitude());
    System.out.println("维度:" + location.getLongitude());
  }
}      

也可以在执行命令中设定:

java -Djava.net.preferIPv4Stack=true -cp .;classes/ my.main.className
或
java -Djava.net.preferIPv6Addresses=true -cp .;classes/ my.main.className      

对于Tomcat服务器,可以在 catalina.bat 或者 catalina.sh 中增加如下环境变量即可:

SET CATALINA_OPTS=-Djava.net.preferIPv4Stack=true
或
SET CATALINA_OPTS=-Djava.net.preferIPv6Addresses=true      

5. 关于地图坐标系

  • 我们常说的坐标系有哪些?

WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。

GCJ02:又称火星坐标系,是由中国国家测绘局制定的地理坐标系统,是由WGS84加密后得到的坐标系。

BD09:为百度坐标系,在GCJ02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标。

  • 百度地图使用什么坐标体系?

使用百度地图的服务,需使用BD09坐标。

若使用非BD09坐标、未经过坐标转换(非BD09转成BD09)直接叠加在地图上,地图展示位置会偏移,因此通过其他坐标(WGS84、GCJ02)调用服务时,需先将其他坐标转换为BD09。

  • 港澳台及海外,百度地图返回什么坐标?

中国地区(包括港澳台),百度地图开放平台的所有产品,都支持返回GCJ02坐标系、BD09坐标系。

海外地区,目前返回的是WGS84坐标。

  • 注意
  • 坐标系的转换