如何从我的Jelastic环境中查询内部部署的API



我正在编写一个Jelastic清单,在其中部署两个节点。在一个节点上,我有一个API,我需要查询它来设置第二个节点。大致如下:

type: install
name: My test
nodes:
- count: 1
cloudlets: 4
nodeGroup: auth
nodeType: docker
image: my-api:latest
- count: 1
cloudlets: 16
nodeGroup: cp
nodeType: docker
image: some-service:latest
onInstall:
- script: |
import com.hivext.api.core.utils.Transport;

try {
const body = new Transport().get("http://${nodes.auth.master.intIP}:9011/api/key", {
"Authorization": "my-api-key"
});    

return { result: 0, body: body };
} catch (e) {
return {
type: "error",
message: "unknown error: " + e
};
}

在我的脚本中,当我进行时

const body = new Transport().get("http://www.google.com"); 

它工作,我得到的谷歌页面的正文内容。然而,

const body = new Transport().get("http://${nodes.auth.master.intIP}:9011/api/key", {
"Authorization": "my-api-key"
});

返回

ERROR: script.response: {"type":"error","message":"unknown error: JavaException: java.io.IOException: Failed to select a proxy"}

我做错了什么?如何在上面的脚本中查询我的服务?当我通过常规cmdcurl时,它就工作了:

curl -s -H "Authorization: my-api-key" http://${nodes.auth.master.intIP}:9011/api/key

另外,顺便提一下,我在哪里可以找到com.hivext.api.core.utils.Transport的文档?

您不能通过脚本的内部IP访问您的环境节点(至少不能保证(。作为一种变通方法,您可以通过公共IP或端点访问节点。如果公共IP或端点不适用于您的情况,并且您的服务只能在内部访问,您可以尝试通过curl和ExecCmd API访问您的节点。例如:

type: install
name: My test
nodes:
- count: 1
cloudlets: 4
nodeGroup: auth
nodeType: docker
image: my-api:latest
- count: 1
cloudlets: 16
nodeGroup: cp
nodeType: docker
image: some-service:latest

onInstall:
- script: |
function execInternalApi(nodeId, url) {
let resp = api.env.control.ExecCmdById({
envName: '${env.name}', 
nodeId: nodeId,
commandList: toJSON([{
command: 'curl -fSsl -H "Authorization: my-api-key" '' + url + '''
}])
})

if (resp.result != 0) return resp

return { result: 0, out: resp.responses[0].out }
}

let body = execInternalApi(${nodes.auth.master.id}, 'http://localhost:9011/api/key');

return { result: 0, body: body };