Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postman如何配置动态端口和IP #56

Open
superleeyom opened this issue Dec 14, 2023 · 0 comments
Open

Postman如何配置动态端口和IP #56

superleeyom opened this issue Dec 14, 2023 · 0 comments
Assignees

Comments

@superleeyom
Copy link
Owner

背景

起因是每次使用 Postman 调试服务接口的时候,如果服务一旦被重启,对应服务的ip和端口就会改变,就需要重新配置服务的ip和端口,非常的繁琐以及麻烦。所以就针对这个问题,做了一点研究,简单的记录下。

步骤

1、维护多套环境及相应的环境变量

实际开发中,我们一般会有多套环境,比如:开发环境dev、测试环境test、生产环境prod等等。每个环境,我们配置好对应的consul(注册中心)环境变量、对应服务名环境变量,如图所示:

image.png

2、维护collection目录层级以及脚本

建议以服务名为目录,在对应的目录层级,维护如下的脚本,动态的获取注册中心上对应的指定服务的ip和端口:

pm.sendRequest(pm.environment.get("consul_service") + "trade-assets-service",
function(err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.environment.set("trade-assets-service", res.json()[0].ServiceAddress + ":" + res.json()[0].ServicePort);
    }
});

image.png
注: "trade-assets-service" 根据自己目录名变化

3、在对应的collection目录模拟request请求

维护好了动态获取脚本后,就可以正常的模拟对应的request请求,请求里面的ip和端口只需要填充对应的服务的占位符即可:

image.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant