如何解決hangfire使用redis存儲時,若是採用了prefix報「Key has MOVED from Endpoint」的錯

 

當咱們使用redis做爲hangfire的存儲時,若是使用了下面紅色字體的配置html

 

 GlobalConfiguration.Configuration.UseRedisStorage(@"192.168.0.3:7002",node

                                                               new Hangfire.Redis.RedisStorageOptionsredis

                                                               {服務器

                                                                    Prefix = "{hangfire-A}:",app

                                                                   // Db =2ide

                                                               });字體

            app.UseHangfireDashboard();ui

 

則你頗有可能會看到下面的錯誤url

Key has MOVED from Endpoint 127.0.0.1:7001 and hashslot 6991 but CommandFlags.NoRedirect was specified - redirect not followed for MGET. IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=32766,Min=4,Max=32767), Local-CPU: n/aspa

 

 

在Hangfire的官網,對這種現象有一個說明,但經常被咱們忽略掉。具體說明以下

 

Redis Cluster support

You can use a single endpoint to connect to a Redis cluster, Hangfire will detect other instances automatically by querying the node configuration. However, it’s better to pass multiple endpoints in order to mitigate connectivity issues, when some of endpoints aren’t available, e.g. during the failover process.

Since Hangfire requires transactions, and Redis doesn’t support ones that span multiple hash slots, you also need to configure the prefix to assign it to the same hash tag:

GlobalConfiguration.Configuration.UseRedisStorage(
    "localhost:6379,localhost:6380,localhost:6381",
    new RedisStorageOptions { Prefix = "{hangfire-1}:" });

This will bind all the keys to a single Redis instance. To be able to fully utilize your Redis cluster, consider using multiple JobStorage instances and leveraging some load-balancing technique (round-robin is enough for the most cases). To do so, pick different hash tags for different storages and ensure they are using hash slots that live on different masters by using commands CLUSTER NODES and CLUSTER KEYSLOT.

 

來自 <http://docs.hangfire.io/en/latest/configuration/using-redis.html>

 

 

文章中明確說明,這種操做會讓hangfire的key被綁定到一個單獨的redis實例上。這個實例就是咱們須要配置的鏈接地址(注:這裏若是配置集羣的地址,同樣會出錯,只能配置綁定的那個地址)。但怎麼找到這個地址呢?

 

首先,咱們使用CLUSTER NODES命令查看每一個節點的hash slots分佈狀況,效果以下:

 

 

 

上圖顯示了每一個節點的分佈狀況,而後咱們須要使用CLUSTER KEYSLOT 命令找出咱們須要作爲key的值會hash成什麼值,而後找到對應值分配 的服務器。 如上「hangfire-A" hash後對應的slor是16173, 對應的是7200端口對應的節點。

 

因此在代碼中,咱們配置服務器地址時就要使用這個端口。具體以下:

 GlobalConfiguration.Configuration.UseRedisStorage(@"192.168.0.3:7002",

                                                               new Hangfire.Redis.RedisStorageOptions

                                                               {

                                                                    Prefix = "{hangfire-A}:",

                                                                   // Db =2

                                                               });

 

而後咱們的服務就能夠正常起動了

相關文章
相關標籤/搜索