这里写自定义目录标题
超图微服务云套件大版本升级时,有时候会涉及服务的迁移或重新发布,此时需要对服务进行备份,以免重新发布服务出现漏的,便于核对。本文通过fetch实现了服务的备份,备份成果以数组对象形式存储在txt里下载下来存储。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./fetch.min.js"></script>
</head>
<body>
<script>
<!-- 设计2个变量:urls列表,Authorization-->
//Authorization:打开云套件服务实例,点击编辑,从抓到的请求头里获取Authorization值
//urls:NGINX代理到本地后直接切换页面一次抓取所有内容。
//待备份的服务列表
const urls = [
//形如 'http://localhost:31111/dashboard/web/api/service/instances?componentName=spatialanalysis-JKC_MLX&interfaceName=restjsr'
];
//服务配置文件具体内容数组对象myservice,也就是供下载的txt文件内容
const myservice = []
// json1为分页里抓取到的含url信息的json,需要提取到urls里面
let json1 = {
"total": 169,
"list": [
{
"componentName": "spatialanalysis-CCC_MLX",
"interfaceName": "restjsr",
"alias": "Instance-spatialanalysis-CCC_MLX-restjsr",
"createTime": 1702536290255,
"dataSource": null,
"serviceType": null,
"replicas": 0,
"address": null,
"authorization": null,
"name": "spatialanalysis-CCC_MLX/restjsr"
},
{
"componentName": "data-CCC_MLX",
"interfaceName": "rest",
"alias": "Instance-data-CCC_MLX-rest",
"createTime": 1702536242447,
"dataSource": null,
"serviceType": null,
"replicas": 0,
"address": null,
"authorization": null,
"name": "data-CCC_MLX/rest"
}
]
}
let arr1 = json1.list;
//构建待请求的urls数组
arr1.forEach(item => {
let str1 = "http://localhost:31111/dashboard/web/api/service/instances?componentName="
let str2 = item.componentName
let str3 = "&interfaceName="
let str4 = item.interfaceName
let mystr=str1+str2+str3+str4
console.log(mystr)
urls.push(mystr)
})
async function fetchUrlsInOrder(urls) {
const failedUrls = [];
for (const url of urls) {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
//打开fiddler classic软件抓取请求,打开云套件服务实例,点击编辑,从浏览器调试窗口网络抓到的请求头里获取Authorization值
'Authorization': 'Bearer e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TtaswPEE7G7cQEnztxN1hiU3O4SvROVLM8YdI7mbVkLG_MKUhVKHisHxHFlIYPJkhv5udWLm1o3A6fWyIsa2YWKIX-1ACHTnO83Ex3oquwyIuaeo5huiR_rX3P0lyQDMWHj1uiVL6y3BRcGsuri7eRAJbXtMP1yqFmPIHlP0cWmHlxejoUC11Y1jV1wAk3GBPLD4lcQDagbnvkzAZ3ieXmo8OQVpdKuQJa0UMgPV0fWSvwVVBKwtoAN4bi-jk0J2G4tnQ',
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log('输出实体')
console.log(data)
myservice.push(data)
}).catch(() => {
failedUrls.push(url);
});
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
} catch (error) {
console.log(error);
}
}
return failedUrls;
}
function savetxt(mycontent) {
exportRaw('我的备份内容.txt', mycontent);//执行该函数即可保存
function fakeClick(obj) {
var ev = document.createEvent("MouseEvents");
ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
obj.dispatchEvent(ev);
}
function exportRaw(name, data) {
var urlObject = window.URL || window.webkitURL || window;
var export_blob = new Blob([data]);
var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
save_link.href = urlObject.createObjectURL(export_blob);
save_link.download = name;
fakeClick(save_link);
}
}
async function test() {
const failedUrls = await fetchUrlsInOrder(urls);
savetxt(JSON.stringify(myservice));//下载我的备份配置文件数组
console.log('请求失败的地址有:', failedUrls);
}
test();//开始执行备份
</script>
</body>
</html>