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