diff --git a/pig-common/src/main/java/com/github/pig/common/constant/CommonConstant.java b/pig-common/src/main/java/com/github/pig/common/constant/CommonConstant.java index 18657d42..46b0b5e4 100644 --- a/pig-common/src/main/java/com/github/pig/common/constant/CommonConstant.java +++ b/pig-common/src/main/java/com/github/pig/common/constant/CommonConstant.java @@ -42,10 +42,6 @@ public interface CommonConstant { * 按钮 */ String BUTTON = "1"; - /** - * log rabbit队列名称 - */ - String LOG_QUEUE = "log"; /** * 删除标记 @@ -57,5 +53,8 @@ public interface CommonConstant { */ String UTF8 = "UTF-8"; + /** + * JSON 资源 + */ String CONTENT_TYPE = "application/json; charset=utf-8"; } diff --git a/pig-common/src/main/java/com/github/pig/common/constant/MqQueueConstant.java b/pig-common/src/main/java/com/github/pig/common/constant/MqQueueConstant.java new file mode 100644 index 00000000..7400d4ec --- /dev/null +++ b/pig-common/src/main/java/com/github/pig/common/constant/MqQueueConstant.java @@ -0,0 +1,18 @@ +package com.github.pig.common.constant; + +/** + * @author lengleng + * @date 2018/1/15 + * MQ 消息队列 + */ +public interface MqQueueConstant { + /** + * log rabbit队列名称 + */ + String LOG_QUEUE = "log"; + + /** + * 发送短信验证码队列 + */ + String MOBILE_CODE_QUEUE = "mobile_code_queue"; +} diff --git a/pig-common/src/main/java/com/github/pig/common/util/template/MobileMsgTemplate.java b/pig-common/src/main/java/com/github/pig/common/util/template/MobileMsgTemplate.java new file mode 100644 index 00000000..961292f8 --- /dev/null +++ b/pig-common/src/main/java/com/github/pig/common/util/template/MobileMsgTemplate.java @@ -0,0 +1,40 @@ +package com.github.pig.common.util.template; + +import java.io.Serializable; + +/** + * @author lengleng + * @date 2018/1/15 + * 短信消息模板 + */ +public class MobileMsgTemplate implements Serializable{ + /** + * 手机号 + */ + private String mobile; + /** + * 文本 + */ + private String text; + + public MobileMsgTemplate(String mobile, String text) { + this.mobile = mobile; + this.text = text; + } + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java b/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java index ae71be51..39a95ada 100644 --- a/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java +++ b/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java @@ -2,6 +2,7 @@ package com.github.pig.gateway.service.impl; import com.alibaba.fastjson.JSONObject; import com.github.pig.common.constant.CommonConstant; +import com.github.pig.common.constant.MqQueueConstant; import com.github.pig.common.entity.SysLog; import com.github.pig.common.util.UserUtils; import com.github.pig.common.vo.ErrorPojo; @@ -104,7 +105,7 @@ public class LogSendServiceImpl implements LogSendService { logVo.setSysLog(log); if (StringUtils.isNotEmpty(request.getHeader(CommonConstant.REQ_HEADER))) { logVo.setToken(request.getHeader(CommonConstant.REQ_HEADER)); - rabbitTemplate.convertAndSend(CommonConstant.LOG_QUEUE, logVo); + rabbitTemplate.convertAndSend(MqQueueConstant.LOG_QUEUE, logVo); } } } diff --git a/pig-modules/pig-mc-service/pom.xml b/pig-modules/pig-mc-service/pom.xml new file mode 100644 index 00000000..4c74d3bc --- /dev/null +++ b/pig-modules/pig-mc-service/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.github.pig + pig-mc-service + ${pig.version} + jar + + pig-mc-service + 消息中心 + + + com.github.pig + pig-modules + ${pig.version} + + + + + com.github.pig + pig-common + ${pig.version} + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + ${project.name} + + + + + + diff --git a/pig-modules/pig-mc-service/src/main/java/com/github/pig/mc/PigMessageCenterApplication.java b/pig-modules/pig-mc-service/src/main/java/com/github/pig/mc/PigMessageCenterApplication.java new file mode 100644 index 00000000..31b56c65 --- /dev/null +++ b/pig-modules/pig-mc-service/src/main/java/com/github/pig/mc/PigMessageCenterApplication.java @@ -0,0 +1,20 @@ +package com.github.pig.mc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author lengleng + * @date 2018年01月15日13:24:23 + * 消息中心 + */ +@EnableDiscoveryClient +@SpringBootApplication +public class PigMessageCenterApplication { + + public static void main(String[] args) { + SpringApplication.run(PigMessageCenterApplication.class, args); + } + +} diff --git a/pig-modules/pig-mc-service/src/main/java/com/github/pig/mc/listener/MobileCodeReceiveListener.java b/pig-modules/pig-mc-service/src/main/java/com/github/pig/mc/listener/MobileCodeReceiveListener.java new file mode 100644 index 00000000..7e4b285a --- /dev/null +++ b/pig-modules/pig-mc-service/src/main/java/com/github/pig/mc/listener/MobileCodeReceiveListener.java @@ -0,0 +1,25 @@ +package com.github.pig.mc.listener; + +import com.github.pig.common.constant.MqQueueConstant; +import com.github.pig.common.util.template.MobileMsgTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +/** + * @author lengleng + * @date 2018年01月15日13:51:53 + * 监听短信发送请求 + */ +@Component +@RabbitListener(queues = MqQueueConstant.MOBILE_CODE_QUEUE) +public class MobileCodeReceiveListener { + private static final Logger logger = LoggerFactory.getLogger(MobileCodeReceiveListener.class); + + @RabbitHandler + public void receive(MobileMsgTemplate mobileMsgTemplate) { + logger.info("消息中心接收到短信发送请求-> 手机号:{} -> 验证码: {} ", mobileMsgTemplate.getMobile(), mobileMsgTemplate.getText()); + } +} diff --git a/pig-modules/pig-mc-service/src/main/resources/bootstrap.yml b/pig-modules/pig-mc-service/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..b46dee61 --- /dev/null +++ b/pig-modules/pig-mc-service/src/main/resources/bootstrap.yml @@ -0,0 +1,32 @@ +spring: + application: + name: pig-mc-service + profiles: + active: dev + cloud: + config: + fail-fast: true + discovery: + service-id: pig-config-server + enabled: true + profile: ${spring.profiles.active} + label: ${spring.profiles.active} +--- +spring: + profiles: dev +eureka: + instance: + prefer-ip-address: true + client: + serviceUrl: + defaultZone: http://pig:gip6666@localhost:1025/eureka + +--- +spring: + profiles: prd +eureka: + instance: + prefer-ip-address: true + client: + serviceUrl: + defaultZone: http://pig:gip6666@pig-eureka:1025/eureka \ No newline at end of file diff --git a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/config/RabbitConfig.java b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/config/RabbitConfig.java index 55e85d20..d50cfd8a 100644 --- a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/config/RabbitConfig.java +++ b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/config/RabbitConfig.java @@ -1,6 +1,6 @@ package com.github.pig.admin.common.config; -import com.github.pig.common.constant.CommonConstant; +import com.github.pig.common.constant.MqQueueConstant; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -14,10 +14,21 @@ import org.springframework.context.annotation.Configuration; public class RabbitConfig { /** * 初始化 log队列 + * * @return */ @Bean public Queue initLogQueue() { - return new Queue(CommonConstant.LOG_QUEUE); + return new Queue(MqQueueConstant.LOG_QUEUE); + } + + /** + * 初始化 手机验证码通道 + * + * @return + */ + @Bean + public Queue initMobileCodeQueue() { + return new Queue(MqQueueConstant.MOBILE_CODE_QUEUE); } } diff --git a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java index fbb72bf1..3d20a370 100644 --- a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java +++ b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java @@ -1,11 +1,10 @@ package com.github.pig.admin.common.listener; import com.github.pig.admin.service.SysLogService; -import com.github.pig.common.constant.CommonConstant; +import com.github.pig.common.constant.MqQueueConstant; import com.github.pig.common.entity.SysLog; import com.github.pig.common.util.UserUtils; import com.github.pig.common.vo.LogVo; -import org.apache.commons.lang.StringUtils; import org.slf4j.MDC; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; @@ -17,7 +16,7 @@ import org.springframework.stereotype.Component; * @date 2017/11/17 */ @Component -@RabbitListener(queues = CommonConstant.LOG_QUEUE) +@RabbitListener(queues = MqQueueConstant.LOG_QUEUE) public class LogReceiveListener { private static final String KEY_USER = "user"; diff --git a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java index f31310bc..05460937 100644 --- a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java +++ b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java @@ -11,13 +11,16 @@ import com.github.pig.admin.mapper.SysUserMapper; import com.github.pig.admin.service.SysMenuService; import com.github.pig.admin.service.SysUserRoleService; import com.github.pig.admin.service.UserService; +import com.github.pig.common.constant.MqQueueConstant; import com.github.pig.common.constant.SecurityConstants; import com.github.pig.common.util.Query; +import com.github.pig.common.util.template.MobileMsgTemplate; import com.github.pig.common.vo.SysRole; import com.github.pig.common.vo.UserVo; import com.xiaoleilu.hutool.util.RandomUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; @@ -47,6 +50,8 @@ public class UserServiceImpl extends ServiceImpl impleme @Autowired private SysUserMapper sysUserMapper; @Autowired + private RabbitTemplate rabbitTemplate; + @Autowired private SysUserRoleService sysUserRoleService; @Override @@ -111,7 +116,7 @@ public class UserServiceImpl extends ServiceImpl impleme *

