【问题标题】:how to connection aws redis with c#如何将 aws redis 与 c# 连接起来
【发布时间】:2020-11-13 12:00:12
【问题描述】:

如何使用c#连接aws redis?

我创建 aws redis 完成并创建 linux 主机以连接 redis 成功,

但是当我使用 c# 代码连接时,我会失败。

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect("test.qzgpeh.ng.0001.use2.cache.amazonaws.com:6379,abortConnect=false");
    });

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }

错误信息

没有连接是活动的/可用于服务此操作:GET hello; UnableToConnect on test.qzgpeh.ng.0001.use2.cache.amazonaws.com:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, 未完成: 0, last-read: 5s ago, last-write: 5s前,保持活动状态:60 秒,状态:正在连接,管理器:10 个中的 10 个可用,最后一次心跳:从不,全局:5 秒前,v:2.1.58.34321,mc:1/1/0,管理器:10 个中的 10 个可用, 客户端名称: HAUSER, IOCP: (Busy=0,Free=1000,Min=12,Max=1000), WORKER: (Busy=0,Free=32767,Min=12,Max=32767), v: 2.1.58.34321

我该如何解决?

【问题讨论】:

  • 您也应该发布错误消息。
  • 好的,请检查新帖子
  • 你的redis服务器使用TLS吗?如果是这样,您需要在配置字符串中告诉它。指定 TLS 版本可能也是值得的,因为在过去的几个月里,这一直是一个很大的痛点(通常在 IT 中,而不是专门针对这种情况)。如果您不想猜测/查找配置字符串键,您可以使用ConfigurationOptions API,它为您提供了一个带有属性的对象模型。
  • 此外,连接问题的一个很好的调试技巧是将TextWriter 作为可选的log 参数传递。这样你可能会得到更详细的输出。我猜你的服务器也需要密码? (同样,在配置字符串或ConfigurationOptions 对象上)

标签: c# redis amazon-elastic-beanstalk


【解决方案1】:

如果有人遇到类似问题,请发帖。

我试图从 Elastic Beanstalk .Net 应用程序访问 Redis 数据并遇到类似问题。

就我而言,这是来自安全组的问题。我按照以下步骤操作

  1. 创建我的 VPC 以及子网和安全组
  2. 在 EBS 中创建和部署应用程序。
  3. 创建一个安全组(用于 Redis)并允许来自 EBS 安全组的连接
  4. 在同一 VPC 和安全组中创建 Redis(第 3 步安全组)
  5. 能够从 .Net 应用程序访问 Redis

Redis 安全组入站规则

.NET Core 示例代码

public class PrivacyModel : PageModel
{
    private readonly ILogger<PrivacyModel> _logger;
    private readonly IDatabase cache;
    private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect("******-redis-cache.yq8meb.0001.aps1.cache.amazonaws.com:6379,abortConnect=false");
    });

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }

    public PrivacyModel(ILogger<PrivacyModel> logger)
    {
        _logger = logger;
        this.cache = Connection.GetDatabase();
    }

    [BindProperty]
    public string StartTime { get; set; }
    public void OnGet()
    {
        StartTime = this.cache.StringGet("StartTime");
        if (string.IsNullOrWhiteSpace(StartTime))
        {
            StartTime = DateTime.Now.ToString();
            this.cache.StringSet("StartTime", StartTime);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2017-10-21
    • 2018-03-14
    • 2015-10-07
    • 2021-11-08
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多