【Azure Redis 快取】使用開源工具redis-copy時遇見6379埠無法連線到Redis伺服器的問題

路邊兩盞燈發表於2023-05-15

問題描述

當使用Azure Redis服務時,需要把一個Redis服務的資料匯入到另一個Redis上,因為Redis服務沒有使用高階版,所以不支援直接匯入/匯出RDB檔案。

以程式設計方式來讀取資料並寫入到新的Redis服務端,使用開源工具 Redis-Copy 卻遇見了 6379 埠無法連線的問題。而用 redis-cli.exe 卻正常連線。

redis-copy 工具使用 6379 埠

redis-copy.exe

--se xxxxx.redis.cache.chinacloudapi.cn --sa <your source password> --sp 6379 --sssl false

--de xxxxx.redis.cache.chinacloudapi.cn --da <your destination password> --dp 6379 --dssl false

報錯:

  • UnableToConnect on xxxxxxxx.redis.cache.chinacloudapi.cn:6379/Interactive 
  • No connection is available to service this operation 
  • It was not possible to connect to the redis server.

【Azure Redis 快取】使用開源工具redis-copy時遇見6379埠無法連線到Redis伺服器的問題

【Azure Redis 快取】使用開源工具redis-copy時遇見6379埠無法連線到Redis伺服器的問題

Redis-cli.exe 工具使用 6379 埠,正常連線

redis-cli.exe -h yourcachename.redis.cache.chinacloudapi.cn -p 6379 -a YourAccessKey

【Azure Redis 快取】使用開源工具redis-copy時遇見6379埠無法連線到Redis伺服器的問題

那麼,這是什麼情況呢?如何才能正確使用 redis-copy.exe 工具呢?

 

問題解答

根據 redis-cli.exe 工具的驗證,Redis伺服器的 6379埠在同一個客戶端機器上,是可以正常連線的。那麼問題就需要轉移到 redis-copy.exe 的這個開源工具上來研究了。

第一步:去 github 上下載 redis-copy的原始碼:https://github.com/deepakverma/redis-copy

第二步:本地Visual Studio 工具開啟後,把啟動指令後面攜帶的引數填入Debug Start options中

【Azure Redis 快取】使用開源工具redis-copy時遇見6379埠無法連線到Redis伺服器的問題

第三步:除錯程式碼,發現問題根源是SSL的引數值依舊為True,而埠為 6379。 用SSL的方式去連結非SSL埠,這就是問題的根源。

【Azure Redis 快取】使用開源工具redis-copy時遇見6379埠無法連線到Redis伺服器的問題

問題出現在 CommandLine.Parser.Default.ParseArguments<Options>(args) 這句程式碼上,經過反覆實現,發現CommandLine在轉換 bool 型別的時候,只要攜帶了這個引數,不管內容是什麼,都會被轉換為 true

第四步:解決辦法

最快的解決辦法 ---- 使用6380埠連線

redis-copy.exe

 --se xxxxx.redis.cache.chinacloudapi.cn --sa <your source password> --sp 6380  

 --de xxxxx.redis.cache.chinacloudapi.cn --da <your destination password>  --dp 6380 

 

修改Redis-Copy原始碼 ---- 解決SSL賦值問題

[主要]方案一:在Options.cs 檔案中,修改 SourceSSL 和 DestinationSSL 的預設值為False。當需要使用6380埠連線時,攜帶 --sssl , --dssl引數

        [Option("sssl", Required = false, Default = false, HelpText = "Connect Source over ssl" )]
        public bool SourceSSL { get; set; }

... ...

       [Option("dssl", Required = false, Default = false, HelpText = "Destination Source over ssl" )]
        public bool DestinationSSL { get; set; }

修改程式碼,重新編譯exe檔案後。

使用6379埠的命令為: redis-copy.exe  --se xxxx --sa **** --sp 6379  --de xxxx --da **** --dp 6379  

使用6380埠的命令為: redis-copy.exe  --se xxxx --sa **** --sp 6380 --sssl true  --de xxxx --da **** --dp 6380 --dssl true  

 

[其他]方案二:在Options.cs 檔案中,修改 SourceSSL 和 DestinationSSL 的型別為String,然後再初始化Redis連線字串的時候轉換為bool型別。

        [Option("sssl", Required = false, Default = true, HelpText = "Connect Source over ssl" )]
        public string SourceSSL { get; set; }

... ...

        [Option("dssl", Required = false, Default = true, HelpText = "Destination Source over ssl" )]
        public string DestinationSSL { get; set; }

.... ....

            ConfigurationOptions configsource = new ConfigurationOptions();
            configsource.Ssl =Convert.ToBoolean(options.SourceSSL);
            configsource.Password = options.SourcePassword;
            configsource.AllowAdmin = true;
            configsource.SyncTimeout = 60000; // increasing timeout for source for SCAN command
            sourcecon = GetConnectionMultiplexer(options.SourceEndpoint, options.SourcePort, configsource);

... ...

        ConfigurationOptions configdestination = new ConfigurationOptions();
            configdestination.Ssl = Convert.ToBoolean(options.DestinationSSL);
            configdestination.Password = options.DestinationPassword;
            configdestination.AllowAdmin = true;
            destcon = GetConnectionMultiplexer(options.DestinationEndpoint, options.DestinationPort, configdestination);

 

參考資料

以程式設計方式遷移 : https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#migrate-programmatically 

使用 Redis 命令列工具進行連線: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-how-to-redis-cli-tool#connect-using-the-redis-command-line-tool

redis-copy : https://github.com/deepakverma/redis-copy
 

 

 
 

相關文章