无法使用JS从我的功能应用程序连接到Redis缓存

问题描述:

我无法从我的功能应用程序连接到Redis缓存。我使用node作为开发运行时并直接在门户上进行开发。如果我尝试连接来自本地机器的独立节点JS程序
的redis缓存,它就会连接。

我在尝试执行教程时收到以下响应:  https://docs.microsoft.com/en-us/azure/azure-cache-for-redis / cache-nodejs-get-started

I am getting the below response when I try execute the tutorial: https://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-nodejs-get-started





函数继续运行,在控制台中我得到以下红色信息:

The function keeps running, and in console I get the following message in red:


function(t){if(void 0 === t&&(t =" legacy")),this._body instanceof b)返回this._body.toString(); if( this._body instanceof ArrayBuffer)switch(t){case" legacy":return String.fromCharCode.apply(null,new Uint16Array(this._body)); case" iso-8859":return
String.fromCharCode。 apply(null,new Uint8Array(this._body)); default:throw new Error(" encodingHint的值无效:" + t)} return null == this._body?"":" object" = = typeof this._body?JSON.stringify(this._body,null,2):this._body.toString()}

function(t){if(void 0===t&&(t="legacy"),this._body instanceof b)return this._body.toString();if(this._body instanceof ArrayBuffer)switch(t){case"legacy":return String.fromCharCode.apply(null,new Uint16Array(this._body));case"iso-8859":return String.fromCharCode.apply(null,new Uint8Array(this._body));default:throw new Error("Invalid value for encodingHint: "+t)}return null==this._body?"":"object"==typeof this._body?JSON.stringify(this._body,null,2):this._body.toString()}

我尝试从我的azure函数连接到Redis缓存并且它工作正常。以下是我使用的步骤:

I tried connecting to the Redis Cache from my azure Function and it worked. Below are the steps that I used:

1.从门户创建了node.js azure函数。

1. Created a node.js azure function from portal.

2.浏览kudu控制台您的功能应用程序(https://< YourFunctionAppName> .scm.azurewebsites.net / DebugConsole) - >  D:\ home \site \wwwroot \< YourFunctionName>

2. Browse to kudu console your funcion app (https://<YourFunctionAppName>.scm.azurewebsites.net/DebugConsole) --> D:\home\site\wwwroot\<YourFunctionName>

3.运行node命令安装redis和bluebird模块为" npm
install redis -f
"&" npm install bluebird "

3. Run the node command to install redis and bluebird module as "npm install redis -f" & "npm install bluebird"

4.在您的功能应用中添加以下应用设置。

4. Add a below App setting to your Function App.

   REDISCACHEHOSTNAME =< Redis缓存主机名>

  REDISCACHEHOSTNAME = <Redis Cache Host Name>

   REDISCACHEKEY =< Redis缓存密钥>

  REDISCACHEKEY = <Redis Cache Key>




4.使用下面的代码示例连接到Azure Redis:

4. Used the below code sample to connect to Azure Redis :

var redis = require("redis");
var bluebird = require("bluebird");

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

var cacheConnection = redis.createClient(6380, GetEnvironmentVariable("REDISCACHEHOSTNAME"), 
        {
            auth_pass: GetEnvironmentVariable("REDISCACHEKEY"), 
            tls: {
                servername: GetEnvironmentVariable("REDISCACHEHOSTNAME")
            },
             retry_strategy: function (options) {
        if (options.error && options.error.code === 'ECONNREFUSED') {
            // End reconnecting on a specific error and flush all commands with
            // a individual error
            return new Error('The server refused the connection');
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
            // End reconnecting after a specific timeout and flush all commands
            // with a individual error
            return new Error('Retry time exhausted');
        }
        if (options.attempt > 10) {
            // End reconnecting with built in error
            return undefined;
        }
        // reconnect after
        return Math.min(options.attempt * 100, 3000);
            }});


module.exports = async function (context, myTimer) {
   // Connect to the Azure Cache for Redis over the SSL port using the key.
  
    // Simple PING command
    context.log("\nCache command: PING");
    context.log("Cache response : " + await cacheConnection.pingAsync());

    // Simple get and put of integral data types into the cache
    context.log("\nCache command: GET Message");
    context.log("Cache response : " + await cacheConnection.getAsync("Message"));    

    context.log("\nCache command: SET Message");
    context.log("Cache response : " + await cacheConnection.setAsync("Message",
        "Hello! The cache is working from Node.js!"));    

    // Demonstrate "SET Message" executed as expected...
    context.log("\nCache command: GET Message");
    context.log("Cache response : " + await cacheConnection.getAsync("Message"));    

    // Get the client list, useful to see if connection list is growing...
    context.log("\nCache command: CLIENT LIST");
    context.log("Cache response : " + await cacheConnection.clientAsync("LIST"));

};

function GetEnvironmentVariable(name)
{
    return process.env[name];
}