繁体   English   中英

如何将 aws redis 与 c# 连接

[英]how to connection aws redis with c#

如何使用 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 秒,state:正在连接,管理器:10 个中的 10 个可用,最后一次心跳:从不,全局:5 秒前,v:2.1.58.34321,mc:1/1/0,管理器: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

我该如何解决?

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

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

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

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

Redis 安全组入站规则

在此处输入图像描述

.NET 内核示例代码

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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM