Maven

  1. <dependency>
  2. <groupId>com.github.hiwepy</groupId>
  3. <artifactId>ip2region-spring-boot-starter</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>

Sample

  1. import java.io.IOException;
  2. import javax.annotation.PostConstruct;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. @EnableIP2region
  7. @SpringBootApplication
  8. public class Application {
  9. @Autowired
  10. IP2regionTemplate template;
  11. @PostConstruct
  12. public void test() throws IOException {
  13. System.out.println(template.binarySearch("127.0.0.1"));
  14. System.out.println(template.binarySearch("127.0.0.1"));
  15. }
  16. public static void main(String[] args) throws Exception {
  17. SpringApplication.run(Application.class, args);
  18. }
  19. }

如果使用外部IP数据,可自定义配置,参考如下:

  1. ip2region:
  2. external: false
  3. index-block-size: 4096
  4. total-header-size: 8192
  5. location: classpath:ip2region/ip2region.db

参考资料