codeFreemarker Thymeleaf

38 阅读2分钟

image.pngimage.png

Thymeleaf执行的效率低。
发布后生成 多线程异步

没有生成成功,解决方案1重试retry2扫描数据库和消息中间件3发消息记录日志

本类不会有异步方法,写一个方法

在artile微服务中添加MinIO和freemarker的支持
我们的为微服务中已经包含了模板文件,位置在resources/templates/article.ftl
把templates下的 plugins放入minIo中
index.js和index.css两个文件手动上传到MinIO

image.png

article微服务中导入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>com.heima</groupId>
    <artifactId>heima-leadnews-file-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
需要article配置文件配置minio
minio:
  accessKey: minio
  secretKey: minio123
  bucket: leadnews
  endpoint: http://192.168.137.136:9000
  readPath: http://192.168.137.136:9000
  在article微服务创建ArticleFreemarkerService接口
package com.heima.article.service;

import com.heima.article.entity.ApArticle;

public interface ArticleFreemarkerService {

    /**
     * 生成文章详情静态页面
     * 写入Minio
     * @param articleId
     * @param content
     */
    String buildContentHtml(Long articleId,String content);
}
创建实现类ArticleFreemarkerServiceImpl
package com.heima.article.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.heima.article.service.FreeMarkerService;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.file.service.MinioService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class FreeMarkerServiceImpl implements FreeMarkerService {

    @Autowired
    private Configuration configuration;
    @Autowired
    private MinioService minioService;
    @Lazy //延迟注入
    @Autowired
    private ApArticleService apArticleService;

    @Override
    public String buildContentHtml(Long articleId,String content) {

        try {
//        读取模板页面
            Template template = configuration.getTemplate("article.ftl");
//        准备数据模型
            Map map = new HashMap<>();
             转为数组
            map.put("content", JSONArray.parseArray(content));
//        调用API生成静态页,存入流中
            StringWriter stringWriter = new StringWriter();
            存到模板
            template.process(map,stringWriter);
//        放入minio
            String fileName = articleId+".html";
            InputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes());
            //        获取到访问路径
            String url = minioService.uploadHtmlFile("", fileName,inputStream );
            log.info("上传后的url=={}",url);
            return url;
        }catch (Exception e){
            e.printStackTrace();
            throw new LeadException(AppHttpCodeEnum.UPLOAD_ERROR);
        }
    }
}
在保存文章saveArticle的方法中使用异步方式调用生成页面的方法,更新文章静态页面

在article服务的初始化类中添加包名,初始化线程池
package com.heima.wemedia.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({"com.heima.common.aliyun","com.heima.common.knife4j","com.heima.common.exception",
        "com.heima.common.bcrypt","com.heima.common.delayTask","com.heima.common.threadpool"})
public class InitConfig {
}

@Autowired
private ThreadPoolTaskExecutor taskExecutor;

public Long saveArticle(WmNewsResultDTO dto) {
    //省略代码。。。。。
//        发布成功后,业务解耦,发消息,异步,调用freemarkerapi 生成静态页面
taskExecutor.submit(new Runnable() {
    @Override
    public void run() {
        freeMarkerService.buildContentHtml(articleId,dto.getContent());
    }
});
    //省略代码。。。。。
}
启动类articeapplication@EnableAsync

image.png image.png