[FIX] 修復 MQTT Gateway 啟動崩潰與認證失敗問題

1. 在 mqtt-gateway/main.go 中加入連線重試邏輯 (Redis & MQTT),避免在服務初始化或認證暫時失敗時直接退出。
2. 更新 App/Console/Commands/MqttSyncAuth.php,加入同步 star-cloud-gateway 自身認證資料至 Redis 的邏輯。
3. 修改 compose.yaml,為 mqtt-gateway 加入 restart: always,並為 emqx 加入健康檢查以優化容器啟動流程。
This commit is contained in:
sky121113 2026-04-17 09:08:18 +08:00
parent 550ebc7fb6
commit 588f5659ca
3 changed files with 45 additions and 13 deletions

View File

@ -27,6 +27,14 @@ class MqttSyncAuth extends Command
*/
public function handle(MachineService $machineService)
{
// 1. 同步 Gateway 自身的認證資料 (與 mqtt-gateway/main.go 對應)
$this->info("Syncing star-cloud-gateway auth...");
$gatewayKey = "machine_auth:star-cloud-gateway";
$gatewayPass = hash('sha256', "StarCloudSecret999");
\Illuminate\Support\Facades\Redis::hSet($gatewayKey, 'password', $gatewayPass);
$this->info("Gateway auth synced.");
// 2. 同步所有機台的認證資料
$machines = Machine::withoutGlobalScopes()->get();
$this->info("Syncing " . $machines->count() . " machines to Redis...");

View File

@ -105,6 +105,11 @@ services:
- 'sail-emqx:/opt/emqx/data'
networks:
- sail
healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s
timeout: 5s
retries: 5
depends_on:
- redis
mqtt-gateway:
@ -123,6 +128,7 @@ services:
TZ: 'Asia/Taipei'
networks:
- sail
restart: always
depends_on:
- emqx
- redis

View File

@ -10,6 +10,8 @@ import (
"star-cloud-gateway/internal/handler"
"syscall"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/redis/go-redis/v9"
)
@ -17,29 +19,40 @@ import (
func main() {
cfg := config.LoadConfig()
// 1. 初始化 Redis
rdb := redis.NewClient(&redis.Options{
Addr: cfg.RedisAddr,
Password: cfg.RedisPassword,
DB: 0,
})
if err := rdb.Ping(context.Background()).Err(); err != nil {
log.Fatalf("Failed to connect to Redis at %s: %v", cfg.RedisAddr, err)
var rdb *redis.Client
var client mqtt.Client
// 1. 初始化 Redis (帶重試機制)
for {
rdb = redis.NewClient(&redis.Options{
Addr: cfg.RedisAddr,
Password: cfg.RedisPassword,
DB: 0,
})
if err := rdb.Ping(context.Background()).Err(); err != nil {
log.Printf("Waiting for Redis at %s... (%v)", cfg.RedisAddr, err)
time.Sleep(5 * time.Second)
continue
}
log.Printf("Connected to Redis at %s", cfg.RedisAddr)
break
}
log.Printf("Connected to Redis at %s", cfg.RedisAddr)
// 加入 Laravel 前綴,確保雙方桶子同一個
prefixedIncomingKey := "starcloud_database_" + cfg.IncomingQueueKey
pusher := bridge.NewRedisPusher(rdb, prefixedIncomingKey)
mqttHandler := handler.NewMqttHandler(pusher)
// 2. 初始化 MQTT
// 2. 初始化 MQTT (帶重試機制)
opts := mqtt.NewClientOptions()
opts.AddBroker(cfg.MQTTAddr)
opts.SetClientID(cfg.MQTTClientID)
opts.SetUsername("star-cloud-gateway")
opts.SetPassword("StarCloudSecret999") // 與 Redis 中的 hash 對應
opts.SetCleanSession(false)
opts.SetAutoReconnect(true)
opts.SetMaxReconnectInterval(10 * time.Second)
opts.SetOnConnectHandler(func(c mqtt.Client) {
log.Printf("Connected to MQTT Broker as Gateway [star-cloud-gateway] at %s", cfg.MQTTAddr)
// 訂閱所有機台的上行 Topic
@ -52,9 +65,14 @@ func main() {
}
})
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("Failed to connect to MQTT: %v", token.Error())
client = mqtt.NewClient(opts)
for {
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Printf("Waiting for MQTT Broker at %s... (%v)", cfg.MQTTAddr, token.Error())
time.Sleep(5 * time.Second)
continue
}
break
}
// 3. 啟動 Redis Consumer (下行指令)