Azure WorkerRole 可以检测到它所在的位置吗?

发布于 2024-11-16 06:48:45 字数 333 浏览 3 评论 0原文

我的问题的意思是:是否可以检测辅助角色部署在哪个数据中心?我正在尝试以编程方式确定我的辅助角色是否位于北美和欧洲 Azure 数据中心,以便连接到位于同一地理位置的数据库。

例如,是否有环境变量?或者也许我可以根据公共 IP 地址获取此信息?我愿意接受任何替代方案。

我问这个问题的原因是因为我想在北美数据中心部署完全相同的辅助角色,并在欧盟和最终亚太地区部署它,但我希望该角色连接到最近的数据库。我试图避免在 .CFG 中手动指定连接字符串,因为我担心有一天有人会将代码部署到 EU 并忘记调整连接字符串。我的目标是让所有连接字符串都出现在 .CFG 中,并且我的辅助角色足够聪明,可以根据其位置选择合适的连接字符串。

What I mean by my question is: is it possible to detect which datacenter the worker role is deployed in? I'm trying to programatically figure out if my worker role is in a North American versus European Azure datacenter for the purpose of connecting to the database located in the same geography.

For example, is there an environment variable? Or maybe I could derive this information based on the public IP address? I'm willing to entertain any alternative.

The reason I'm asking this question is because I would like to deploy the exact same worker role in a NA datacenter and also deploy it in EU and eventually also in APAC but I want the role to connect to the closest database. I am trying to avoid having to manually specify the connection string in the .CFG because I am afraid one day someone will deploy the code to the EU and forget to adjust the connection string. My goal is to have all the connections strings to be present in the .CFG and my worker role to be smart enough to select the appropriate one based on its location.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-11-23 06:48:45

是的,但您必须使用服务管理 API。要在没有任何配置设置的情况下执行此操作,您需要首先枚举托管服务,然后为每个调用获取托管服务属性 (http://msdn.microsoft.com/en-us/library/ee460806.aspx)。您需要将 RoleEnvironment 中的 DeploymentId 与此响应中的 PrivateId 相匹配。这会告诉您正在运行哪个托管服务。在同一个响应中还有位置。

现在,有一种更简单的方法。只需将值放入 ServiceConfiguration 中,并根据部署的不同而有所不同!

Yes, but you would have to use the Service Management API. To do this without any configuration settings, you would first enumerate the hosted services and then for each one call for Get Hosted Services Properties (http://msdn.microsoft.com/en-us/library/ee460806.aspx). You would need to match the DeploymentId you have in your RoleEnvironment to the PrivateId in this response. That would tell you which hosted service you are running under. In that same response is the location.

Now, there is an even easier method. Just put the value in the ServiceConfiguration and have that differ by deployment!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文