• serviceStack.Redis 在PooledRedisClientManager 中设置密码


    ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能。但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置,我通过一个缓存工厂来获取连接。在redisClient实例化可以直接设置密码。

     1  /// <summary>
     2     /// 缓存客户端管理器工厂
     3     /// </summary>
     4     public class PoolManagerFactory
     5     {
     6         private static PooledRedisClientManager Manager = null;
     7         public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts, int initialDB = 0)
     8         {
     9             if (Manager == null)
    10             {
    11                 Manager = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig()
    12                 {
    13                     MaxWritePoolSize = 5,
    14                     MaxReadPoolSize = 5,
    15                     AutoStart = true
    16                 }, initialDB, 50, 5);
    17             }
    18             return Manager;
    19         }
    20 
    21     }

    我一直认为readWriteHosts数组中只能输入ip:port来代表一个redis 实例的连接,但是如何把密码加在里面呢?想不到如何实现,只能下载了源代码查看,原来这个实现是通过分隔字符串来实现,

     1     /// <summary>
     2         /// IP地址中可以加入auth验证   password@ip:port
     3         /// </summary>
     4         /// <param name="hosts"></param>
     5         /// <returns></returns>
     6         public static List<RedisEndpoint> ToRedisEndPoints(this IEnumerable<string> hosts)
     7         {
     8             if (hosts == null) return new List<RedisEndpoint>();
     9             //redis终结点的列表
    10             var redisEndpoints = new List<RedisEndpoint>();
    11             foreach (var host in hosts)
    12             {
    13                 RedisEndpoint endpoint;
    14                 string[] hostParts;
    15                 if (host.Contains("@"))
    16                 {
    17                     hostParts = host.SplitOnLast('@');
    18                     var password = hostParts[0];
    19                     hostParts = hostParts[1].Split(':');
    20                     endpoint = GetRedisEndPoint(hostParts);
    21                     endpoint.Password = password;
    22                 }
    23                 else
    24                 {
    25                     hostParts = host.Split(':');
    26                     endpoint = GetRedisEndPoint(hostParts);
    27                 }
    28                 redisEndpoints.Add(endpoint);
    29             }
    30             return redisEndpoints;
    31         }

    在ip:port前面加上@用来表示密码,比如password@ip:port    ,现在才知道能看到源码的程序是多么的幸福的一件事。开源伟大。

    master:设置密码:config set requirepass password

    slave 指定master密码:config set masterauth password   就可以实现在master设置密码,并且不需要重启redis实例 非常方便,但是这种方式重启后失效。

  • 相关阅读:
    快速排序算法
    学习了几天的jQuery Mobile的一点感受
    jQuery Mobile 图标无法显示
    html5基础知识
    百度地图与谷歌地图 (常识、区别,更倾向于使用百度地图,纠错信息比谷歌多)
    (转)百度Map API
    历年软件设计师下午考试试题汇总统计
    HTML5的本地存储
    EnterpriseArchitectect 软件的勾选的几个选项对应的中文意思
    访问权限控制表
  • 原文地址:https://www.cnblogs.com/jiagoushi/p/4024530.html
Copyright © 2020-2023  润新知