离线版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即可

相关推荐
火车叼位3 小时前
Windows 双网关自动切换:Node.js + 计划任务实现旁路由优先
网络协议·程序员
小张小张爱学习4 小时前
网络编程高频面试题
网络
Shingmc35 小时前
【Linux】数据链路层
linux·服务器·网络
A hao5 小时前
IP65防护等级对户外LED显示屏意味着什么
网络
无心水5 小时前
【Hermes:进阶调优与性能优化】45、性能调优:降低延迟与 token 消耗的 7 个技巧 —— 让 Hermes 智能体跑得更快、花得更少
网络·性能优化·mcp协议·openclaw·养龙虾·hermes·honcho
小贾要学习6 小时前
【Linux】Linux高性能IO多路复用:epoll全方位详解(从原理到实战)
linux·服务器·网络
编程大师哥6 小时前
高效服务器管理工具 Xshell 8 下载安装配置设置详细教程
网络
想唱rap7 小时前
五种IO模型和非阻塞IO
linux·运维·服务器·网络·数据库·tcp/ip
Bruce_Liuxiaowei8 小时前
AI攻防时间差:当漏洞发现速度碾压修复速度— 聚焦技术核心
网络·人工智能·网络安全·ai·系统安全
方安乐8 小时前
交换机的自学机制
运维·服务器·网络