IP归属地查询系统
-
通过 HttpServletRequest 对象,获取用户的 「IP」 地址 -
通过 IP 地址,获取对应的省份、城市
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
/**
* 常用获取客户端信息的工具
*/
public class NetworkUtil {
/**
* 获取ip地址
*/
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader(“x-forwarded-for”);
if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getHeader(“Proxy-Client-IP”);
}
if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getHeader(“WL-Proxy-Client-IP”);
}
if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getHeader(“HTTP_CLIENT_IP”);
}
if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getHeader(“HTTP_X_FORWARDED_FOR”);
}
if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
// 本机访问
if (“localhost”.equalsIgnoreCase(ip) || “127.0.0.1”.equalsIgnoreCase(ip) || “0:0:0:0:0:0:0:1”.equalsIgnoreCase(ip)){
// 根据网卡取本机配置的IP
InetAddress inet;
try {
inet = InetAddress.getLocalHost();
ip = inet.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照’,’分割
if (null != ip && ip.length() > 15) {
if (ip.indexOf(“,”) > 15) {
ip = ip.substring(0, ip.indexOf(“,”));
}
}
return ip;
}
/**
* 获取mac地址
*/
public static String getMacAddress() throws Exception {
// 取mac地址
byte[] macAddressBytes = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
// 下面代码是把mac地址拼装成String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < macAddressBytes.length; i++) {
if (i != 0) {
sb.append(“-“);
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(macAddressBytes[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
return sb.toString().trim().toUpperCase();
}
}





-
01,>80%,淘宝IP地址库,http://ip.taobao.com/%5C
-
02,≈10%,GeoIP,https://geoip.com/%5C
-
03,≈2%,纯真 IP 库,http://www.cz88.net/%5C
②多查询客户端的支持

-
vIndex 索引缓存:使用固定的 512KiB 的内存空间缓存 vector index 数据,减少一次 IO 磁盘操作,保持平均查询效率稳定在 10-20 微秒之间。 -
xdb 整个文件缓存:将整个 xdb 文件全部加载到内存,内存占用等同于 xdb 文件大小,无磁盘 IO 操作,保持微秒级别的查询效率。
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.6.4</version>
</dependency>
import java.io.*;
import java.util.concurrent.TimeUnit;
public class SearcherTest {
public static void main(String[] args) {
// 1、创建 searcher 对象
String dbPath = “ip2region.xdb file path”;
Searcher searcher = null;
try {
searcher = Searcher.newWithFileOnly(dbPath);
} catch (IOException e) {
System.out.printf(“failed to create searcher with `%s`: %s\n”, dbPath, e);
return;
}
// 2、查询
try {
String ip = “1.2.3.4”;
long sTime = System.nanoTime();
String region = searcher.search(ip);
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() – sTime));
System.out.printf(“{region: %s, ioCount: %d, took: %d μs}\n”, region, searcher.getIOCount(), cost);
} catch (Exception e) {
System.out.printf(“failed to search(%s): %s\n”, ip, e);
}
// 3、备注:并发使用,每个线程需要创建一个独立的 searcher 对象单独使用。
}
}
import java.io.*;
import java.util.concurrent.TimeUnit;
public class SearcherTest {
public static void main(String[] args) {
String dbPath = “ip2region.xdb file path”;
// 1、从 dbPath 中预先加载 VectorIndex 缓存,并且把这个得到的数据作为全局变量,后续反复使用。
byte[] vIndex;
try {
vIndex = Searcher.loadVectorIndexFromFile(dbPath);
} catch (Exception e) {
System.out.printf(“failed to load vector index from `%s`: %s\n”, dbPath, e);
return;
}
// 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
Searcher searcher;
try {
searcher = Searcher.newWithVectorIndex(dbPath, vIndex);
} catch (Exception e) {
System.out.printf(“failed to create vectorIndex cached searcher with `%s`: %s\n”, dbPath, e);
return;
}
// 3、查询
try {
String ip = “1.2.3.4”;
long sTime = System.nanoTime();
String region = searcher.search(ip);
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() – sTime));
System.out.printf(“{region: %s, ioCount: %d, took: %d μs}\n”, region, searcher.getIOCount(), cost);
} catch (Exception e) {
System.out.printf(“failed to search(%s): %s\n”, ip, e);
}
// 备注:每个线程需要单独创建一个独立的 Searcher 对象,但是都共享全局的制度 vIndex 缓存。
}
}
import java.io.*;
import java.util.concurrent.TimeUnit;
public class SearcherTest {
public static void main(String[] args) {
String dbPath = “ip2region.xdb file path”;
// 1、从 dbPath 加载整个 xdb 到内存。
byte[] cBuff;
try {
cBuff = Searcher.loadContentFromFile(dbPath);
} catch (Exception e) {
System.out.printf(“failed to load content from `%s`: %s\n”, dbPath, e);
return;
}
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
Searcher searcher;
try {
searcher = Searcher.newWithBuffer(cBuff);
} catch (Exception e) {
System.out.printf(“failed to create content cached searcher: %s\n”, e);
return;
}
// 3、查询
try {
String ip = “1.2.3.4”;
long sTime = System.nanoTime();
String region = searcher.search(ip);
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() – sTime));
System.out.printf(“{region: %s, ioCount: %d, took: %d μs}\n”, region, searcher.getIOCount(), cost);
} catch (Exception e) {
System.out.printf(“failed to search(%s): %s\n”, ip, e);
}
// 备注:并发使用,用整个 xdb 数据缓存创建的查询对象可以安全的用于并发,也就是你可以把这个 searcher 对象做成全局对象去跨线程访问。
}
}


②编译测试程序
cd binding/java/
mvn compile package
③查询测试
java -jar ip2region-{version}.jar search [command options]
options:
–db string ip2region binary xdb file path
–cache-policy string cache policy: file/vectorIndex/content
ip2region xdb searcher test program, cachePolicy: vectorIndex
type ‘quit’ to exit
ip2region>> 1.2.3.4
{region: 美国|0|华盛顿|0|谷歌, ioCount: 7, took: 82 μs}
ip2region>>
④bench 测试
java -jar ip2region-{version}.jar bench [command options]
options:
–db string ip2region binary xdb file path
–src string source ip text file path
–cache-policy string cache policy: file/vectorIndex/content
Bench finished, {cachePolicy: vectorIndex, total: 3417955, took: 8s, cost: 2 μs/op}
微信赞赏
支付宝扫码领红包