# 基础公用模块

# commons 公用类依赖

# common-redis 缓存相关配置封装

  • 提供RedisTemplate的初始化配置
  • 提供公用RedisService接口
   /**
     * 获取redisTemplate
     */
    RedisTemplate<String,Object> getRedisTemplate();

    /**
     * 添加应用模块
     */
    void addAppModule(String applicationName);

    /**
     * 获取全部应用模块
     */
    List<String> getAppModules();
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# common-base 公用基础的实体类等封装

  • 模块公用的IAM基础实体类
  • 模块公用的Message基础实体类
  • AppModuleInitializer:应用模块的权限自动提取类,可自动收集模块的权限注解信息用于前端配置等。
  • 分页配置
  • 操作日志保存接口

# common-excel excel解析相关封装

提取自diboot-file组件,提供对easyexcel的增强优化,通过注解实现id-name自动转换等。

具体可参考: diboot file组件基础文档

# base-modules 基础公用服务模块

  • file-storage 文件服务模块
  • scheduler 定时任务模块
  • msg 消息通知服务模块

# 1. file-storage 文件服务模块

主要配置参数

  • server.port: 8102,默认端口
  • spring.datasource.*: 数据库连接信息
  • fdfs.*: FastDFS相关配置参数
  • files.storage.directory: 文件存储路径
    • 本地存储磁盘路径,如:D:/temp
    • FastDFS、OSS等为网络路径,如: http://10.0.0.9:8888

核心代码说明

  • ResourceServerConfig: 资源服务器配置

  • SpringWebConfig: Spring Web MVC相关配置 其中关于文件存储实现,默认为本地存储。如需切换成FastDFS或OSS等,可自行替换此处。

/**
 * 初始化文件存储实现类
 */
@Bean
public FileStorageService fileStorageService() {
    return new LocalFileStorageServiceImpl();
}
1
2
3
4
5
6
7
  • FileStorageService 文件存储与预置实现类 FileStorageService为文件存储接口,包含上传、下载等接口。默认预置本地和FastDFS实现:

    • LocalFileStorageServiceImpl:本地存储实现类
    • FastdfsAbstractFileStorageServiceImpl: FastDFS实现类

    如需扩展其他接口,实现FileStorageService并替换初始化Bean实现即可。

  • 常规Entity、Service、Mapper、Controller等基础模型信息,同diboot file组件

# 2. scheduler 定时任务模块

主要配置参数

  • server.port: 8101,默认端口
  • spring.datasource.*: 数据库连接信息
  • spring.quartz.*: Quartz相关配置参数

核心代码说明

  • ResourceServerConfig: 资源服务器配置

  • SpringWebConfig: Spring Web MVC相关配置

  • 常规Entity、Service、Mapper、Controller等基础模型信息,同diboot-scheduler组件。

如何新增定时任务

  • 定义你的定时任务Job类,继承quartz的QuartzJobBean

  • 添加@CollectThisJob注解,声明Job名称、参数、以及默认的定时cron,以便组件自动收集Job信息供前端选择。

    @DisallowConcurrentExecution
    @CollectThisJob(name = "我的定时任务", paramJson = "{\"daysBefore\":30}", cron = "* * 1 * * ?")
    public class MyJob extends QuartzJobBean {
    
        @Override
        protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            // 获取参数
            JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
            int days = 30;
            if(jobDataMap.containsKey("daysBefore")){
                days = jobDataMap.getInt("daysBefore");
            }
            //TODO 执行定时任务
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  • 前端"定时任务管理"功能中,配置该定时任务调度作业。

# 3. msg-notification 消息通知服务模块

主要配置参数

  • server.port: 8103,默认端口
  • spring.datasource.*: 数据库连接信息
  • spring.mail.*: 邮箱相关配置参数

核心代码说明

  • ResourceServerConfig: 资源服务器配置

  • SpringWebConfig: Spring Web MVC相关配置,以及默认的信息模版、发送通道相关配置

    /**
     * 模版变量服务
     *
     * @return
     */
    @Bean
    public TemplateVariableService templateVariableService() {
        return new SystemTemplateVariableServiceImpl();
    }
    /**
     * 简单邮箱发送通道
     *
     * @return
     */
    @Bean("EMAIL")
    public ChannelStrategy simpleEmailChannel() {
        return new SimpleEmailChannel();
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

简单邮箱发送通道中的@Bean,必须手动指定bean名称,且名称规则为消息组件发送通道类型(见字典:MESSAGE_CHANNEL)

  • MessageController#sendMessage: 提供消息发送实现

    • 如果其他服务需要访问,可以通过diboot-common-api公共服务API下的MessageApiService#sendMessage调用
  • TemplateVariableService: 模版变量service

    • 默认实现为SystemTemplateVariableServiceImpl提供基础的模版变量,如有其他变量可自行添加
    • 通过@TemplateVariable注解向全局提供模版变量,方便前端编辑消息模版
    • 发送的时将模版内容进行解析替换成具体内容
  • ChannelStrategy: 发送通道策略接口

    • 默认实现为SimpleEmailChannel提供简单邮件发送能力
  • VariableData: 替换模版变量实体

    • 发送消息时传入,用于替换模版变量
  • 常规Entity、Service、Mapper、Controller等

    • 组织文件础模型信息,同diboot-message组件。

简单邮件发送步骤

  • 1、yml配置文件增加spring.mail.*配置
  • 2、启动前端项目,进入"消息模版管理"菜单新增模版。
  • 3、发送消息调用代码,如下:
// 需要发送的消息实体
Message message = new Message()
        .setChannel("EMAIL")
        .setSender("xxx@qq.com")
        .setReceiver("xxx@qq.com")
        .setStatus("SEND")
        .setTitle("发送标题")
        .setTemplateId(10000L)
        .setBusinessType("业务类型")
        .setBusinessCode("业务标识别")
        .setExtDataAttribute(Message.RECEIVERS, "xxx@qq.com");
// 配置模版需要替换的变量
VariableData variableData = new VariableData().setRealName("张三").setPhone("155xxx");
// 设置发送内容:消息实体 + 变量值实体
MessageTransferDTO messageTransferDTO = new MessageTransferDTO()
        .setMessage(message)
        .setVariableData(emailVariableData);
// 远程调用访问
messageApiService.sendMessage(messageTransferDTO);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

注意:默认实现的邮件发送中,使用Message#setExtDataAttribute(Message.RECEIVERS, "xxx@qq.com")发送优先级高于Message#setReceiver

更多参考diboot message组件基础文档