From 520567a1005b48cedafb0409a38be58393dd77e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Sat, 8 Apr 2023 10:01:59 +0800 Subject: [PATCH] =?UTF-8?q?Spring=E4=BA=8B=E4=BB=B6=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E5=AE=9E=E7=8E=B0=E4=B8=9A=E5=8A=A1=E8=A7=A3?= =?UTF-8?q?=E8=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springeventqudong/.gitignore | 27 ++++ springeventqudong/fanfu-web.iml | 92 ++++++++++++ springeventqudong/pom.xml | 136 ++++++++++++++++++ .../main/java/com/woniu/WoNiuApplication.java | 16 +++ .../service/controller/TestController.java | 48 +++++++ .../com/woniu/service/event/BizEvent.java | 63 ++++++++ .../woniu/service/event/BizEventListener.java | 25 ++++ .../service/event/CouponsHandlerListener.java | 22 +++ .../com/woniu/service/event/EventEngine.java | 15 ++ .../service/event/EventEngineConfig.java | 44 ++++++ .../woniu/service/event/EventEngineImpl.java | 76 ++++++++++ .../woniu/service/event/EventEngineTopic.java | 23 +++ .../woniu/service/event/EventSubscriber.java | 32 +++++ .../event/MembershipHandlerListener.java | 20 +++ .../service/event/MsgHandlerListener.java | 22 +++ .../java/com/woniu/service/ipregion/Ip.java | 16 +++ .../com/woniu/service/ipregion/IpAspect.java | 34 +++++ .../com/woniu/service/util/AddressUtil.java | 31 ++++ .../woniu/service/util/HttpContextUtil.java | 28 ++++ .../java/com/woniu/service/util/IPUtil.java | 37 +++++ springeventqudong/woniu-web.iml | 124 ++++++++++++++++ 21 files changed, 931 insertions(+) create mode 100644 springeventqudong/.gitignore create mode 100644 springeventqudong/fanfu-web.iml create mode 100644 springeventqudong/pom.xml create mode 100644 springeventqudong/src/main/java/com/woniu/WoNiuApplication.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/controller/TestController.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/BizEvent.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/BizEventListener.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/CouponsHandlerListener.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/EventEngine.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/EventEngineConfig.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/EventEngineImpl.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/EventEngineTopic.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/EventSubscriber.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/MembershipHandlerListener.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/event/MsgHandlerListener.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/ipregion/Ip.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/ipregion/IpAspect.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/util/AddressUtil.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/util/HttpContextUtil.java create mode 100644 springeventqudong/src/main/java/com/woniu/service/util/IPUtil.java create mode 100644 springeventqudong/woniu-web.iml diff --git a/springeventqudong/.gitignore b/springeventqudong/.gitignore new file mode 100644 index 0000000..682d4ed --- /dev/null +++ b/springeventqudong/.gitignore @@ -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/ diff --git a/springeventqudong/fanfu-web.iml b/springeventqudong/fanfu-web.iml new file mode 100644 index 0000000..4fb2f38 --- /dev/null +++ b/springeventqudong/fanfu-web.iml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/springeventqudong/pom.xml b/springeventqudong/pom.xml new file mode 100644 index 0000000..2276085 --- /dev/null +++ b/springeventqudong/pom.xml @@ -0,0 +1,136 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.9.RELEASE + + + + com.ayi + woniu-web + 0.0.1-SNAPSHOT + woniou-web + create by woniu + + jar + + + 1.8 + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.google.guava + guava + 30.0-jre + + + org.projectlombok + lombok + + + org.assertj + assertj-core + + + + org.springframework.boot + spring-boot-starter-validation + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.freemarker + freemarker + 2.3.31 + + + + io.springfox + springfox-swagger2 + 2.7.0 + + + io.springfox + springfox-swagger-ui + 2.7.0 + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.projectlombok + lombok + true + + + + com.alibaba + fastjson + 1.2.79 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.aspectj + aspectjweaver + 1.9.5 + + + + + org.lionsoul + ip2region + 2.6.3 + + + + commons-io + commons-io + 2.6 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/springeventqudong/src/main/java/com/woniu/WoNiuApplication.java b/springeventqudong/src/main/java/com/woniu/WoNiuApplication.java new file mode 100644 index 0000000..a5ca290 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/WoNiuApplication.java @@ -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); + } + + + +} diff --git a/springeventqudong/src/main/java/com/woniu/service/controller/TestController.java b/springeventqudong/src/main/java/com/woniu/service/controller/TestController.java new file mode 100644 index 0000000..fcf3591 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/controller/TestController.java @@ -0,0 +1,48 @@ +package com.woniu.service.controller; + +import com.woniu.service.event.BizEvent; +import com.woniu.service.event.EventEngine; +import com.woniu.service.event.EventEngineTopic; +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * 基于Spring事件驱动模式实现业务解耦 + */ +@RestController +@RequestMapping("/test") +public class TestController { + + @Resource(name = "eventEngineJob") + private EventEngine eventEngine; + + @GetMapping("/doRegisterVip") + public String doRegisterVip(@RequestParam(required = true) String userName, + @RequestParam(required = true) Integer age) { + Map paramMap = new HashMap<>(16); + paramMap.put("userName", userName); + paramMap.put("age", age); + //1、注册会员,这里不实现了 + System.out.println("注册会员成功"); + //2、入会员群 + eventEngine.publishEvent( + new BizEvent(EventEngineTopic.JOIN_MEMBERSHIP_GROUP, UUID.randomUUID().toString(), paramMap)); + //3、发优惠券 + eventEngine.publishEvent( + new BizEvent(EventEngineTopic.ISSUE_COUPONS, UUID.randomUUID().toString(), paramMap)); + //4、推送消息 + eventEngine.publishEvent( + new BizEvent(EventEngineTopic.SEND_WELCOME_MSG, UUID.randomUUID().toString(), paramMap)); + return "注册会员成功"; + } + + +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/event/BizEvent.java b/springeventqudong/src/main/java/com/woniu/service/event/BizEvent.java new file mode 100644 index 0000000..1095d09 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/BizEvent.java @@ -0,0 +1,63 @@ +package com.woniu.service.event; + +import java.util.EventObject; + +/** + * 业务事件 + */ +public class BizEvent extends EventObject { + + /** + * Topic + */ + private final String topic; + + /** + * 业务id + */ + private final String bizId; + + /** + * 数据 + */ + private final Object data; + + /** + * @param topic 事件topic,用于区分事件类型 + * @param bizId 业务ID,标识这一次的调用 + * @param data 事件传输对象 + */ + public BizEvent(String topic, String bizId, Object data) { + super(data); + this.topic = topic; + this.bizId = bizId; + this.data = data; + } + + /** + * Getter method for property topic. + * + * @return property value of topic + */ + public String getTopic() { + return topic; + } + + /** + * Getter method for property id. + * + * @return property value of id + */ + public String getBizId() { + return bizId; + } + + /** + * Getter method for property data. + * + * @return property value of data + */ + public Object getData() { + return data; + } +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/event/BizEventListener.java b/springeventqudong/src/main/java/com/woniu/service/event/BizEventListener.java new file mode 100644 index 0000000..a3bfe60 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/BizEventListener.java @@ -0,0 +1,25 @@ +package com.woniu.service.event; + + +import java.util.EventListener; + +/** + * 业务事件监听器 + */ +public interface BizEventListener extends EventListener { + + /** + * 是否执行事件 + * + * @param event 事件 + * @return + */ + public boolean decide(BizEvent event); + + /** + * 执行事件 + * + * @param event 事件 + */ + public void onEvent(BizEvent event); +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/event/CouponsHandlerListener.java b/springeventqudong/src/main/java/com/woniu/service/event/CouponsHandlerListener.java new file mode 100644 index 0000000..b1c925f --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/CouponsHandlerListener.java @@ -0,0 +1,22 @@ +package com.woniu.service.event; + + + +import org.springframework.stereotype.Component; + +/** + * 优惠券处理器 + */ +@Component +public class CouponsHandlerListener implements BizEventListener { + + @Override + public boolean decide(BizEvent event) { + return true; + } + + @Override + public void onEvent(BizEvent event) { + System.out.println("优惠券处理器:十折优惠券已发放"); + } +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/event/EventEngine.java b/springeventqudong/src/main/java/com/woniu/service/event/EventEngine.java new file mode 100644 index 0000000..6a5f8b6 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/EventEngine.java @@ -0,0 +1,15 @@ +package com.woniu.service.event; + +/** + * @className: EventEngine + * @author: woniu + * @date: 2023/4/8 + **/ +public interface EventEngine { + /** + * 发送事件 + * + * @param event 事件 + */ + void publishEvent(BizEvent event); +} diff --git a/springeventqudong/src/main/java/com/woniu/service/event/EventEngineConfig.java b/springeventqudong/src/main/java/com/woniu/service/event/EventEngineConfig.java new file mode 100644 index 0000000..73f4783 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/EventEngineConfig.java @@ -0,0 +1,44 @@ +package com.woniu.service.event; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +@Configuration +public class EventEngineConfig { + + /** + * 线程池异步处理事件 + */ + private static final Executor EXECUTOR = new ThreadPoolExecutor(20, 50, 10, TimeUnit.MINUTES, + new LinkedBlockingQueue(500), new CustomizableThreadFactory("EVENT_ENGINE_POOL")); + + @Bean("eventEngineJob") + public EventEngine initJobEngine(CouponsHandlerListener couponsHandlerListener, + MembershipHandlerListener membershipHandlerListener, + MsgHandlerListener msgHandlerListener) { + Map> bizEvenListenerMap = new HashMap<>(); + //注册优惠券事件 + bizEvenListenerMap.put(EventEngineTopic.ISSUE_COUPONS, Arrays.asList(couponsHandlerListener)); + //注册会员群事件 + bizEvenListenerMap.put(EventEngineTopic.JOIN_MEMBERSHIP_GROUP, Arrays.asList(membershipHandlerListener)); + //注册消息推送事件 + bizEvenListenerMap.put(EventEngineTopic.SEND_WELCOME_MSG, Arrays.asList(msgHandlerListener)); + + EventEngineImpl eventEngine = new EventEngineImpl(); + eventEngine.setBizSubscribers(bizEvenListenerMap); + eventEngine.setAsync(true); + eventEngine.setBizListenerExecutor(EXECUTOR); + return eventEngine; + } + +} diff --git a/springeventqudong/src/main/java/com/woniu/service/event/EventEngineImpl.java b/springeventqudong/src/main/java/com/woniu/service/event/EventEngineImpl.java new file mode 100644 index 0000000..e4d745a --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/EventEngineImpl.java @@ -0,0 +1,76 @@ +package com.woniu.service.event; + + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; + +import org.springframework.util.CollectionUtils; + +/** + * 事件引擎实现类 + */ +public class EventEngineImpl implements EventEngine { + + /** + * 异步执行器。也系统需要自行定义线程池 + */ + private Executor bizListenerExecutor; + + /** + * 是否异步,默认为false + */ + private boolean async; + + /** + * 订阅端 KEY是TOPIC,VALUES是监听器集合 + */ + private Map> bizSubscribers = new HashMap<>(16); + + @Override + public void publishEvent(BizEvent event) { + List listeners = bizSubscribers.get(event.getTopic()); + if (CollectionUtils.isEmpty(listeners)) { + return; + } + for (BizEventListener bizEventListener : listeners) { + if (bizEventListener.decide(event)) { + //异步执行的话,放入线程池 + if (async) { + bizListenerExecutor.execute(new EventSubscriber(bizEventListener, event)); + } else { + bizEventListener.onEvent(event); + } + + } + } + } + + /** + * Setter method for property bizListenerExecutor. + * + * @param bizListenerExecutor value to be assigned to property bizListenerExecutor + */ + public void setBizListenerExecutor(Executor bizListenerExecutor) { + this.bizListenerExecutor = bizListenerExecutor; + } + + /** + * Setter method for property bizSubscribers. + * + * @param bizSubscribers value to be assigned to property bizSubscribers + */ + public void setBizSubscribers(Map> bizSubscribers) { + this.bizSubscribers = bizSubscribers; + } + + /** + * Setter method for property async. + * + * @param async value to be assigned to property async + */ + public void setAsync(boolean async) { + this.async = async; + } +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/event/EventEngineTopic.java b/springeventqudong/src/main/java/com/woniu/service/event/EventEngineTopic.java new file mode 100644 index 0000000..02c46bd --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/EventEngineTopic.java @@ -0,0 +1,23 @@ +package com.woniu.service.event; + + +/** + * 事件引擎topic,用于区分事件类型 + */ +public class EventEngineTopic { + /** + * 入会员群 + */ + public static final String JOIN_MEMBERSHIP_GROUP = "joinMembershipGroup"; + + /** + * 发优惠券 + */ + public static final String ISSUE_COUPONS = "issueCoupons"; + + /** + * 推送消息 + */ + public static final String SEND_WELCOME_MSG = "sendWelcomeMsg"; + +} diff --git a/springeventqudong/src/main/java/com/woniu/service/event/EventSubscriber.java b/springeventqudong/src/main/java/com/woniu/service/event/EventSubscriber.java new file mode 100644 index 0000000..81c0f1b --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/EventSubscriber.java @@ -0,0 +1,32 @@ +package com.woniu.service.event; + +/** + * 事件监听者。注意:此时已经没有线程上下文,如果需要请修改构造函数,显示复制上下文信息 + */ +public class EventSubscriber implements Runnable { + + /** + * 业务监听器 + **/ + private BizEventListener bizEventListener; + + /** + * 业务事件 + */ + private BizEvent bizEvent; + + /** + * @param bizEventListener 事件监听者 + * @param bizEvent 事件 + */ + public EventSubscriber(BizEventListener bizEventListener, BizEvent bizEvent) { + super(); + this.bizEventListener = bizEventListener; + this.bizEvent = bizEvent; + } + + @Override + public void run() { + bizEventListener.onEvent(bizEvent); + } +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/event/MembershipHandlerListener.java b/springeventqudong/src/main/java/com/woniu/service/event/MembershipHandlerListener.java new file mode 100644 index 0000000..76445e4 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/MembershipHandlerListener.java @@ -0,0 +1,20 @@ +package com.woniu.service.event; + + +import org.springframework.stereotype.Component; + +/** + * 会员群处理器 + */ +@Component +public class MembershipHandlerListener implements BizEventListener { + @Override + public boolean decide(BizEvent event) { + return true; + } + + @Override + public void onEvent(BizEvent event) { + System.out.println("会员群处理器:您已成功加入会员群"); + } +} diff --git a/springeventqudong/src/main/java/com/woniu/service/event/MsgHandlerListener.java b/springeventqudong/src/main/java/com/woniu/service/event/MsgHandlerListener.java new file mode 100644 index 0000000..79ff857 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/event/MsgHandlerListener.java @@ -0,0 +1,22 @@ +package com.woniu.service.event; + + + +import org.springframework.stereotype.Component; + +/** + * 消息推送处理器 + */ +@Component +public class MsgHandlerListener implements BizEventListener { + + @Override + public boolean decide(BizEvent event) { + return true; + } + + @Override + public void onEvent(BizEvent event) { + System.out.println("消息推送处理器:欢迎成为会员!!!"); + } +} diff --git a/springeventqudong/src/main/java/com/woniu/service/ipregion/Ip.java b/springeventqudong/src/main/java/com/woniu/service/ipregion/Ip.java new file mode 100644 index 0000000..aa2bca2 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/ipregion/Ip.java @@ -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 { +} diff --git a/springeventqudong/src/main/java/com/woniu/service/ipregion/IpAspect.java b/springeventqudong/src/main/java/com/woniu/service/ipregion/IpAspect.java new file mode 100644 index 0000000..5dc18a7 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/ipregion/IpAspect.java @@ -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(); + } + +} diff --git a/springeventqudong/src/main/java/com/woniu/service/util/AddressUtil.java b/springeventqudong/src/main/java/com/woniu/service/util/AddressUtil.java new file mode 100644 index 0000000..4af40e6 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/util/AddressUtil.java @@ -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")); + } + +} \ No newline at end of file diff --git a/springeventqudong/src/main/java/com/woniu/service/util/HttpContextUtil.java b/springeventqudong/src/main/java/com/woniu/service/util/HttpContextUtil.java new file mode 100644 index 0000000..501cfc6 --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/util/HttpContextUtil.java @@ -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(); + } +} diff --git a/springeventqudong/src/main/java/com/woniu/service/util/IPUtil.java b/springeventqudong/src/main/java/com/woniu/service/util/IPUtil.java new file mode 100644 index 0000000..96f91dc --- /dev/null +++ b/springeventqudong/src/main/java/com/woniu/service/util/IPUtil.java @@ -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; + } + +} diff --git a/springeventqudong/woniu-web.iml b/springeventqudong/woniu-web.iml new file mode 100644 index 0000000..6e18898 --- /dev/null +++ b/springeventqudong/woniu-web.iml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file