* 1. 先去redis 查询是否 60S内已经发送 * 2. 未发送: 产生4位数字 手机号-验证码 - * 3. 调用短信网关发送信息 + * 3. 发往消息中心-》发送信息 * 4. 保存redis * * @param mobile 手机号 @@ -123,7 +128,8 @@ public class UserServiceImpl extends ServiceImpl impleme boolean result = false; if (tempCode == null) { String code = RandomUtil.randomNumbers(4); - logger.info("短信发送成功 -> 手机号:{} -> 验证码:{}", mobile, code); + logger.info("短信发送请求消息中心 -> 手机号:{} -> 验证码:{}", mobile, code); + rabbitTemplate.convertAndSend(MqQueueConstant.MOBILE_CODE_QUEUE,new MobileMsgTemplate(mobile,code)); redisTemplate.opsForValue().set(SecurityConstants.DEFAULT_CODE_KEY + mobile, code, SecurityConstants.DEFAULT_IMAGE_EXPIRE, TimeUnit.SECONDS); result = true; } diff --git a/pig-modules/pom.xml b/pig-modules/pom.xml index 3557e318..890e6f88 100644 --- a/pig-modules/pom.xml +++ b/pig-modules/pom.xml @@ -20,6 +20,7 @@ + pig-mc-service pig-upms-service