Freemarker
存在一份模板文件,需要替换其中指定的数据,现有的代码是通过正则匹配替换的。其中模板配置文件大致如下,需要将模板中的内容动态替换
1 2 3
| jobmanager.rpc.address: {{app-name}}-jobmanager-headless taskmanager.numberOfTaskSlots: {{numberOfTaskSlots}} kubernetes.namespace: {{namespace}}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| String flinkConfig = FileUtils.getFileContent("flink/flink-conf.yaml"); Map<String, String> configData = new HashMap<String, String>(); configData.put("numberOfTaskSlots",flinkEntity.getSlot().toString()); configData.put("app-name",flinkEntity.getName()); configData.put("namespace",flinkEntity.getNamespace()); String ConfigContent = FileUtils.parsePattern(flinkConfig,configData);
public static String getFileContent(String filePath){ try { Resource resource = new ClassPathResource(filePath); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is,"utf-8"); BufferedReader br = new BufferedReader(isr); String data = null; StringBuffer sb = new StringBuffer(); while ((data = br.readLine()) != null) { sb.append(data + System.getProperty("line.separator")); } br.close(); isr.close(); is.close(); return sb.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }
public static String parsePattern(String content, Map<String, String> data) { String pattern = "\\{\\{(.+?)\\}\\}"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(content); StringBuffer sb = new StringBuffer(); while (m.find()) { String key = m.group(1); String value = data.get(key); m.appendReplacement(sb, value == null ? "" : value); } m.appendTail(sb); return sb.toString(); }
|
但是新的模板内有计算需求,要不就在代码中计算好传进来,要不在模板中计算,使用freemarker
FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件
这种方式通常被称为 MVC (模型 视图 控制器) 模式,对于动态网页来说,是一种特别流行的模式。 它帮助从开发人员(Java 程序员)中分离出网页设计师(HTML设计师)。设计师无需面对模板中的复杂逻辑, 在没有程序员来修改或重新编译代码时,也可以修改页面的样式。
而FreeMarker最初的设计,是被用来在MVC模式的Web开发框架中生成HTML页面的,它没有被绑定到 Servlet或HTML或任意Web相关的东西上。它也可以用于非Web应用环境中。
这里有个官方模板:http://freemarker.foofun.cn/pgui_quickstart_all.html
下面是代码配置:需要在启动主类加上注解配置@SpringBootApplication(exclude = { FreeMarkerAutoConfiguration.class })
1 2 3 4 5 6
| <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import freemarker.template.Configuration; import freemarker.template.TemplateExceptionHandler; import org.springframework.stereotype.Component; import java.io.File;
@Component public class FreemarkerConfig { public Configuration getFreemarker() { try{ Configuration cfg = new Configuration(Configuration.getVersion()); cfg.setClassForTemplateLoading(this.getClass(), "/template"); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); return cfg; }catch (Exception e){ e.printStackTrace(); } return null; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import com.alibaba.fastjson.JSONObject; import com.api.config.FreemarkerConfig; import com.api.model.entity.RosEntity; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateExceptionHandler; import org.springframework.beans.factory.annotation.Autowired;
import java.io.File; import java.io.StringWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map;
public class TemplateUtils {
public static String resolveTemplate (Map data,String fileName,FreemarkerConfig cfg){ try{ Template temp =cfg.getFreemarker().getTemplate(fileName); Writer out = new StringWriter(); temp.process(data, out); String result = out.toString(); out.close(); return result; }catch (Exception e){ e.printStackTrace(); } return null; } }
|
下面是模板文件:config.template
1 2 3 4 5 6 7 8 9 10 11 12
| max_connections = ${ (100 * flavor.ram/512)?c } max_user_connections = ${ (100 * flavor.ram/512)?c } back_log = ${ (50 + 20 * flavor.ram/512)?c } open_files_limit = ${ (512 * flavor.ram/512)?c } table_open_cache = ${ (256 * flavor.ram/512)?c } table_definition_cache = ${ (256 * flavor.ram/512)?c } query_cache_size = ${ (8 * flavor.ram/512)?c }M tmp_table_size = ${ (8 * flavor.ram/512)?c }M max_heap_table_size = ${ (8 * flavor.ram/512)?c }M thread_cache_size = ${ (4 * flavor.ram/512)?c } innodb_buffer_pool_size = ${ (250 * flavor.ram/512)?c }M innodb_open_files = ${ (512 * flavor.ram/512)?c }
|
1 2 3 4 5 6 7 8 9 10 11
| import com.api.config.FreemarkerConfig;
public class DealImpl { @Autowired private FreemarkerConfig cfg;
Map<String, Object> data = new HashMap<>(); data.put("flavor", server.getFlavor()); String db = TemplateUtils.resolveTemplate(data,"mysql/config.template",cfg); }
|
这里有个坑是freemarker里的数值计算后,会没三位数字有个逗号,可以用?c将其强制转换为字符串