离线版IP归属地查询

这里简单介绍一下Ip2regionSearcher是什么?

ip2region 是准确率 99.9% 的 IP 地址定位库,0.0x毫秒级查询,提供了 Java、PHP、C、Python、Node.js、Golang、C#、Rust、Lua的查询绑定和 Binary、B树、内存三种查询算法!

Setp1:导入依赖

java 复制代码
<dependency>
    <groupId>net.dreamlu</groupId>
    <artifactId>mica-ip2region</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.11</version>
</dependency>

提示:Ip2regionSearcher的和hutool工具包依赖包,这里导入hutool为了后期利用获取IP工具类

Setp2:编写测试接口

java 复制代码
import cn.hutool.extra.servlet.ServletUtil;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 测试控制器
 * <p>
 *
 * @className: XController
 * @author: BertXie
 * @create: 2023-01-09 11:40
 */

@RestController
@RequiredArgsConstructor
public class XController {

    private final Ip2regionSearcher searchSearcher;

    @GetMapping("/ip")
    public String getHomeLocation(HttpServletRequest request) {
        IpInfo ipInfo = searchSearcher.memorySearch(ServletUtil.getClientIP(request));
        return ipInfo != null ? ipInfo.getCity() : "未知星球";
    }

}

注意:这里 注入了Ip2regionSearcher 对象,并 使用了memorySearch方法 ,会 返回一个IP详情对象 ,还用到了 Hutool工具 的获取IP的方法 ServletUtil.getClientIP

测试效果图

提示:这里我是写的接口,你也可以直接写一个测试类,填入IP即可

相关推荐
YMWM_1 小时前
UDP协议详解:从原理到Python实践
网络·网络协议·udp
pengyi8710151 小时前
共享 IP 与独享 IP 怎么选?被封后升级方案避坑
网络·网络协议·tcp/ip
YuanDaima20481 小时前
Linux 进阶运维与 AI 环境实战:进程管理、网络排错与 GPU 监控
linux·运维·服务器·网络·人工智能
凯勒姆3 小时前
网工网络设备原理及配置
网络·智能路由器
上海云盾-小余3 小时前
网站恶意爬虫拦截策略:智能识别与封禁实操方案
网络·爬虫·安全·web安全
xhbh6664 小时前
网关端口映射和路由器端口转发有什么区别?配置要点全解析
运维·服务器·网络·智能路由器·端口映射·映射·无痕网关
半壶清水4 小时前
用P4 Tutorial、BMv2 和 Mininet‌解析网络第一集------模拟环境搭建
运维·服务器·网络·网络协议·tcp/ip
高翔·权衡之境4 小时前
主题10:实时性——硬实时与软实时
服务器·网络·驱动开发·信息与通信·智能硬件
BullSmall4 小时前
Promtheus和Alertmanager 之间是通过管理平面还是业务层面IP交互
网络协议·tcp/ip·平面
黄筱筱筱筱筱筱筱5 小时前
交换综合实验
网络