马府强
2 years ago
14 changed files with 784 additions and 0 deletions
@ -0,0 +1,27 @@ |
|||||
|
# Created by .ignore support plugin (hsz.mobi) |
||||
|
### Java template |
||||
|
# Compiled class file |
||||
|
*.class |
||||
|
|
||||
|
# Log file |
||||
|
*.log |
||||
|
|
||||
|
# BlueJ files |
||||
|
*.ctxt |
||||
|
|
||||
|
# Mobile Tools for Java (J2ME) |
||||
|
.mtj.tmp/ |
||||
|
|
||||
|
# Package Files # |
||||
|
*.jar |
||||
|
*.war |
||||
|
*.nar |
||||
|
*.ear |
||||
|
*.zip |
||||
|
*.tar.gz |
||||
|
*.rar |
||||
|
|
||||
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml |
||||
|
hs_err_pid* |
||||
|
/.idea/ |
||||
|
/target/ |
@ -0,0 +1,92 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" version="4"> |
||||
|
<component name="FacetManager"> |
||||
|
<facet type="Spring" name="Spring"> |
||||
|
<configuration /> |
||||
|
</facet> |
||||
|
<facet type="web" name="Web"> |
||||
|
<configuration> |
||||
|
<webroots /> |
||||
|
<sourceRoots> |
||||
|
<root url="file://$MODULE_DIR$/src/main/java" /> |
||||
|
<root url="file://$MODULE_DIR$/src/main/resources" /> |
||||
|
</sourceRoots> |
||||
|
</configuration> |
||||
|
</facet> |
||||
|
</component> |
||||
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> |
||||
|
<output url="file://$MODULE_DIR$/target/classes" /> |
||||
|
<output-test url="file://$MODULE_DIR$/target/test-classes" /> |
||||
|
<content url="file://$MODULE_DIR$"> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> |
||||
|
<excludeFolder url="file://$MODULE_DIR$/target" /> |
||||
|
</content> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.13.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.13.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.26" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.43" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.glassfish:jakarta.el:3.0.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.43" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.16.1" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.vintage:junit-vintage-engine:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.3.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.20" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.20" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.3.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.18" level="project" /> |
||||
|
</component> |
||||
|
</module> |
@ -0,0 +1,136 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
<parent> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||
|
<version>2.3.9.RELEASE</version> |
||||
|
<relativePath/> <!-- lookup parent from repository --> |
||||
|
</parent> |
||||
|
|
||||
|
<groupId>com.ayi</groupId> |
||||
|
<artifactId>woniu-web</artifactId> |
||||
|
<version>0.0.1-SNAPSHOT</version> |
||||
|
<name>woniou-web</name> |
||||
|
<description>create by woniu</description> |
||||
|
|
||||
|
<packaging>jar</packaging> |
||||
|
|
||||
|
<properties> |
||||
|
<java.version>1.8</java.version> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>com.google.guava</groupId> |
||||
|
<artifactId>guava</artifactId> |
||||
|
<version>30.0-jre</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.assertj</groupId> |
||||
|
<artifactId>assertj-core</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-validation</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- spring web --> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.freemarker</groupId> |
||||
|
<artifactId>freemarker</artifactId> |
||||
|
<version>2.3.31</version> |
||||
|
</dependency> |
||||
|
<!-- swagger 因为mybatisPlus代码生成器会自带swagger的注解 --> |
||||
|
<dependency> |
||||
|
<groupId>io.springfox</groupId> |
||||
|
<artifactId>springfox-swagger2</artifactId> |
||||
|
<version>2.7.0</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>io.springfox</groupId> |
||||
|
<artifactId>springfox-swagger-ui</artifactId> |
||||
|
<version>2.7.0</version> |
||||
|
</dependency> |
||||
|
<!-- 启动后加载配置文件 --> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-configuration-processor</artifactId> |
||||
|
<optional>true</optional> |
||||
|
</dependency> |
||||
|
<!-- lombok 简化实体类管理工具--> |
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<optional>true</optional> |
||||
|
</dependency> |
||||
|
<!-- fastjson 解析json用到,也可以换成自己喜欢用的 --> |
||||
|
<dependency> |
||||
|
<groupId>com.alibaba</groupId> |
||||
|
<artifactId>fastjson</artifactId> |
||||
|
<version>1.2.79</version> |
||||
|
</dependency> |
||||
|
<!-- 测试 --> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<!-- aop切面 --> |
||||
|
<dependency> |
||||
|
<groupId>org.aspectj</groupId> |
||||
|
<artifactId>aspectjweaver</artifactId> |
||||
|
<version>1.9.5</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- ip2region --> |
||||
|
<dependency> |
||||
|
<groupId>org.lionsoul</groupId> |
||||
|
<artifactId>ip2region</artifactId> |
||||
|
<version>2.6.3</version> |
||||
|
</dependency> |
||||
|
<!-- 用于读取ip2region.xdb文件使用 --> |
||||
|
<dependency> |
||||
|
<groupId>commons-io</groupId> |
||||
|
<artifactId>commons-io</artifactId> |
||||
|
<version>2.6</version> |
||||
|
</dependency> |
||||
|
|
||||
|
|
||||
|
</dependencies> |
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,16 @@ |
|||||
|
package com.woniu; |
||||
|
|
||||
|
import org.springframework.boot.SpringApplication; |
||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
|
||||
|
|
||||
|
@SpringBootApplication |
||||
|
public class WoNiuApplication { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
SpringApplication.run(WoNiuApplication.class, args); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package com.woniu.service.controller; |
||||
|
|
||||
|
import com.woniu.service.ipregion.Ip; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
/** |
||||
|
* SpringBoot整合ip2region实现使用ip监控用户访问城市 |
||||
|
* 公众号:程序员蜗牛g |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("/test") |
||||
|
public class TestController { |
||||
|
|
||||
|
@GetMapping("/hello") |
||||
|
@Ip |
||||
|
public String hello() { |
||||
|
return "hello"; |
||||
|
} |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package com.woniu.service.ipregion; |
||||
|
|
||||
|
import java.lang.annotation.ElementType; |
||||
|
import java.lang.annotation.Retention; |
||||
|
import java.lang.annotation.RetentionPolicy; |
||||
|
import java.lang.annotation.Target; |
||||
|
|
||||
|
/** |
||||
|
* @className: Ip |
||||
|
* @author: woniu 公众号:程序员蜗牛g |
||||
|
* @date: 2023/4/5 |
||||
|
**/ |
||||
|
@Target(ElementType.METHOD) |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
public @interface Ip { |
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
package com.woniu.service.ipregion; |
||||
|
|
||||
|
import com.woniu.service.util.AddressUtil; |
||||
|
import com.woniu.service.util.HttpContextUtil; |
||||
|
import com.woniu.service.util.IPUtil; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||
|
import org.aspectj.lang.annotation.Around; |
||||
|
import org.aspectj.lang.annotation.Aspect; |
||||
|
import org.aspectj.lang.annotation.Pointcut; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.text.MessageFormat; |
||||
|
|
||||
|
@Aspect |
||||
|
@Component |
||||
|
@Slf4j |
||||
|
public class IpAspect { |
||||
|
|
||||
|
|
||||
|
@Pointcut("@annotation(com.woniu.service.ipregion.Ip)") |
||||
|
public void pointcut() { |
||||
|
} |
||||
|
|
||||
|
@Around("pointcut()") |
||||
|
public Object doAround(ProceedingJoinPoint point) throws Throwable { |
||||
|
HttpServletRequest request = HttpContextUtil.getHttpServletRequest(); |
||||
|
String ip = IPUtil.getIpAddr(request); |
||||
|
log.info(MessageFormat.format("当前IP为:[{0}];当前IP地址解析出来的地址为:[{1}]", ip, AddressUtil.getCityInfo(ip))); |
||||
|
return point.proceed(); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package com.woniu.service.util; |
||||
|
|
||||
|
import org.lionsoul.ip2region.xdb.Searcher; |
||||
|
|
||||
|
public class AddressUtil { |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 根据IP地址查询登录来源 |
||||
|
* |
||||
|
* @param ip |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getCityInfo(String ip) { |
||||
|
try { |
||||
|
Searcher searcher = Searcher.newWithFileOnly("ip2region/ip2region.xdb"); |
||||
|
//开始查询
|
||||
|
return searcher.searchByStr(ip); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
//默认返回空字符串
|
||||
|
return ""; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
//204.16.111.255
|
||||
|
System.out.println(getCityInfo("204.16.111.255")); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
package com.woniu.service.util; |
||||
|
|
||||
|
|
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
/** |
||||
|
* @desc 全局获取HttpServletRequest、HttpServletResponse |
||||
|
* 公众号:程序员蜗牛g |
||||
|
*/ |
||||
|
public class HttpContextUtil { |
||||
|
|
||||
|
private HttpContextUtil() { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public static HttpServletRequest getHttpServletRequest() { |
||||
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
||||
|
} |
||||
|
|
||||
|
public static HttpServletResponse getHttpServletResponse() { |
||||
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
package com.woniu.service.util; |
||||
|
|
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
|
||||
|
/** |
||||
|
* @desc 查询当前访问的IP地址 |
||||
|
*/ |
||||
|
public class IPUtil { |
||||
|
|
||||
|
private static final String UNKNOWN = "unknown"; |
||||
|
|
||||
|
protected IPUtil() { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取 IP地址 |
||||
|
* 使用 Nginx等反向代理软件, 则不能通过 request.getRemoteAddr()获取 IP地址 |
||||
|
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址, |
||||
|
* X-Forwarded-For中第一个非 unknown的有效IP字符串,则为真实IP地址 |
||||
|
*/ |
||||
|
public static String getIpAddr(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.getRemoteAddr(); |
||||
|
} |
||||
|
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; |
||||
|
} |
||||
|
|
||||
|
} |
Binary file not shown.
@ -0,0 +1,51 @@ |
|||||
|
package com.woniu; |
||||
|
|
||||
|
import org.apache.commons.io.FileUtils; |
||||
|
import org.lionsoul.ip2region.xdb.Searcher; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.text.MessageFormat; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
public class AddressUtil { |
||||
|
|
||||
|
/** |
||||
|
* 当前记录地址的本地DB |
||||
|
*/ |
||||
|
private static final String TEMP_FILE_DIR = "/home/admin/app/"; |
||||
|
|
||||
|
/** |
||||
|
* 根据IP地址查询登录来源 |
||||
|
* |
||||
|
* @param ip |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getCityInfo(String ip) { |
||||
|
try { |
||||
|
// 获取当前记录地址位置的文件
|
||||
|
String dbPath = Objects.requireNonNull(AddressUtil.class.getResource("/ip2region/ip2region.xdb")).getPath(); |
||||
|
File file = new File(dbPath); |
||||
|
//如果当前文件不存在,则从缓存中复制一份
|
||||
|
if (!file.exists()) { |
||||
|
dbPath = TEMP_FILE_DIR + "ip.db"; |
||||
|
System.out.println(MessageFormat.format("当前目录为:[{0}]", dbPath)); |
||||
|
file = new File(dbPath); |
||||
|
FileUtils.copyInputStreamToFile(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("ip2region/ip2region.xdb")), file); |
||||
|
} |
||||
|
//创建查询对象
|
||||
|
Searcher searcher = Searcher.newWithFileOnly(dbPath); |
||||
|
//开始查询
|
||||
|
return searcher.searchByStr(ip); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
//默认返回空字符串
|
||||
|
return ""; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
System.out.println(getCityInfo("1.2.3.4")); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,171 @@ |
|||||
|
package com.woniu; |
||||
|
|
||||
|
import com.google.common.collect.Lists; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.junit.Test; |
||||
|
import org.junit.runner.RunWith; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
import org.springframework.util.CollectionUtils; |
||||
|
|
||||
|
import java.util.*; |
||||
|
import java.util.concurrent.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 性能优化-如何爽玩多线程来开发?场景实战! |
||||
|
*/ |
||||
|
@SpringBootTest |
||||
|
@RunWith(SpringRunner.class) |
||||
|
@Slf4j |
||||
|
public class TestA { |
||||
|
|
||||
|
ThreadPoolExecutor executor = new ThreadPoolExecutor( |
||||
|
20, |
||||
|
50, |
||||
|
5, |
||||
|
TimeUnit.SECONDS, |
||||
|
new ArrayBlockingQueue<>(200), |
||||
|
Executors.defaultThreadFactory(), |
||||
|
new ThreadPoolExecutor.CallerRunsPolicy() |
||||
|
); |
||||
|
|
||||
|
/** |
||||
|
* 并行聚合处理数据! |
||||
|
* 主要运用CompletableFuture.allOf()方法, |
||||
|
* 将原本串行的操作改为并行。本案例相对比较常规 |
||||
|
*/ |
||||
|
@Test |
||||
|
public void threadDemo1() throws Exception { |
||||
|
|
||||
|
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> { |
||||
|
//逻辑A
|
||||
|
log.info("A"); |
||||
|
return "A"; |
||||
|
}, executor); |
||||
|
|
||||
|
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> { |
||||
|
//逻辑B
|
||||
|
log.info("B"); |
||||
|
return "B"; |
||||
|
}, executor); |
||||
|
|
||||
|
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> { |
||||
|
//逻辑C
|
||||
|
log.info("C"); |
||||
|
return "C"; |
||||
|
}, executor); |
||||
|
|
||||
|
CompletableFuture.allOf(future1, future2, future3) |
||||
|
.handle((result, exception) -> { |
||||
|
if (exception != null) { |
||||
|
log.error("处理失败:{}", exception); |
||||
|
} else { |
||||
|
//等待三个任务执行完 接着处理这个逻辑
|
||||
|
|
||||
|
} |
||||
|
return null; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 修改for循环为并行操作 |
||||
|
* <p> |
||||
|
* 这里借鉴了parallelStream流的思路,将串行的for循环分割成多个集合后,对分割后的集合进行循环。这应该是最普遍的多线程应用场景了 |
||||
|
*/ |
||||
|
@Test |
||||
|
public void threadDemo2() throws Exception { |
||||
|
|
||||
|
List<String> slAddList = new ArrayList<>(50000); |
||||
|
slAddList.addAll(Arrays.asList("1", "2", "3", "4", "5", "6")); |
||||
|
|
||||
|
List<List<String>> partition = Lists.partition(slAddList, 2); |
||||
|
|
||||
|
CompletableFuture.allOf(partition.stream().map(partitionList -> CompletableFuture.runAsync(() -> { |
||||
|
log.info("处理逻辑partitionList"); |
||||
|
}, executor) |
||||
|
).toArray(CompletableFuture[]::new)) |
||||
|
.whenComplete((res, e) -> |
||||
|
{ |
||||
|
if (e != null) { |
||||
|
log.error("多线程处理数据失败", e); |
||||
|
} else { |
||||
|
try { |
||||
|
log.info("进一步处理循环后的结果"); |
||||
|
} catch (Exception ex) { |
||||
|
log.error("处理失败", ex); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 修改Map遍历为并行操作 |
||||
|
*/ |
||||
|
@Test |
||||
|
public void threadDemo3() throws Exception { |
||||
|
//代码示例
|
||||
|
Map<String, List<String>> materialMap = new HashMap<String, List<String>>() { |
||||
|
{ |
||||
|
put("name", Arrays.asList("a","b","c")); |
||||
|
put("age", Arrays.asList("1","2","3")); |
||||
|
put("city", Arrays.asList("nj","bj","cz")); |
||||
|
put("province", Arrays.asList("js","zj","hn")); |
||||
|
put("love", Arrays.asList("bk","jk","bk")); |
||||
|
} |
||||
|
}; |
||||
|
List<Map<String, List<String>>> mapList = mapSplit(materialMap, 2); |
||||
|
CompletableFuture<Void> handle = |
||||
|
CompletableFuture.allOf(mapList.stream().map(splitMap -> CompletableFuture.runAsync(() -> { |
||||
|
splitMap.forEach((identity, list) -> { |
||||
|
log.info("identity:{},list:{}",identity,list); |
||||
|
}); |
||||
|
}, executor)).toArray(CompletableFuture[]::new)) |
||||
|
.exceptionally(e -> { |
||||
|
log.error("多线程组装数据失败", e); |
||||
|
return null; |
||||
|
}); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 将map切成段--工具类 |
||||
|
* |
||||
|
* @param splitMap 被切段的map |
||||
|
* @param splitNum 每段的大小 |
||||
|
*/ |
||||
|
public static <k, v> List<Map<k, v>> mapSplit(Map<k, v> splitMap, int splitNum) { |
||||
|
if (splitMap == null || splitNum <= 0) { |
||||
|
List<Map<k, v>> list = new ArrayList<>(); |
||||
|
list.add(splitMap); |
||||
|
return list; |
||||
|
} |
||||
|
Set<k> keySet = splitMap.keySet(); |
||||
|
Iterator<k> iterator = keySet.iterator(); |
||||
|
int i = 1; |
||||
|
List<Map<k, v>> total = new ArrayList<>(); |
||||
|
Map<k, v> tem = new HashMap<>(); |
||||
|
while (iterator.hasNext()) { |
||||
|
k next = iterator.next(); |
||||
|
tem.put(next, splitMap.get(next)); |
||||
|
if (i == splitNum) { |
||||
|
total.add(tem); |
||||
|
tem = new HashMap<>(); |
||||
|
i = 0; |
||||
|
} |
||||
|
i++; |
||||
|
} |
||||
|
if (!CollectionUtils.isEmpty(tem)) { |
||||
|
total.add(tem); |
||||
|
} |
||||
|
return total; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,124 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> |
||||
|
<component name="FacetManager"> |
||||
|
<facet type="Spring" name="Spring"> |
||||
|
<configuration /> |
||||
|
</facet> |
||||
|
<facet type="web" name="Web"> |
||||
|
<configuration> |
||||
|
<webroots /> |
||||
|
<sourceRoots> |
||||
|
<root url="file://$MODULE_DIR$/src/main/java" /> |
||||
|
<root url="file://$MODULE_DIR$/src/main/resources" /> |
||||
|
</sourceRoots> |
||||
|
</configuration> |
||||
|
</facet> |
||||
|
</component> |
||||
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> |
||||
|
<output url="file://$MODULE_DIR$/target/classes" /> |
||||
|
<output-test url="file://$MODULE_DIR$/target/test-classes" /> |
||||
|
<content url="file://$MODULE_DIR$"> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> |
||||
|
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> |
||||
|
<excludeFolder url="file://$MODULE_DIR$/target" /> |
||||
|
</content> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.13.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.13.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.26" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.43" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.43" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.vintage:junit-vintage-engine:5.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.3.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.10.20" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.20" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.3.3" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.13.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.google.guava:guava:30.0-jre" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.google.guava:failureaccess:1.0.1" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:3.5.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.3.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.18" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.assertj:assertj-core:3.16.1" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-validation:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.glassfish:jakarta.el:3.0.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.1.7.Final" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: jakarta.validation:jakarta.validation-api:2.0.2" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.4.1.Final" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.freemarker:freemarker:2.3.31" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-swagger2:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.swagger:swagger-annotations:1.5.13" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.swagger:swagger-models:1.5.13" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.4" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-spi:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-core:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-schema:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-swagger-common:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-spring-web:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.11" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.5.1" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.plugin:spring-plugin-metadata:1.2.0.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.mapstruct:mapstruct:1.1.0.Final" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: io.springfox:springfox-swagger-ui:2.7.0" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-configuration-processor:2.3.9.RELEASE" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.79" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.5" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: org.lionsoul:ip2region:2.6.3" level="project" /> |
||||
|
<orderEntry type="library" name="Maven: commons-io:commons-io:2.6" level="project" /> |
||||
|
</component> |
||||
|
</module> |
Loading…
Reference in new issue