redis-copy使用6379端口無法連接到Redis服務器的問題
問題描述
當使用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.


Redis-cli.exe 工具使用 6379 端口,正常連接
redis-cli.exe -h yourcachename.redis.cache.chinacloudapi.cn -p 6379 -a YourAccessKey

那么,這是什么情況呢?如何才能正確使用 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中

第三步:調試代碼,發(fā)現問題根源是SSL的參數值依舊為True,而端口為 6379。 用SSL的方式去鏈接非SSL端口,這就是問題的根源。

問題出現在 CommandLine.Parser.Default.ParseArguments<Options>(args) 這句代碼上,經過反復實現,發(fā)現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
redis-copy : https://github.com/deepakverma/redis-copy
到此這篇關于redis-copy使用6379端口無法連接到Redis服務器的問題的文章就介紹到這了,更多相關redis-copy無法連接到Redis服務器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談Redis位圖(Bitmap)及Redis二進制中的問題
這篇文章主要介紹了Redis位圖(Bitmap)及Redis二進制中的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

