ha-camel

高可用Redis

Usage no npm install needed!

<script type="module">
  import haCamel from 'https://cdn.skypack.dev/ha-camel';
</script>

README

ha-camel,快速构建高可用 Node Redis 服务

API

关于 logger

  • 推荐使用配套模块 ha-logger
  • 默认可以不传,模块内置了 simpleLogger
  • 如果不使用 ha-logger 也不使用内置的 simpleLogger,那么必须保证传入的 logger 模块有 infoerror 方法,来记录启动和异常 !

创建 Redis 实例

ha-camel.base(logger, conf, type)

参数 参数类型 含义
logger object 日志模块
conf object Redis 配置项
type string Redis 类型

base type

  • type fork Redis 单实例
  • type cluster Redis 单集群

base conf

  • 当 type 为 fork:

    • 参数:

      参数 含义
      port 端口
      host 地址
    • 示例:

    // 常用
    const conf = { port: '6379', host: '127.0.0.1' }
    // 有密码
    const conf = { port: '6379', host: '127.0.0.1', password: "pwd" }
    
  • 当 type 为 cluster:

    • 参数:

      参数 含义
      client (必填)配置项数组
      net (非必填)兼容 nat 映射模式时传参
      pwd (非必填)集群是否使用同一个通用密码时进行传参
    • 示例:

    // 常用
    const conf1 = {
      client: [
        { port: '7001', host: '127.0.0.1' },
        { port: '7002', host: '127.0.0.1' },
        { port: '7003', host: '127.0.0.1' }
      ]
    }
    // 需要配置密码
    const conf2 = {
      client: [
        { port: '7001', host: '127.0.0.1', password: 'password-for-30001' }, // 使用独立配置的密码
        { port: '7002', host: '127.0.0.1', password: null }, // 不使用密码
        { port: '7003', host: '127.0.0.1' }
      ], // 其他通用密码
      pwd: 'pwd'
    }
    // 兼容nat映射模式
    const conf3 = {
      client: [
        {
          host: '203.0.113.73',
          port: 30001
        }
      ],
      net: {
        '10.0.1.230:30001': { host: '203.0.113.73', port: '7001' },
        '10.0.1.231:30001': { host: '203.0.113.73', port: '7002' },
        '10.0.1.232:30001': { host: '203.0.113.73', port: '7003' }
      }
    }
    

 


 

创建 Redis 互斥锁

ha-camel.lock(logger, conf, type)

参数 参数类型 含义
logger object 日志模块
conf object Redis 配置项
type string Redis 实例

lock type

  • type fork Redis 单实例
  • type cluster Redis 单集群
  • type queue Redis 多实例 Redlock
  • type queueCluster Redis 多集群 Redlock

lock conf

  • 使用 forkcluster 注册单例锁时传参要求与 base conf 一致
  • 但是使用 queuequeueCluster注册Redlock服务时,需要传集合,且数量不能小于 3

例如:

  • queue 模式:

    // 常用
    const conf = [
      { port: '6379', host: '127.0.0.1' },
      { port: '6380', host: '127.0.0.1' },
      { port: '6381', host: '127.0.0.1' }
    ]
    
  • queueCluster 模式:

    // 常用
    const conf = [
      {
        client: [
          { port: '7001', host: '127.0.0.1' },
          { port: '7002', host: '127.0.0.1' },
          { port: '7003', host: '127.0.0.1' }
        ]
      },
      {
        client: [
          { port: '8001', host: '127.0.0.1' },
          { port: '8002', host: '127.0.0.1' },
          { port: '8003', host: '127.0.0.1' }
        ]
      },
      {
        client: [
          { port: '9001', host: '127.0.0.1' },
          { port: '9002', host: '127.0.0.1' },
          { port: '9003', host: '127.0.0.1' }
        ]
      }
    ]