API网关Gravitee-MongoDB数据迁移详细教程(三)

MongoDB数据迁移(API网关Gravitee-接口配置信息迁移),由于需要再不同环境中部署网关信息,接口如果单独配置会很费时间,所以就研究一次性迁移过去。

示例:将10.11.22.33服务器上的mongo数据迁移到10.11.22.36服务器上的mongo

 

步骤一
1.登录10.11.22.33服务器,(导出)备份数据

进入mongo安装目录 
~# cd /app/mongodb-3.6.12/bin

如果bin目录下没有dump目录则创建
~# mkdir dump

执行mongodump命令备份数据
~# mongodump 
(如果提示:-bash: mongodump: command not found --- start )
~# vim ~/.bashrc
export PATH=export PATH=/app/mongodb-3.6.12/bin:$PATH
~# source ~/.bashrc
(如果提示:-bash: mongodump: command not found --- end )

备份完成后可以在dump目录下看到三个文件目录,也就是我们的API网关相应的数据库信息
~# ll /app/mongodb-3.6.12/bin/dump
admin
gravitee
gravitee-am

将/app/mongodb-3.6.12/bin/dump文件夹通过SecureFX或其它工具下载至本地


步骤二
2.登录10.11.22.36服务器,导入备份好的数据
先通过SecureFX或其它工具将刚刚下载好的dump目录上传到服务器 /app/graviteev2/mongodb 目录

进入mongo安装目录 
~# cd /app/graviteev2/mongodb/mongodb-linux/bin

执行 mongorestore 命令将备份好的数据导入当前mongo(--drop增加此命令表示先删除已有的数据库信息,再将数据全部导入)
导入前先确定 /app/graviteev2/mongodb/dump 目录的权限,建议设置为 777 (chmod -R 777 /app/graviteev2/mongodb/dump)
~# mongorestore -h 10.11.22.36:27017 -d gravitee --drop /app/graviteev2/mongodb/dump/gravitee
~# mongorestore -h 10.11.22.36:27017 -d gravitee-am --drop /app/graviteev2/mongodb/dump/gravitee-am
~# mongorestore -h 10.11.22.36:27017 -d admin --drop /app/graviteev2/mongodb/dump/admin
(如果提示:-bash: mongorestore: command not found --- start )
~# vim ~/.bashrc
export PATH=export PATH=/app/mongodb-3.6.12/bin:$PATH
~# source ~/.bashrc
(如果提示:-bash: mongorestore: command not found --- end )

输入mongo进入数据库(可以不需要重启数据库,直接进入即可)
~# mongo

 

步骤三

3.修改mongo数据库里面的网关配置IP信息
由于不同服务器部署的网关对应的服务ip需要不同,所以我们直接把接口数据拿过来是不可以直接使用的,需要更改ip,这里我们主要通过mongo脚本手工执行,批量替换。需要用到mongo可视化工具 nosqlbooster4mongo。

使用 nosqlbooster4mongo 工具连接mongo数据库 10.11.22.36:27017
主要看这两个数据库 gravitee 和 gravitee-am

3.1 先进入 gravitee 数据库,执行以下脚本,查询出gravitee库所有的数据信息,然后全局搜索 10.11.22.35,10.11.22.34

var collections = db.getCollectionNames();
for(var i = 0; i< collections.length; i++){ 
print('Collection: ' + collections[i]); // print the name of each collection
db.getCollection(collections[i]).find().forEach(printjson);
}


开始执行替换(IP)脚本,因为不同环境的部署服务器不同,所以需要改变IP.
gravitee数据库
apis表:
-- apis.definition 执行到出现了:Script executed successfully, but there is no result to show.(此次执行2次)

db.getCollection('apis').find({'definition':/10.11.22.35/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.definition)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.35','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.definition = tmp ;
db.apis.save(item);
} 
);

 

-- apis.definition 执行到出现了:Script executed successfully, but there is no result to show.(此次执行11次)

db.getCollection('apis').find({'definition':/10.11.22.34/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.definition)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.34','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.definition = tmp ;
db.apis.save(item);
} 
);

  

events表:
-- events.payload 执行到出现了:Script executed successfully, but there is no result to show.(此次执行2次)

db.getCollection('events').find({'payload':/10.11.22.35/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.payload)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.35','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.payload = tmp ;
db.events.save(item);
} 
);


