根据IP定位城市

在开发中经常会遇到通过IP来获取用户位置信息的场景,如根据位置做风控策略等等,Geolocation是由MaxMind公司开源的IP数据库,本次我们介绍该开源工具的简单使用。

步骤一: GeoLite2-City.mmdb是一款被MaxMind公司开发的开源Geolocation库,可以利用 IP 地址来获取其地理位置信息。这是一个免费版的数据库,它提供国家、省份、城市、经纬度等信息,我们可以使用它来做 IP 地址的地理定位。
在项目中增加MaxMind的数据库读取依赖。如果你使用的是Maven来构建项目,只需在pom.xml文件中加入以下依赖:

<dependency>
    <groupId>com.maxmind.geoip2</groupId>
    <artifactId>geoip2</artifactId>
    <version>2.15.0</version>
</dependency>

步骤二:准备GeoLite2城市数据库
下载GeoLite2-City.mmdb数据库文件。你可以直接从MaxMind的官方网站上下载这个数据库:https://dev.maxmind.com/geoip/geoip2/geolite2/

步骤三:使用DatabaseReader获取地理位置信息

接下来,我们需要使用DatabaseReader类来查询IP对应的地理位置信息。假设我们的GeoLite2-City.mmdb数据库文件路径为 geolite/GeoLite2-City.mmdb,以下是操作代码:

import java.io.InputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResourceLoader;

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 lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.UtilityClass;

@UtilityClass
public class GeoIpLocatorUtils {
    private static final Logger logger = LoggerFactory.getLogger(GeoIpLocatorUtils.class);
    private static final String GEO_LITE_CITY_PATH = "classpath:geolite/GeoLite2-City.mmdb";

    private static DatabaseReader reader;

    static {
        try {
            InputStream inputStream =
                    new FileSystemResourceLoader().getResource(GEO_LITE_CITY_PATH).getInputStream();
            reader = new DatabaseReader.Builder(inputStream).build();
        } catch (Exception e) {
            logger.error("Load reader database error.");
            throw new IllegalArgumentException();
        }
    }

    public static GeoLocation getLocation(String ipAddress) {
        try {
            InetAddress ip = InetAddress.getByName(ipAddress);
            Optional<CityResponse> optional = reader.tryCity(ip);
            if (optional.isPresent()) {
                CityResponse response = optional.get();
                City city = response.getCity();
                Country country = response.getCountry();
                Location location = response.getLocation();
                return GeoLocation.builder()
                        .countryIsoCode(country.getIsoCode())
                        .country(country.getName())
                        .city(city.getName())
                        .latitude(location.getLatitude())
                        .longitude(location.getLongitude())
                        .build();
            }
        } catch (Exception e) {
            logger.error("Geo ip get location error. ipAddress={}", ipAddress);
            return null;
        }
        return null;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class GeoLocation implements Serializable {
        private static final long serialVersionUID = -446831836931739094L;

        private String country;

        private String countryIsoCode;

        private String city;

        private Double latitude;

        private Double longitude;
    }

}

https://blog.csdn.net/qq_32508383/article/details/126443173

https://dev.maxmind.com/geoip/geolite2-free-geolocation-data

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值