簡體   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