-- events.payload 执行到出现了:Script executed successfully, but there is no result to show.(此次执行13次)

db.getCollection('events').find({'payload':/10.11.22.34/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.payload)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.34','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.payload = tmp ;
db.events.save(item);
} 
);

  

-- events.properties 执行到出现了:Script executed successfully, but there is no result to show. (此次执行2次)

db.events.where("properties.origin").eq("10.11.22.34").forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.properties.origin)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.34','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.properties = tmp ;
db.events.save(item);
} 
);

  

audits表:
-- audits.patch 执行到出现了:Script executed successfully, but there is no result to show.(此次执行15次)

db.getCollection('audits').find({'patch':/10.11.22.34/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.patch)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.34','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.patch = tmp ;
db.audits.save(item);
} 
);

  

-- 更新 db.identity_providers ,执行一遍即可

/* 更新之前的数据
db.identity_providers
{
"_id" : "demo",
"name" : "demo",
"description" : "demo第三方登录",
"enabled" : true,
"type" : "GRAVITEEIO_AM",
"configuration" : {
"scopes" : [
"openid"
],
"clientId" : "demo",
"serverURL" : "http://10.11.22.34/am/",
"clientSecret" : "demo",
"domain" : "demo",
"color" : "#3C3C3C"
},
"userProfileMapping" : {
"id" : "sub",
"firstname" : "given_name",
"lastname" : "family_name",
"email" : "email",
"picture" : "picture"
},
"createdAt" : ISODate("2019-09-12T14:57:27.294+08:00"),
"updatedAt" : ISODate("2019-09-12T14:57:53.805+08:00"),
"_class" : "io.gravitee.repository.mongodb.management.internal.model.IdentityProviderMongo"
}
*/
db.identity_providers.update({ _id: "demo" }, {
$set: {
"name": "demo",
"description": "apim",
"enabled": true,
"type": "GRAVITEEIO_AM",
"configuration": {
"scopes": [
"openid"
],
"clientId": "apim",
"clientSecret": "apim",
"serverURL": "http://10.11.22.36/am/",
"domain": "demo",
"color": "#3C3C3C"
},
"userProfileMapping": {
"id": "sub",
"firstname": "given_name",
"lastname": "family_name",
"email": "email",
"picture": "picture"
},
"createdAt": ISODate("2019-04-28T11:51:16.105+08:00"),
"updatedAt": ISODate("2019-04-28T12:17:06.238+08:00"),
"_class": "io.gravitee.repository.mongodb.management.internal.model.IdentityProviderMongo"
}
})

 

gravitee-am 数据库
-- reporters.configuration 执行到出现了:Script executed successfully, but there is no result to show.(此次执行3次)

db.getCollection('reporters').find({'configuration':/10.11.22.33/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.configuration)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.33','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.configuration = tmp ;
db.reporters.save(item);
} 
);

  

-- identities.configuration 执行到出现了:Script executed successfully, but there is no result to show.(此次执行3次)

db.getCollection('identities').find({'configuration':/10.11.22.33/}).forEach( 
function(item) {
print("===========START==============================") 
var tmp = String(item.configuration)
print(tmp) 
print("=========================================") 
tmp = tmp.replace('10.11.22.33','10.11.22.36')
print(tmp) 
print("============END==========================") 
item.configuration = tmp ;
db.identities.save(item);
} 
);

  

验证是否还有ip未被替换,执行以下语句查询所有的文本信息,然后在查询结果中全局搜索10.11.22.34:

var collections = db.getCollectionNames();
for(var i = 0; i< collections.length; i++){ 
print('Collection: ' + collections[i]); // print the name of each collection
db.getCollection(collections[i]).find().forEach(printjson);
}

  

步骤四
根据当前数据生成新的Token

http://10.11.22.36/am/demo/oauth/tokenbody
请求方式: POST
请求参数(body):
  grant_type:password
  client_id:webuser
  client_secret:webuser
  username:sz-webuser
  password:password111
  scope:openid

测试其它接口的可用性,需要登录 http://10.11.22.36/apim/portal  ,先deploy相应接口信息,再使用postmain测试接口。

 

 posted on 2020-05-19 13:09  阿叮339  阅读(655)  评论(0编辑  收藏  举报