perf(消息系统): 优化消息系统发送消息

master
wayn 4 years ago
parent 3f93ad17e1
commit 7e67784471

@ -0,0 +1,32 @@
package com.wayn.admin.api.controller.message;
import com.wayn.common.core.domain.tool.EmailConfig;
import com.wayn.common.core.domain.vo.SendMailVO;
import com.wayn.common.core.service.tool.IMailConfigService;
import com.wayn.common.util.R;
import com.wayn.common.util.mail.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
@RestController
@RequestMapping("message/email")
public class EmailController {
@Autowired
private IMailConfigService mailConfigService;
@PostMapping
public R sendEmail(String subject, String content, String tos) {
EmailConfig emailConfig = mailConfigService.getById(1L);
SendMailVO sendMailVO = new SendMailVO();
sendMailVO.setSubject(subject);
sendMailVO.setContent(content);
sendMailVO.setTos(Arrays.asList(tos));
MailUtil.sendMail(emailConfig, sendMailVO, false);
return R.success();
}
}

@ -66,6 +66,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/*/api-docs").anonymous() .antMatchers("/*/api-docs").anonymous()
.antMatchers("/druid/**").anonymous() .antMatchers("/druid/**").anonymous()
.antMatchers("/elastic/**").anonymous() .antMatchers("/elastic/**").anonymous()
.antMatchers("/message/**").anonymous()
// 除上面外的所有请求全部需要鉴权认证 // 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated().and() .anyRequest().authenticated().and()
.headers().frameOptions().disable(); .headers().frameOptions().disable();

@ -24,6 +24,15 @@ public class WaynConfig {
*/ */
private static String email; private static String email;
/**
*
*/
private static String adminUrl;
/**
*
*/
private static String mobileUrl;
public static String getUploadDir() { public static String getUploadDir() {
return uploadDir; return uploadDir;
} }
@ -63,4 +72,20 @@ public class WaynConfig {
public void setEmail(String email) { public void setEmail(String email) {
WaynConfig.email = email; WaynConfig.email = email;
} }
public static String getAdminUrl() {
return adminUrl;
}
public void setAdminUrl(String adminUrl) {
WaynConfig.adminUrl = adminUrl;
}
public static String getMobileUrl() {
return mobileUrl;
}
public void setMobileUrl(String mobileUrl) {
WaynConfig.mobileUrl = mobileUrl;
}
} }

@ -53,6 +53,8 @@ wayn:
version: 1.1.0 version: 1.1.0
email: 166738430@qq.com email: 166738430@qq.com
uploadDir: E:/wayn/upload uploadDir: E:/wayn/upload
adminUrl: http://localhost:81/message/email
mobileUrl: http://localhost:82/message/email
# 快递列表 # 快递列表
shop: shop:

