Springboot集成ip2region离线IP地名映射

简介: Java集成解析IP地址为行政区域

title: Springboot集成ip2region离线IP地名映射
date: 2020-12-16 11:15:34
categories: springboot

description: Springboot集成ip2region离线IP地名映射

springboot

1. 背景

前段时间因业务需要,客户提出分析外网访问的IP,即针对一些热点区域的IP访问想做到事后预警与分析。

因为我们服务是运行在相对隔离的资源环境中,无法直接去请求外网,于是想到用离线的方式来处理从网关发来的数据。找了下,看到个开源项目

使用起来相对成本较低,本着先用满足需求再说,虽然在性能上并不能满足海量IP的分析,还有提升的空间。

看作者的例子,较为简单,也不多说。先看下我的目录结构吧

war3-infi
+---src
|   +---main
|   |   +---java
|   |   \---resources
|   |       +---generator
|   |       +---ipdb
|   |   |   |   +---ip2region.db
|   |       +---mapper
|   |       +---application.yml

2. 集成

在上述的样例中,我们将项目 Clone 到本地。

  • ip2region.db 这是需要下载,下载地址,这里提供Csv、Txt、Db文件三种格式,根据需要自行选择。
  • ipdb这是我放db库文件的路径,当然可以自定义,只需要在application.yml 中配置即可。
  • application.yml 这个就不多说啦。
server:
  port: 9090

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379

ip2region:
  external: false
  index-block-size: 4096
  total-header-size: 8192
  location: classpath:ipdb/ip2region.db

例子如下,RegionAddressDataBlock 两种结果返回封装。


package xyz.wongs.drunkard.war3.web.controller;


import com.github.hiwepy.ip2region.spring.boot.IP2regionTemplate;
import com.github.hiwepy.ip2region.spring.boot.ext.RegionAddress;
import lombok.extern.slf4j.Slf4j;
import org.nutz.plugins.ip2region.DataBlock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.wongs.drunkard.base.aop.annotion.ApplicationLog;
import xyz.wongs.drunkard.base.message.annoation.ResponseResult;
import xyz.wongs.drunkard.base.message.exception.DrunkardException;
import xyz.wongs.drunkard.war3.limit.RequestLimit;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName IndexController
 * @Description 
 * @author WCNGS@QQ.COM
 * @Github <a>https://github.com/rothschil</a>
 * @date 20/11/18 11:00
 * @Version 1.0.0
*/
@Slf4j
@RestController
@ResponseResult
public class IndexController {

    @Autowired
    IP2regionTemplate template;

    /** 根据输入IP地址,返回解析后的地址
     * @Description
     * @param ip
     * @return xyz.wongs.drunkard.base.message.response.ResponseResult
     * @throws
     * @date 2020/8/17 18:26
     */
    @GetMapping(value = "/convert/{ip}")
    public DataBlock convertDataBlock(@PathVariable String ip){
        DataBlock dataBlock = null;
        try {
            dataBlock = template.binarySearch(ip);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataBlock;
    }

    /** 根据输入IP地址,返回解析后的地址
     * @Description
     * @param ip
     * @return xyz.wongs.drunkard.base.message.response.ResponseResult
     * @throws
     * @date 2020/8/17 18:26
     */
    @RequestLimit(maxCount=3)
    @GetMapping(value = "/region/{ip}")
    public RegionAddress convert(@PathVariable String ip){
        RegionAddress regionAddress = null;
        try {
            regionAddress = template.getRegionAddress(ip);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return regionAddress;
    }

    @GetMapping(value = "/region/ip={ip}")
    public RegionAddress caseInsensitive(@PathVariable String ip){
        RegionAddress regionAddress = null;
        try {
            regionAddress = template.getRegionAddress(ip);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return regionAddress;
    }

}

3. 打开浏览器

访问 http://localhost:9090/region/ip=109.27.45.12 这是我之前一个例子,用来解析IP地址,获取地域信息的。

样例响应

4. 源码地址,如果觉得对你有帮助,请Star

觉得对你有帮助,请Star

Github源码地址

Gitee源码地址

目录
相关文章
|
23天前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
124 6
|
24天前
|
数据采集 数据安全/隐私保护
Kameleo指纹浏览器进阶使用:轻松集成IPXProxy海外代理IP
Kameleo是一款出色的指纹浏览器,它能够帮助用户实现隐身浏览。大家在进行网络抓取的时候总会碰到一些阻碍,而采取指纹浏览器可以提升网络抓取的效率,并且集成代理IP能增加一层防护,让数据采集更加全面,为制定营销策略提供更好的支持。那如何将Kameleo指纹浏览器与IPXProxy海外代理IP集成?
|
1月前
|
Java 关系型数据库 MySQL
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
144 4
|
1月前
|
安全
全面掌握Dolphin指纹浏览器:IPXProxy代理IP集成使用指南
对于需要管理多个社交媒体或电商平台的用户而言,Dolphin指纹浏览器提供了便捷的多账号隔离功能,搭配代理IP可以有效的防止账户关联。并且Dolphin指纹浏览器能够支持团队协作,方便团队进行互动、跟踪账户、查看代理状态。一些刚接触指纹浏览器的用户,不知道如何将Dolphin指纹浏览器和代理IP集成使用,下面以IPXProxy代理IP为例,带来详细的操作步骤。
|
1月前
|
消息中间件 Java 测试技术
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
113 1
|
2月前
|
消息中间件 Java Kafka
springboot集成kafka
springboot集成kafka
94 2
|
2月前
|
Java 应用服务中间件 API
springboot+nginx获取真实IP
springboot+nginx获取真实IP
38 4
|
1月前
|
消息中间件 Java Kafka
Spring Boot与Apache Kafka Streams的集成
Spring Boot与Apache Kafka Streams的集成
若依修改,集成mybatisplus报错,若依集成mybatisplus,总是找不到映射是怎么回事只要是用mp的方法就找报,改成mybatisPlus配置一定要改
若依修改,集成mybatisplus报错,若依集成mybatisplus,总是找不到映射是怎么回事只要是用mp的方法就找报,改成mybatisPlus配置一定要改
|
1月前
|
容器
springboot-自定义注解拦截ip aop和ioc
springboot-自定义注解拦截ip aop和ioc