1.创建模板
worker_processes 1;
events {
worker_connections 1024;
}
stream {
server {
listen listen-port;
proxy_pass ip:port;
proxy_connect_timeout 2s;
}
#foreach($port in [9000..9009])
server {
listen $port;
proxy_pass ip:$port;
proxy_connect_timeout 2s;
}
#end
}
2.执行模板生成
public static String getTempContent(Dict dict, String content) throws IOException {
// 映射模板对象
final Map<String, Object> map = Convert.convert(new TypeReference<Map<String, Object>>() {
}, dict);
// 初始化并取得Velocity引擎
VelocityEngine ve = new VelocityEngine();
ve.init();
// 取得velocity的模版内容, 模板内容来自字符传
// 取得velocity的上下文context
VelocityContext context = new VelocityContext(map);
// 输出流
StringWriter writer = new StringWriter();
// 转换输出
ve.evaluate(context, writer, "", content); // 关键方法
System.out.println(writer.toString());
return writer.toString();
}
public static void main(String[] args) {
getTemplates();
}
public static void getTemplates() {
String str = ResourceUtil.readUtf8Str("nginx_config.vm");
try {
String value = VelocityUtils.getTempContent(new Dict(),str);
//System.out.println(value);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version><!--$NO-MVN-MAN-VER$-->
</dependency>
标签:String,批量,writer,nginx,proxy,new,port,模板,配置文件
From: https://www.cnblogs.com/jackson1024/p/17340756.html