在开发中经常会遇到通过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;
- }
-
- }
-