@ -244,7 +244,7 @@ public class BaseElasticService {
public boolean deleteIndex(String idxName) { public boolean deleteIndex(String idxName) {
try { try {
if (!this.indexExist(idxName)) { if (!this.indexExist(idxName)) {
log.error(" idxName={} 不存在,idxSql={}", idxName); log.error(" idxName={} 不存在", idxName);
return false; return false;
} }
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT); AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);

@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -33,26 +34,23 @@ import java.util.Map;
@Service @Service
public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order> implements IAdminOrderService { public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order> implements IAdminOrderService {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Autowired @Autowired
private AdminOrderMapper adminOrderMapper; private AdminOrderMapper adminOrderMapper;
// @Autowired // @Autowired
private WxPayService wxPayService; private WxPayService wxPayService;
@Autowired @Autowired
private IOrderGoodsService iOrderGoodsService; private IOrderGoodsService iOrderGoodsService;
@Autowired @Autowired
private IGoodsProductService iGoodsProductService; private IGoodsProductService iGoodsProductService;
@Autowired @Autowired
private IMemberService iMemberService; private IMemberService iMemberService;
@Autowired @Autowired
private IMailConfigService mailConfigService; private IMailConfigService mailConfigService;
@Autowired @Value("${wayn.adminUrl}")
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法 private String adminUrl;
@Override @Override
public IPage<Order> listPage(IPage<Order> page, Order order) { public IPage<Order> listPage(IPage<Order> page, Order order) {
@ -132,7 +130,7 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
// 注意订单号只发后6位 // 注意订单号只发后6位
String email = iMemberService.getById(order.getUserId()).getEmail(); String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotEmpty(email)) { if (StringUtils.isNotEmpty(email)) {
sendEmail("订单已经退款", order.getOrderSn().substring(8, 14), email); sendEmail("订单已经退款", order.getOrderSn().substring(8, 14), email, adminUrl);
} }
// logHelper.logOrderSucceed("退款", "订单编号 " + order.getOrderSn()); // logHelper.logOrderSucceed("退款", "订单编号 " + order.getOrderSn());
return R.success(); return R.success();
@ -164,8 +162,7 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
// "您的订单已经发货,快递公司 {1},快递单 {2} ,请注意查收" // "您的订单已经发货,快递公司 {1},快递单 {2} ,请注意查收"
String email = iMemberService.getById(order.getUserId()).getEmail(); String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotEmpty(email)) { if (StringUtils.isNotEmpty(email)) {
sendEmail("您的订单已经发货,快递公司 申通,快递单 " + order.getOrderSn().substring(8, 14) + ",请注意查收", order.getOrderSn().substring(8, 14), email); sendEmail("您的订单已经发货,快递公司 申通,快递单 " + order.getOrderSn().substring(8, 14) + ",请注意查收", order.getOrderSn().substring(8, 14), email, adminUrl);
} }
return R.success(); return R.success();
} }
@ -192,11 +189,12 @@ public class AdminOrderServiceImpl extends ServiceImpl<AdminOrderMapper, Order>
* @param content * @param content
* @param tos * @param tos
*/ */
private void sendEmail(String subject, String content, String tos) { private void sendEmail(String subject, String content, String tos, String notifyUrl) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("subject", subject); map.put("subject", subject);
map.put("content", content); map.put("content", content);
map.put("tos", tos); map.put("tos", tos);
map.put("notifyUrl", notifyUrl);
// 异步发送邮件 // 异步发送邮件
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map); rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
} }

@ -52,7 +52,7 @@ public class Gen {
// 数据源配置 // 数据源配置
DataSourceConfig dsc = new DataSourceConfig(); DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/wayn_shop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"); dsc.setUrl("jdbc:mysql://192.168.31.49:3306/wayn_shop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
// dsc.setSchemaName("public"); // dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root"); dsc.setUsername("root");
@ -61,10 +61,10 @@ public class Gen {
// 包配置 // 包配置
PackageConfig pc = new PackageConfig(); PackageConfig pc = new PackageConfig();
pc.setService("service.shop"); pc.setService("service.tool");
pc.setServiceImpl("service.shop.impl"); pc.setServiceImpl("service.tool.impl");
pc.setMapper("mapper.shop"); pc.setMapper("mapper.tool");
pc.setEntity("domain.shop"); pc.setEntity("domain.tool");
pc.setModuleName(""); pc.setModuleName("");
pc.setParent("com.wayn.common.core"); pc.setParent("com.wayn.common.core");
mpg.setPackageInfo(pc); mpg.setPackageInfo(pc);

@ -1,6 +1,7 @@
package com.wayn.message.reciver; package com.wayn.message.reciver;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -26,6 +27,11 @@ public class DirectReceiver {
@RabbitHandler @RabbitHandler
public void process(Map testMessage) { public void process(Map testMessage) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString()); System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
String notifyUrl = (String) testMessage.get("notifyUrl");
if (StringUtils.isEmpty(notifyUrl)) {
log.error("notifyUrl不能为空参数" + testMessage.toString());
return;
}
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap(); MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap();
@ -33,9 +39,8 @@ public class DirectReceiver {
multiValueMap.add("content", testMessage.get("content")); multiValueMap.add("content", testMessage.get("content"));
multiValueMap.add("tos", testMessage.get("tos")); multiValueMap.add("tos", testMessage.get("tos"));
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(multiValueMap, headers); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(multiValueMap, headers);
String url = "http://localhost:82/message/email";
try { try {
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); ResponseEntity<String> response = restTemplate.postForEntity(notifyUrl, request, String.class);
if (response.getStatusCode().value() != 200) { if (response.getStatusCode().value() != 200) {
throw new Exception(testMessage.toString() + " 邮件发送失败"); throw new Exception(testMessage.toString() + " 邮件发送失败");
} }

@ -4,7 +4,13 @@ server:
spring: spring:
#配置rabbitMq 服务器 #配置rabbitMq 服务器
rabbitmq: rabbitmq:
host: 127.0.0.1 host: 182.254.214.165
port: 5672 port: 5672
username: guest username: admin
password: guest password: admin
# 日志配置
logging:
level:
com.wayn: debug
org.springframework: info

@ -32,6 +32,7 @@ import com.wayn.mobile.api.service.ICartService;
import com.wayn.mobile.api.service.IOrderService; import com.wayn.mobile.api.service.IOrderService;
import com.wayn.mobile.api.task.CancelOrderTask; import com.wayn.mobile.api.task.CancelOrderTask;
import com.wayn.mobile.api.util.OrderSnGenUtil; import com.wayn.mobile.api.util.OrderSnGenUtil;
import com.wayn.mobile.framework.config.WaynConfig;
import com.wayn.mobile.framework.redis.RedisCache; import com.wayn.mobile.framework.redis.RedisCache;
import com.wayn.mobile.framework.security.util.SecurityUtils; import com.wayn.mobile.framework.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -62,39 +63,29 @@ import java.util.*;
@Service @Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService { public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
@Autowired @Autowired
private IAddressService iAddressService; private IAddressService iAddressService;
@Autowired @Autowired
private ICartService iCartService; private ICartService iCartService;
@Autowired @Autowired
private IOrderGoodsService iOrderGoodsService; private IOrderGoodsService iOrderGoodsService;
@Autowired @Autowired
private IGoodsProductService iGoodsProductService; private IGoodsProductService iGoodsProductService;
@Autowired @Autowired
private WxPayService wxPayService; private WxPayService wxPayService;
@Autowired @Autowired
private IMemberService iMemberService; private IMemberService iMemberService;
@Autowired @Autowired
private IMailConfigService mailConfigService; private IMailConfigService mailConfigService;
@Autowired @Autowired
private OrderMapper orderMapper; private OrderMapper orderMapper;
@Autowired @Autowired
private TaskService taskService; private TaskService taskService;
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Override @Override
public R selectListPage(IPage<Order> page, Integer showType) { public R selectListPage(IPage<Order> page, Integer showType) {
List<Short> orderStatus = OrderUtil.orderStatus(showType); List<Short> orderStatus = OrderUtil.orderStatus(showType);
@ -411,7 +402,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员 // 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail(); String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) { if (StringUtils.isNotBlank(email)) {
sendEmail("新订单通知", order.toString(), email); sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl());
} }
// 删除redis中订单id // 删除redis中订单id
redisCache.deleteZsetObject("order_zset", order.getId()); redisCache.deleteZsetObject("order_zset", order.getId());
@ -443,7 +434,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员 // 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail(); String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) { if (StringUtils.isNotBlank(email)) {
sendEmail("新订单通知", order.toString(), email); sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl());
} }
// 删除redis中订单id // 删除redis中订单id
@ -511,7 +502,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
String email = iMemberService.getById(order.getUserId()).getEmail(); String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotEmpty(email)) { if (StringUtils.isNotEmpty(email)) {
if (StringUtils.isNotBlank(email)) { if (StringUtils.isNotBlank(email)) {
sendEmail("订单正在退款", order.toString(), email); sendEmail("订单正在退款", order.toString(), email, WaynConfig.getMobileUrl());
} }
} }
@ -582,11 +573,12 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
* @param content * @param content
* @param tos * @param tos
*/ */
private void sendEmail(String subject, String content, String tos) { private void sendEmail(String subject, String content, String tos, String notifyUrl) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("subject", subject); map.put("subject", subject);
map.put("content", content); map.put("content", content);
map.put("tos", tos); map.put("tos", tos);
map.put("notifyUrl", notifyUrl);
// 异步发送邮件 // 异步发送邮件
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map); rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
} }

