消息中心

This commit is contained in:
wangiegie@gmail.com 2018-01-15 14:31:26 +08:00
parent 04b05b3500
commit 39244f89d1
12 changed files with 209 additions and 12 deletions

View File

@ -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";
}

View File

@ -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";
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,45 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.pig</groupId>
<artifactId>pig-mc-service</artifactId>
<version>${pig.version}</version>
<packaging>jar</packaging>
<name>pig-mc-service</name>
<description>消息中心</description>
<parent>
<groupId>com.github.pig</groupId>
<artifactId>pig-modules</artifactId>
<version>${pig.version}</version>
</parent>
<dependencies>
<dependency>
<groupId>com.github.pig</groupId>
<artifactId>pig-common</artifactId>
<version>${pig.version}</version>
</dependency>
<!--rabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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";

View File

@ -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<SysUserMapper, SysUser> impleme
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private SysUserRoleService sysUserRoleService;
@Override
@ -111,7 +116,7 @@ public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impleme
* <p>
* 1. 先去redis 查询是否 60S内已经发送
* 2. 未发送 产生4位数字 手机号-验证码
* 3. 调用短信网关发送信息
* 3. 发往消息中心-发送信息
* 4. 保存redis
*
* @param mobile 手机号
@ -123,7 +128,8 @@ public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> 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;
}

View File

@ -20,6 +20,7 @@
</properties>
<modules>
<module>pig-mc-service</module>
<module>pig-upms-service</module>
</modules>