【问题标题】:Multiple Config in JSON for Node.js用于 Node.js 的 JSON 中的多个配置
【发布时间】:2020-05-09 08:49:35
【问题描述】:

我不知道给我的问题取什么标题。 这是对 node.js 的一次冒险,一位乐于助人的人向我介绍了 ioredis。目前我有:

var Redis = require("ioredis");
const DBConfig = require(__dirname+'/../Config.json');
var cluster = new Redis.Cluster([
    {
        port: 6001,
        host: "10.0.0.6",
    },
    {
        port: 6002,
        host: "10.0.0.5",
    },
    {
        port: 6003,
        host: "10.0.0.4",
    },
    {
        port: 6004,
    host: "10.0.0.3",
},
{
    port: 6005,
    host: "10.0.0.2",
    },
    {
        port: 6006,
        host: "10.0.0.1",
    },

]);

但对我来说,这似乎在 json 配置文件中会更好......

Config.json:

{
    "configA" : "abc",
    "someotherconfigB" : "Stuff",
    "foo" : "bar"
}
{
        "port": 6001,
        "host": "10.0.0.6",
    },
    {
        "port": 6002,
        "host": "10.0.0.5",
    },
    {
        "port": 6003,
        "host": "10.0.0.4",
    },
    {
        "port": 6004,
        "host": "10.0.0.3",
    },
    {
        "port": 6005,
        "host": "10.0.0.2",
    },
    {
        "port": 6006,
        "host": "10.0.0.1",
    },
}

我太新了,我只是不确定如何在没有语法错误的情况下实现它。

var Redis = require("ioredis");
const DBConfig = require(__dirname+'/../Config.json');
var cluster = new Redis.Cluster([DBConfig.redis]);

我不确定如何实现“var cluster = new Redis.Cluster([DBConfig.redis]);”正确的

【问题讨论】:

    标签: node.js json


    【解决方案1】:

    您应该将这些设置声明为 arraykey

    {
      "configA" : "abc",
      "someotherconfigB" : "Stuff",
      "foo" : "bar",
      "redisCluster": [
        {
          "port": 6001,
          "host": "10.0.0.6"
        },
        {
          "port": 6002,
          "host": "10.0.0.5"
        },
        {
          "port": 6003,
          "host": "10.0.0.4"
        }
      ]
    }
    
    

    然后使用该键访问所需配置文件中的值。

    const DBConfig = require('../Config.json');
    const cluster = new Redis.Cluster(DBConfig.redisCluster);
    

    【讨论】:

    • 怎么把密码传进去?
    • @user557657 您可以在hostport 旁边定义password 字段
    • 我可以连接到集群,但我无法查询或设置密钥。尝试了简单的查询,但它挂起cluster.get("name", (err, res) => { console.log(res); });
    • 我们可以对主从使用相同的代码吗?具有 2 个从节点的主节点。
    【解决方案2】:

    首先,您需要有一个适当的配置文件。您的文件似乎包含一些配置信息和节点信息。我建议:

    Config.json文件:

    {
      "configs": {
        "configA": "abc",
        "someotherconfigB": "Stuff",
        "foo": "bar"
      },
      "nodes": [
        {
          "port": 6001,
          "host": "10.0.0.6"
        },
        {
          "port": 6002,
          "host": "10.0.0.5"
        },
        {
          "port": 6003,
          "host": "10.0.0.4"
        },
        {
          "port": 6004,
          "host": "10.0.0.3"
        },
        {
          "port": 6005,
          "host": "10.0.0.2"
        },
        {
          "port": 6006,
          "host": "10.0.0.1"
        }
      ]
    }
    
    

    那么你的文件应该是这样的:

    const Redis = require('ioredis');
    const DBConfig = require(__dirname + '/Config.json');
    
    const cluster = new Redis.Cluster(DBConfig.nodes);
    
    Object.entries(DBConfig.configs).map(([key, value]) => {
      cluster.set(key, value);
    });
    
    

    DBConfig.nodes 它已经是一个数组了。不需要在它周围加上括号

    Object.entries(DBConfig.configs) 将为您提供一个由您的DBConfig.configs 属性组成的 [key, value] 对数组

    资源:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多