@ -65,7 +65,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/webjars/**").anonymous() .antMatchers("/webjars/**").anonymous()
.antMatchers("/*/api-docs").anonymous() .antMatchers("/*/api-docs").anonymous()
.antMatchers("/druid/**").anonymous() .antMatchers("/druid/**").anonymous()
.antMatchers("/message/email/**").anonymous() .antMatchers("/message/**").anonymous()
// 除上面外的所有请求全部需要鉴权认证 // 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated().and() .anyRequest().authenticated().and()
.headers().frameOptions().disable(); .headers().frameOptions().disable();

@ -24,6 +24,15 @@ public class WaynConfig {
*/ */
private static String email; private static String email;
/**
*
*/
private static String adminUrl;
/**
*
*/
private static String mobileUrl;
public static String getUploadDir() { public static String getUploadDir() {
return uploadDir; return uploadDir;
} }
@ -63,4 +72,20 @@ public class WaynConfig {
public void setEmail(String email) { public void setEmail(String email) {
WaynConfig.email = email; WaynConfig.email = email;
} }
public static String getAdminUrl() {
return adminUrl;
}
public void setAdminUrl(String adminUrl) {
WaynConfig.adminUrl = adminUrl;
}
public static String getMobileUrl() {
return mobileUrl;
}
public void setMobileUrl(String mobileUrl) {
WaynConfig.mobileUrl = mobileUrl;
}
} }

@ -50,6 +50,8 @@ wayn:
version: 1.1.0 version: 1.1.0
email: 166738430@qq.com email: 166738430@qq.com
uploadDir: E:/wayn/upload uploadDir: E:/wayn/upload
adminUrl: http://localhost:81/message/email
mobileUrl: http://localhost:82/message/email
# wx支付配置 # wx支付配置
shop: shop:

Loading…
Cancel
Save