[英]Can't connect to clustered Azure Redis Cache with redis-rb gem and public access
我们已经使用高级层配置了 Azure Redis 缓存服务器。 服务器是集群的,有 2 个分片,服务器被配置为允许通过防火墙通过公共 inte.net 进行公共访问——允许列出一组已知 IP。
部署我们的 Rails 应用程序两个这些已知 IP 失败并出现错误:
Redis::CannotConnectError: Redis client could not connect to any cluster nodes
这是我们的 Rails 配置:
# application.rb
if Rails.env.test?
config.cache_store = :redis_cache_store, {
url: config.nines[:redis_url],
expires_in: 90.minutes,
namespace: ENV['TEST_ENV_NUMBER'],
}
else
config.cache_store = :redis_store, {
namespace:ENV['TEST_ENV_NUMBER'],
cluster: [ Rails.application.config.nines[:redis_url] ],
replica: true, # allow reads from replicas
compress: true,
compress_threshold: 1024,
expires_in: 90.minutes
}
end
config.nines[:redis_url]
设置如下: rediss://:<pw>@<cache-name>.redis.cache.windows.net:6380/0
然后,我们在代码中初始化 Redis 连接,如下所示:
if Rails.env.test?
::Redis.new :url => redis_url, :db => ENV['REDIS_DB']
else
::Redis.new(cluster: [ "#{redis_url}" ], db: ENV['REDIS_DB'])
end
我们正在使用redis-rb
gem 和redis-rails
gem。
如果有人能指出我们做错了什么,请分享!
谢谢用户 Peter Pan - Stack Overflow 。 将您的建议作为答案发布,以帮助其他社区成员。
您可以尝试以下代码:
# Import the redis library for Ruby
require "redis"
# Create a redis client instance for connecting Azure Redis Cache
# At here, for enabling SSL, set the `:ssl` symbol with the
# symbol value `:true`, see https://github.com/redis/redis-rb#ssltls-support
redis = Redis.new(
:host => '<azure redis cache name>.redis.cache.windows.net',
:port => 6380,
:db => <the db index you selected like 10>,
:password => "<access key>",
:ssl => :true)
# Then, set key `foo` with value `bar` and return `OK`
status = redis.set('foo', 'bar')
puts status # => OK
# Get the value of key `foo`
foo = redis.get('foo')
puts foo # => bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.