实现概述:
1、查询环境A模板信息
2、获取模板信息值转换
3、同步保存至环境B
package com.jayce.boot.route.common.util;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.jayce.boot.route.common.util.superClient.JsonUtil;
import com.jayce.boot.route.common.util.superClient.SuperClientUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 实现ES模板从环境A,同步到B
*
* @author sunjie
* @datetime 2025/3/8 10:51
**/
public class EsTemplateSyncUtil {
public static void main(String[] args) {
List<String> taskList = Lists.newArrayList("tempalte1",
"tempalte2",
"tempalte3");
for (String taskNo : taskList) {
sync(taskNo);
}
}
public static Boolean sync(String templateName) {
//查询模板
try {
System.out.println(">>>>>>>>>>>>>>>>>>" + templateName + " GET TEMPLATE");
Map<String, String> header = new HashMap<>();
header.put("Authorization", "Basic xxxxxxx");
JsonNode jsonNode = SuperClientUtil.ipHttpGet("http://环境A:9200", "/_template/" + templateName, header);
System.out.println(jsonNode);
//解析参数
Map<String, Object> map = JsonUtil.nodeToEntity(jsonNode, Map.class);
Object paramObj = map.get(templateName);
//同步到新环境
Map<String, String> header2 = new HashMap<>();
header2.put("Authorization", "Basic xxxxxxx");
templateName = templateName.replace("*", "");
JsonNode jsonNode2 = SuperClientUtil.ipHttpPost("http://环境B:9200", "/_template/" + templateName, JsonUtil.toJsonString(paramObj), header2);
System.out.println("ES response:" + jsonNode2);
System.out.println("<<<<<<<<<<<<<<<<<<" + templateName + " SYNC SUCCESS!");
} catch (Exception e) {
System.out.println("<<<<<<<<<<<<<<<<<<" + templateName + " SYNC FAIL!!!!!!!!");
}
return true;
}
}