云片、阿里云短信发送的调试完成
parent
d843d6a5a8
commit
0d0110ec08
|
@ -48,8 +48,8 @@ public class RedisConfig {
|
||||||
* 创建 Redis Pub/Sub 广播消费的容器
|
* 创建 Redis Pub/Sub 广播消费的容器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory,
|
public RedisMessageListenerContainer redisMessageListenerContainer(
|
||||||
List<AbstractChannelMessageListener<?>> listeners) {
|
RedisConnectionFactory factory, List<AbstractChannelMessageListener<?>> listeners) {
|
||||||
// 创建 RedisMessageListenerContainer 对象
|
// 创建 RedisMessageListenerContainer 对象
|
||||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||||
// 设置 RedisConnection 工厂。
|
// 设置 RedisConnection 工厂。
|
||||||
|
@ -69,8 +69,8 @@ public class RedisConfig {
|
||||||
* Redis Stream 的 xreadgroup 命令:https://www.geek-book.com/src/docs/redis/redis/redis.io/commands/xreadgroup.html
|
* Redis Stream 的 xreadgroup 命令:https://www.geek-book.com/src/docs/redis/redis/redis.io/commands/xreadgroup.html
|
||||||
*/
|
*/
|
||||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||||
public StreamMessageListenerContainer<String, ObjectRecord<String, String>> redisStreamMessageListenerContainer(RedisTemplate<String, Object> redisTemplate,
|
public StreamMessageListenerContainer<String, ObjectRecord<String, String>> redisStreamMessageListenerContainer(
|
||||||
List<AbstractStreamMessageListener<?>> listeners) {
|
RedisTemplate<String, Object> redisTemplate, List<AbstractStreamMessageListener<?>> listeners) {
|
||||||
// 第一步,创建 StreamMessageListenerContainer 容器
|
// 第一步,创建 StreamMessageListenerContainer 容器
|
||||||
// 创建 options 配置
|
// 创建 options 配置
|
||||||
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, ObjectRecord<String, String>> containerOptions =
|
StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, ObjectRecord<String, String>> containerOptions =
|
||||||
|
|
|
@ -38,6 +38,7 @@ public class SmsClientFactoryImpl implements SmsClientFactory {
|
||||||
AbstractSmsClient client = clients.get(properties.getId());
|
AbstractSmsClient client = clients.get(properties.getId());
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
client = this.createSmsClient(properties);
|
client = this.createSmsClient(properties);
|
||||||
|
client.init();
|
||||||
clients.put(client.getId(), client);
|
clients.put(client.getId(), client);
|
||||||
} else {
|
} else {
|
||||||
client.refresh(properties);
|
client.refresh(properties);
|
||||||
|
|
|
@ -16,7 +16,7 @@ import javax.annotation.Resource;
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SmsSendConsumer extends AbstractStreamMessageListener<SysSmsSendMessage> {
|
public class SysSmsSendConsumer extends AbstractStreamMessageListener<SysSmsSendMessage> {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysSmsService smsService;
|
private SysSmsService smsService;
|
|
@ -5,7 +5,7 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短信日志服务接口
|
* 短信日志 Service 实现类
|
||||||
*
|
*
|
||||||
* @author zzf
|
* @author zzf
|
||||||
* @date 13:48 2021/3/2
|
* @date 13:48 2021/3/2
|
||||||
|
|
|
@ -15,14 +15,14 @@ import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短信发送日志服务实现类
|
* 短信日志 Service 实现类
|
||||||
*
|
*
|
||||||
* @author zzf
|
* @author zzf
|
||||||
* @date 2021/1/25 9:25
|
* @date 2021/1/25 9:25
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class SysSmsSendLogServiceImpl implements SysSmsLogService {
|
public class SysSmsLogServiceImpl implements SysSmsLogService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysSmsLogMapper smsLogMapper;
|
private SysSmsLogMapper smsLogMapper;
|
|
@ -12,7 +12,6 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
|
||||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
|
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage;
|
import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer;
|
import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer;
|
||||||
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService;
|
|
||||||
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsLogService;
|
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsLogService;
|
||||||
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService;
|
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService;
|
||||||
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
|
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
|
||||||
|
@ -41,8 +40,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SysSmsServiceImpl implements SysSmsService {
|
public class SysSmsServiceImpl implements SysSmsService {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SysSmsChannelService smsChannelService;
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysSmsTemplateService smsTemplateService;
|
private SysSmsTemplateService smsTemplateService;
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -145,7 +142,8 @@ public class SysSmsServiceImpl implements SysSmsService {
|
||||||
SmsCommonResult<SmsSendRespDTO> sendResult = smsClient.send(message.getLogId(), message.getMobile(),
|
SmsCommonResult<SmsSendRespDTO> sendResult = smsClient.send(message.getLogId(), message.getMobile(),
|
||||||
message.getApiTemplateId(), message.getTemplateParams());
|
message.getApiTemplateId(), message.getTemplateParams());
|
||||||
smsLogService.updateSmsSendResult(message.getLogId(), sendResult.getCode(), sendResult.getMsg(),
|
smsLogService.updateSmsSendResult(message.getLogId(), sendResult.getCode(), sendResult.getMsg(),
|
||||||
sendResult.getApiCode(), sendResult.getApiMsg(), sendResult.getApiRequestId(), sendResult.getData().getSerialNo());
|
sendResult.getApiCode(), sendResult.getApiMsg(), sendResult.getApiRequestId(),
|
||||||
|
sendResult.getData() != null ? sendResult.getData().getSerialNo() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cn.iocoder.dashboard;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.datasource.config.DataSourceConfiguration;
|
||||||
|
import cn.iocoder.dashboard.framework.mybatis.config.MybatisConfiguration;
|
||||||
|
import cn.iocoder.dashboard.framework.redis.config.RedisConfig;
|
||||||
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
|
||||||
|
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
||||||
|
import org.redisson.spring.starter.RedissonAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisIntegrationTest.Application.class)
|
||||||
|
@ActiveProfiles("integration-test") // 设置使用 application-integration-test 配置文件
|
||||||
|
public class BaseDbAndRedisIntegrationTest {
|
||||||
|
|
||||||
|
@Import({
|
||||||
|
// DB 配置类
|
||||||
|
DataSourceConfiguration.class, // 自己的 DB 配置类
|
||||||
|
DataSourceAutoConfiguration.class, // Spring DB 自动配置类
|
||||||
|
DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类
|
||||||
|
DruidDataSourceAutoConfigure.class, // Druid 自动配置类
|
||||||
|
// MyBatis 配置类
|
||||||
|
MybatisConfiguration.class, // 自己的 MyBatis 配置类
|
||||||
|
MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类
|
||||||
|
// Redis 配置类
|
||||||
|
RedisAutoConfiguration.class, // Spring Redis 自动配置类
|
||||||
|
RedisConfig.class, // 自己的 Redis 配置类
|
||||||
|
RedissonAutoConfiguration.class, // Redisson 自动高配置类
|
||||||
|
})
|
||||||
|
public static class Application {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import cn.hutool.core.thread.ThreadUtil;
|
||||||
import cn.iocoder.dashboard.BaseRedisIntegrationTest;
|
import cn.iocoder.dashboard.BaseRedisIntegrationTest;
|
||||||
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
|
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.consumer.mail.SysMailSendConsumer;
|
import cn.iocoder.dashboard.modules.system.mq.consumer.mail.SysMailSendConsumer;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SmsSendConsumer;
|
import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SysSmsSendConsumer;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.message.mail.SysMailSendMessage;
|
import cn.iocoder.dashboard.modules.system.mq.message.mail.SysMailSendMessage;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage;
|
import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class RedisStreamTest {
|
public class RedisStreamTest {
|
||||||
|
|
||||||
@Import({SmsSendConsumer.class, SysMailSendConsumer.class})
|
@Import({SysSmsSendConsumer.class, SysMailSendConsumer.class})
|
||||||
@Disabled
|
@Disabled
|
||||||
public static class ConsumerTest extends BaseRedisIntegrationTest {
|
public static class ConsumerTest extends BaseRedisIntegrationTest {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.service;
|
|
@ -0,0 +1,74 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.service.sms;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.thread.ThreadUtil;
|
||||||
|
import cn.iocoder.dashboard.BaseDbAndRedisIntegrationTest;
|
||||||
|
import cn.iocoder.dashboard.common.enums.UserTypeEnum;
|
||||||
|
import cn.iocoder.dashboard.framework.sms.config.SmsConfiguration;
|
||||||
|
import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SysSmsSendConsumer;
|
||||||
|
import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsChannelServiceImpl;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsLogServiceImpl;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsServiceImpl;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsTemplateServiceImpl;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.user.SysUserService;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Import({SmsConfiguration.class,
|
||||||
|
SysSmsChannelServiceImpl.class, SysSmsServiceImpl.class, SysSmsTemplateServiceImpl.class, SysSmsLogServiceImpl.class,
|
||||||
|
SysSmsProducer.class, SysSmsSendConsumer.class})
|
||||||
|
public class SysSmsServiceIntegrationTest extends BaseDbAndRedisIntegrationTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysSmsServiceImpl smsService;
|
||||||
|
@Resource
|
||||||
|
private SysSmsChannelServiceImpl smsChannelService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private SysUserService userService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendSingleSms_云片发送成功() {
|
||||||
|
// 参数准备
|
||||||
|
String mobile = "15601691399";
|
||||||
|
Long userId = 1L;
|
||||||
|
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||||
|
String templateCode = "test_01";
|
||||||
|
Map<String, Object> templateParams = MapUtil.<String, Object>builder()
|
||||||
|
.put("operation", "登陆").put("code", "1234").build();
|
||||||
|
// 调用
|
||||||
|
smsService.sendSingleSms(mobile, userId, userType, templateCode, templateParams);
|
||||||
|
|
||||||
|
// 等待 MQ 消费
|
||||||
|
ThreadUtil.sleep(1, TimeUnit.HOURS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendSingleSms_阿里云发送成功() {
|
||||||
|
// 参数准备
|
||||||
|
String mobile = "15601691399";
|
||||||
|
Long userId = 1L;
|
||||||
|
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||||
|
String templateCode = "test_02";
|
||||||
|
Map<String, Object> templateParams = MapUtil.<String, Object>builder()
|
||||||
|
.put("code", "1234").build();
|
||||||
|
// 调用
|
||||||
|
smsService.sendSingleSms(mobile, userId, userType, templateCode, templateParams);
|
||||||
|
|
||||||
|
// 等待 MQ 消费
|
||||||
|
ThreadUtil.sleep(1, TimeUnit.HOURS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
// public void testDoSendSms() {
|
||||||
|
// // 等待 MQ 消费
|
||||||
|
// ThreadUtil.sleep(1, TimeUnit.HOURS);
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
|
@ -9,19 +9,15 @@ spring:
|
||||||
# 数据源配置项
|
# 数据源配置项
|
||||||
datasource:
|
datasource:
|
||||||
name: ruoyi-vue-pro
|
name: ruoyi-vue-pro
|
||||||
url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写
|
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
|
||||||
driver-class-name: org.h2.Driver
|
driver-class-name: com.mysql.jdbc.Driver
|
||||||
username: sa
|
username: root
|
||||||
password:
|
password: 123456
|
||||||
schema: classpath:sql/create_tables.sql # MySQL 转 H2 的语句,使用 https://www.jooq.org/translate/ 工具
|
|
||||||
druid:
|
|
||||||
async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度
|
|
||||||
initial-size: 1 # 单元测试,配置为 1,提升启动速度
|
|
||||||
|
|
||||||
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
|
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
|
||||||
redis:
|
redis:
|
||||||
host: 127.0.0.1 # 地址
|
host: 127.0.0.1 # 地址
|
||||||
port: 6379 # 端口(单元测试,使用 16379 端口)
|
port: 6379 # 端口
|
||||||
database: 0 # 数据库索引
|
database: 0 # 数据库索引
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
|
|
Loading…
Reference in New Issue