You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产环境ActionCable连接Redis报String转Integer类型错误求助

问题分析与解决方案

错误根源

no implicit conversion of String into Integer 这个错误几乎都是Redis连接配置中,端口号被当作字符串传递,而Redis客户端需要整数类型导致的。Staging环境正常但生产环境出错,大概率是生产环境的Redis URL解析逻辑有差异,或者配置文件里的参数处理不当。

排查与修复步骤

1. 修正Redis配置文件(redis.rb)

避免手动拆分Redis连接参数,直接用Heroku提供的完整REDIS_URL初始化客户端,防止端口被当作字符串传递:

# config/initializers/redis.rb
require 'redis'

redis_url = ENV['REDIS_URL'] || 'redis://localhost:6379/0'
$redis = Redis.new(url: redis_url)

# 同步Sidekiq的Redis配置
Sidekiq.configure_server do |config|
  config.redis = { url: redis_url }
end

Sidekiq.configure_client do |config|
  config.redis = { url: redis_url }
end

不要单独提取ENV['REDIS_PORT']这类参数,Heroku环境中该变量是字符串类型,直接传递会触发类型转换错误。

2. 调整ActionCable配置(cable.rb)

确保ActionCable的Redis适配器直接使用URL配置,不要手动指定端口:

# config/cable.yml
production:
  adapter: redis
  url: <%= ENV['REDIS_URL'] %>
  channel_prefix: your_app_production

# 或在config/application.rb中统一配置
config.action_cable.redis_adapter = { url: ENV['REDIS_URL'] }

3. 验证并重置Heroku Redis插件

先确认生产环境REDIS_URL格式是否正确:

heroku config:get REDIS_URL --app your-production-app-name

正常格式应为redis://h:[password]@[host]:[port]/[db],若格式异常,重新安装插件:

heroku addons:destroy heroku-redis:hobby-dev
heroku addons:create heroku-redis:hobby-dev

4. 确认Gem版本兼容性

确保相关Gem版本匹配:

  • actioncable:与Rails主版本保持一致
  • sidekiq:使用6.x及以上版本(兼容redis 4.x)
  • 避免同时引入redis和redis-namespace等可能冲突的Gem,除非配置完全适配

测试修复

部署修改并重启应用:

git add .
git commit -m "Fix Redis port type conversion error"
git push heroku main
heroku restart

通过日志验证修复效果:

heroku logs --tail --app your-production-app-name

内容的提问来源于stack exchange,提问作者user25003741

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:11:01