根据IP地址查询Country、Region、City信息

543 阅读1分钟

一、摘要

项目中碰到需要根据IP查询地域信息,常见的提供服务的方式有HTTP接口和提供MMDB文件两种方式。

  • HTTP接口,这种方式很好调用,从服务提供方获取请求token,发起http请求即可。这种方式一般有调用次数限制,也容易被限流,且服务的QPS信息暴露给三方平台了。
  • MMDB文件,这种相当于是把全量数据以文件的方式提供给我们了,随便调用,比较方便。

二、MMDB解析示例

2.1 maxmind-db 1.2.0 版本

2.1.1 依赖引入

<dependency>
    <groupId>com.maxmind.db</groupId>
    <artifactId>maxmind-db</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.0</version>
</dependency>

2.1.2 代码示例

public class IpDemo {
    public static void main(String[] args) throws Exception {
        File database = new File("resources/ip.mmdb");
        Reader reader = new Reader(database);
        InetAddress address = InetAddress.getByName("24.24.24.24");
        JsonNode response = reader.get(address);
        System.out.println(response);
        reader.close();
    }
}

2.2 maxmind-db 2.0.0 版本

<dependency>
    <groupId>com.maxmind.db</groupId>
    <artifactId>maxmind-db</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.0</version>
</dependency>

代码示例

private static void printIpInfoGeoDetail(String ip) throws Exception {
    File database = new File(ipinfoPath);
    Reader reader = new Reader(database);
    InetAddress address = InetAddress.getByName(ip);
    IpInfoResult result = reader.get(address, IpInfoResult.class);
    System.out.println("Ipinfo:" + JsonUtils.toJsonHasNullKey(result));
}



@Data
public class IpInfoResult {
    private String country;
    private String lng;
    private String city;
    private String timezone;
    private String postal_code;
    private String region;
    private String lat;
    private String geoname_id;
    private String region_code;

    @MaxMindDbConstructor
    public IpInfoResult(
            @MaxMindDbParameter(name = "country") String country,
            @MaxMindDbParameter(name = "lng") String lng,
            @MaxMindDbParameter(name = "city") String city,
            @MaxMindDbParameter(name = "timezone") String timezone,
            @MaxMindDbParameter(name = "postal_code") String postal_code,
            @MaxMindDbParameter(name = "region") String region,
            @MaxMindDbParameter(name = "lat") String lat,
            @MaxMindDbParameter(name = "geoname_id") String geoname_id,
            @MaxMindDbParameter(name = "region_code") String region_code
    ) {
        this.country = country;
        this.lng = lng;
        this.city = city;
        this.timezone = timezone;
        this.postal_code = postal_code;
        this.region = region;
        this.lat = lat;
        this.geoname_id = geoname_id;
        this.region_code = region_code;
    }

}

三、附录

3.1 资源网址

常见提供IP地址查询地域信息的网站有:

ipgeolocation.io/

ipinfo.io/

ipregistry.co/

ipgeolocation.io/

www.maxmind.com/en/geoip-de…

3.2 参考地址

maxmind-db 1.2.0 its401.com/article/jed…

maxmind-db 2.0.0 maxmind.github.io/MaxMind-DB-…

GeoIp 示例 maxmind.github.io/GeoIP2-java…