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

求可用Centrifugo/Redis Docker Compose配置及消息转发故障排查

问题:Centrifugo v6 + Redis 无法自动接收Redis消息的Docker Compose配置排查

需求概述

  • 目标配置组件:
    • Centrifugo v5或v6
    • Redis(任意版本)
    • Redis Insight(任意版本)
  • 遇到的问题:在线搜索到的Docker Compose配置大多过时,自行调试的配置中,Centrifugo的HTTP API功能正常,但无法自动接收来自Redis的消息。

尝试过的Docker Compose配置

version: "3.7"
services:
  redis:
    image: redis:7.4.2
    container_name: hub-redis
    ports:
      - "6379:6379"
    environment:
      - log_level=trace
    volumes:
      - redis-data:/data
    networks:
      - app-network
    command: redis-server --appendonly yes

  redis-ui:
    image: redislabs/redisinsight:latest
    container_name: hub-redis-ui
    ports:
      - "5540:5540"
    volumes:
      - redis-ui-data:/db
    networks:
      - app-network
    restart: unless-stopped
    depends_on:
      - redis
    #NOTE: connect using "redis://default@redis:6379" in UI

  centrifugo:
    image: centrifugo/centrifugo:latest
    container_name: hub-centrifugo
    ports:
      - "8000:8000"
    volumes:
      - ./centrifugo-config:/centrifugo
    networks:
      - app-network
    depends_on:
      - redis
    command: centrifugo -c /centrifugo/config.json

networks:
  app-network:
    driver: bridge

volumes:
  redis-data:
  redis-ui-data:

Centrifugo测试配置文件(安全性极低,仅用于测试)

{
    "token_hmac_secret_key": "secret-key",
    "admin": {
        "enabled": true,
        "insecure": true
    },
    "api_key": "api-key",
    "log": {
        "level": "trace",
        "logger": {
            "stdout": true,
            "stderr": true
        }        
    },
    "debug": {
        "enabled": true
    },
    "health": {
        "enabled": true
    },
    "swagger": {
        "enabled": true
    },
    "client": {
        "insecure": true,
        "allowed_origins": [
            "*"
        ]
    },
    "engine": {
        "type": "redis",
        "redis": {
            "address": "redis://hub-redis:6379",
            "prefix": "centrifugo",
            "api_channel": "api", 
            "api_channel_prefix": "centrifugo.api",
            "connect_timeout": "10s",
            "read_timeout": "30s",
            "write_timeout": "30s",
            "idle_timeout": "120s",
            "max_idle": 20,
            "dial_keep_alive": "60s",
            "pool_size": 50            
        }
    },
    "channel": {
        "namespaces": [
            {
                "name": "realtime",
                "presence": true,
                "history_size": 10,
                "history_ttl": "300s",
                "force_recovery": true,
                "allow_subscribe_for_anonymous": true,
                "allow_subscribe_for_client": true,
                "allow_publish_for_anonymous": true,
                "allow_publish_for_subscriber": true,
                "allow_presence_for_anonymous": true,
                "allow_presence_for_subscriber": true,
                "allow_history_for_anonymous": true,
                "allow_history_for_subscriber": true
            }
        ]
    }
}

测试操作与现象

  1. 连接状态:Centrifugo的Docker日志显示已成功连接到Redis Pub/Sub
  2. 消息发布方式:
    • C#代码发布:
      var command = new
      {
          method = "publish",
          @params = new
          {
              channel = "realtime:data",
              data = new { value = new Random().Next(1, 100) }
          }
      };
      
      var options = new JsonSerializerOptions
      {
          PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
          WriteIndented = true  // Easier debugging
      };
      
      string json = JsonSerializer.Serialize(command, options);
      
      // Remove any hidden characters that might cause issues
      json = json.Replace("\r", "").Replace("\n", "");
      
      await db.PublishAsync("centrifugo.api", json);
      
    • Redis CLI命令发布:
      docker exec -it hub-redis redis-cli publish centrifugo.api '{"method":"publish","params":{"channel":"realtime:data","data":{"value":42}}}'
      PUBLISH centrifugo.client.realtime:data "{\"channel\":\"realtime:data\",\"data\":{\"value\":42}}"
      
  3. 现象:Redis能正常接收消息,但无法自动转发给Centrifugo

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 16:21:02