当咱们使用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( 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 }); |
而后咱们的服务就能够正常起动了