【问题标题】:Can't read Azure configuration settings from Node.js无法从 Node.js 读取 Azure 配置设置
【发布时间】:2015-05-15 14:57:19
【问题描述】:

我正在 Azure 中运行 Node.js 应用程序,并尝试获取如下配置设置:

var azure = require('azure');

azure.RoleEnvironment.getConfigurationSettings(function(error, settings) {
   if (error) {
      console.log(error);
      return;
   }

   console.log('Settings: %s', settings);
});

但输出总是这样:

{ "code": "ENOENT", "errno": "ENOENT", "syscall": "connect", "fileName": "\\\\.\\pipe\\WindowsAzureRuntime" }

我正在使用 IISNode 使用所有最新位在 IIS 中运行 Node.Js。如果我在 Azure VM(即 node.exe server.js)上手动运行节点,我也会遇到同样的错误。在我的 PC 上运行 Azure Development Fabric 时也是如此。

提前感谢您的任何帮助或建议!

【问题讨论】:

  • 找到了一个可行的解决方案,用于获取在 IISNode 中运行的 Node.Js 的 ServiceConfiguration.cscfg 设置值。例如,要使名为“TestSetting”的设置作为 IISNode 中的环境变量可供 Node 访问,必须将以下内容添加到 ServiceDefintion.csdef:<Runtime> <Environment> <Variable name="TestSetting"> <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='TestSetting']/@value" /> </Variable> </Environment> </Runtime>

标签: javascript node.js azure


【解决方案1】:

由于您提到您在 IISNode 中运行,因此它必须是 Web 角色。请注意SDK readme 中的以下内容:

服务运行时允许您与当前角色正在运行的机器环境进行交互。请注意,这些命令只有在您的代码在 Azure 模拟器或云中以辅助角色运行时才有效。

【讨论】:

  • 知道了,没见过。我现在正在通过启动任务作为工作角色启动节点对其进行测试。而且我可以通过环境设置将配置设置传递给节点,这很好。但是,即使作为工作角色运行,我仍然会遇到相同的错误。我看到其他人声称它可以工作,所以我一定遗漏了一些东西,但不知道是什么......
  • 我在这里工作(在我回复之前仔细检查)。你得到完全相同的错误?你不是想在那个 Worker Role 中运行 IISNode 吗?
  • 不,工作角色中没有 IISNode。 :) 只是通过启动任务启动它。因此,如果 Azure Node.JS 客户端无法访问 Azure 配置设置,那么在 IISNode 中运行时向节点提供配置设置的推荐方法是什么?显然,将设置放在 web.config 中是不够的,因为我无法在不创建新包的情况下更改它,并且在 IISNode 下运行时节点不会通过启动任务启动,所以我无法以这种方式提供设置......不知道如何否则要做...?
  • 在工作角色下运行时,控制台写入的错误实际上看起来像这样Error: connect ENOENT,输出不完全相同,但似乎是相同的错误。但我现在调用 azure.RoleEnvironment.isAvailable(...),它会返回该错误并为第二个 isAvailable 参数返回“false”。但我并不关心如何获得配置值,但如果可能的话,我更愿意坚持使用 IISNode。因此,如果 Azure Node.js 客户端不这样做,我很乐意采取另一条路线。
  • 是正在运行开发结构还是已部署?老实说,我没有尝试部署它,但让它在模拟器中工作(而 Web 角色没有)。在内部 ping 一些人,看看我是否能得到答案,为什么 IISNode 是这里的一个因素。
【解决方案2】:

这是我的解决方案。这不好,但至少现在可以。

  1. 像这样创建一个 .NET 控制台应用程序

    使用 Microsoft.WindowsAzure; 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.Threading; 使用 System.Threading.Tasks;

    命名空间 CloudConfigurationHelper { 课堂节目 { 静态 int MaxRetryTime = 10;

        public static Dictionary<string, string> GetSettings(string[] keys)
        {
            Dictionary<string, string> settings = new Dictionary<string, string>();
    
            try
            {
                foreach (string key in keys)
                {
                    settings[key] = CloudConfigurationManager.GetSetting(key);
                }
            }
            catch
            {
                MaxRetryTime--;
    
                if (MaxRetryTime <= 0)
                {
                    return settings;
                }
    
                Thread.Sleep(2000);
                return GetSettings(keys);
            }
    
            return settings;
        }
    
        static int Main(string[] args)
        {
            var settings = GetSettings(args);
            if (settings.Count != args.Length)
            {
                return -1;
            }
    
            foreach (string key in settings.Keys)
            {
                Console.WriteLine(key + "=" + settings[key]);
            }
    
            return 0;
        }
    }
    

    }

  2. 将启动任务设置为读取 azure 配置变量并写入 .env 文件。使用https://github.com/motdotla/dotenv 读取.e​​nv 文件并加载到process.env。

Env.cmd 文件:

@echo off
cd %~dp0
CloudConfigurationHelper.exe %*

在ServiceDefinition.csdef中添加启动任务:

<Task commandLine="Env.cmd YOUR_SETTING_KEY &gt; ..\.env executionContext="elevated" />
  1. 在NodeJS web角色中,我们只需要通过process.env["YOUR_SETTING_KEY"]读取这个变量

【讨论】:

  • 应该是 process.env 而不是 .evn 吗?
猜你喜欢
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 2021-02-15
  • 2014-11-03
  • 2010-12-30
  • 2022-07-21
相关资源
最近更新 更多