????Centrifugo 支持 JSON、TOML和YAML三種格式的配置文件判沟。本文以 JSON 格式為例說明 Centrifugo 配置文件的內(nèi)容蛹头。最簡(jiǎn)單的配置文件如下:
{
"secret": "very-long-secret-key"
}
只有 secret
是必須的,在生產(chǎn)環(huán)境你應(yīng)該將該參數(shù)設(shè)置的足夠復(fù)雜并且避免外泄君纫。<font color='red'>唯二的需要知道這個(gè)參數(shù)值的只有 Centrifugo 自身和你的應(yīng)用程序后端谷扣。</font>該值被用來生成客戶端連接令牌土全、調(diào)用 API 以及訂閱私有頻道。
????connection_lifetime
參數(shù)用來設(shè)置客戶端連接的過期時(shí)間会涎,單位為秒裹匙。如果該參數(shù)設(shè)置為0,表示該客戶端連接永不過期在塔。
{
"secret": "very-long-secret-key",
"connect_lifetime": 0
}
????我們重點(diǎn)關(guān)注以下與頻道(Channel)相關(guān)的幾個(gè)參數(shù)。頻道以字符串進(jìn)行標(biāo)識(shí)拨黔,客戶端通過訂閱頻道來獲取發(fā)布到頻道中的實(shí)時(shí)消息蛔溃。配置文件中的一些參數(shù)會(huì)影響到頻道的行為:
watch:Centrifugo 會(huì)同時(shí)發(fā)生消息到 admin channel,這些消息可以在 admin 管理界面的
messages tab
下查看篱蝇。這個(gè)參數(shù)要慎用贺待,特別是針對(duì)消息發(fā)布頻率比較高的頻道,以免超出 admin client 的處理能力零截。本參數(shù)默認(rèn)值為fasle
麸塞。publish:允許客戶端直接發(fā)布消息到頻道,而不經(jīng)過應(yīng)用程序后端涧衙。一般情況下哪工,消息都是由應(yīng)用程序后端通過 Centrifugo API 發(fā)布到 Centrifugo 服務(wù)器的。這個(gè)參數(shù)適用于沒有后端或者 demo 的快速構(gòu)建弧哎。需要注意的一點(diǎn)是客戶端只有成功訂閱了頻道之后才能發(fā)布消息到該頻道雁比。本參數(shù)默認(rèn)值為
false
。anonymous:該參數(shù)允許匿名訪問(在連接參數(shù)中使用空字符串作為 user ID撤嫩,JWT token 中 sub 為空)偎捎。通常情況下,應(yīng)用程序的用戶都需要登錄認(rèn)證序攘,因此客戶端建立連接時(shí)可以使用用戶名作為標(biāo)識(shí)茴她。如果你需要提供公共的實(shí)時(shí)消息服務(wù),你可以在相應(yīng)的頻道上開啟本參數(shù)程奠。本參數(shù)默認(rèn)值為
false
丈牢。presence:是否可查看當(dāng)前頻道的連接信息,即客戶端對(duì)本頻道的訂閱信息瞄沙。本參數(shù)默認(rèn)是為
false
赡麦。join_leave:當(dāng)有用戶訂閱/取消訂閱頻道時(shí)朴皆,是否發(fā)送相關(guān)信息到頻道中。本參數(shù)默認(rèn)值為
false
泛粹。history_size:頻道歷史消息數(shù)量遂铡。因?yàn)?Centrifugo 將所有的消息存儲(chǔ)在內(nèi)存中,因此限制每個(gè)頻道保留的歷史消息數(shù)量是非常重要的晶姊。本參數(shù)默認(rèn)值為
0
扒接,這意味著頻道不會(huì)保留歷史消息。啟用本參數(shù)后们衙,Centrifugo 會(huì)為命名空間中的每條頻道保留指定數(shù)量的歷史消息钾怔,直到消息過期,關(guān)于消息過期詳見參數(shù)history_lifetime
蒙挑。history_lifetime:頻道歷史消息的過期時(shí)間宗侦,單位為秒。本參數(shù)默認(rèn)值為
0
忆蚀,這意味著頻道不會(huì)保留歷史消息矾利。如果要保留歷史消息,需要同時(shí)配置history_size
與history_lifetime
馋袜,并設(shè)置合理的參數(shù)值男旗。recover:是否恢復(fù)丟失的消息。如果開啟本參數(shù)欣鳖,當(dāng)客戶端重新連接時(shí)(如因網(wǎng)絡(luò)原因斷線重連)察皇,Centrifugo 將試圖重新恢復(fù)丟失的消息。這個(gè)參數(shù)的啟用要配合歷史消息相關(guān)參數(shù)(history_size 和 histroy_lifetime)泽台,畢竟消息的恢復(fù)依靠的是頻道歷史消息記錄什荣。注意,所有實(shí)時(shí)事件都需要啟用此功能怀酷。本參數(shù)默認(rèn)值為
false
溃睹。history_drop_inactive:是否丟棄不活躍的歷史消息。本參數(shù)可以大幅降低資源消耗(內(nèi)存胰坟、消息傳播次數(shù))因篇。簡(jiǎn)單來說,該參數(shù)開啟后笔横,Centrifugo 會(huì)主動(dòng)丟棄不需要的歷史消息竞滓。本參數(shù)默認(rèn)值為
false
。namespaces:namespaces 是可選參數(shù)吹缔,如果設(shè)置的話商佑,需要設(shè)置成 namespace 數(shù)組的形式。通過 namespaces 可以針對(duì) namespace 下的頻道配置自定義參數(shù)厢塘。每個(gè) namespace 都有自己的 name茶没,并且擁有上面所有的針對(duì)頻道的參數(shù)肌幽。name 必須唯一,并且由字母抓半、數(shù)字喂急、下劃線或連接符組成,長度必須大于2(
^[-azA-Z0-9_]{2,}$
)笛求。
配置文件示例:
{
"admin": true,
"port": 8000,
"secret":"very-long-secret-key",
"admin_password":"very-long-admin-password",
"admin_secret":"very-long-admin-secret",
"api_key":"very-long-api-key",
"connection_lifetime": 0,
"debug": true,
"presence": true,
"history_size": 50,
"history_lifetime": 600,
"publish": true,
"anonymous": true,
"subscribe_to_publish": true,
"join_leave": true,
"history_recover": true,
"log_file": "D:/tmp/centrifugo.log",
"log_level": "debug",
"engine": "redis",
"redis_host": "127.0.0.1",
"redis_port": "6379",
"redis_db": "12",
"redis_password": "123456a?",
"namespaces":[
{
"name":"public",
"anonymous":true,
"publish":true,
"presence":true,
"join_leave":true,
"history_size":10,
"history_lifetime":30,
"history_recover":true
},
{
"name": "gossips",
"watch": true
}
]
}
在以上配置下:
??- 頻道news
將使用全局參數(shù)配置廊移。
??- 頻道public:news
將使用 public
命名空間的參數(shù)配置。
??- 頻道gossips:news
將使用 gossips
命名空間的參數(shù)配置探入。
通過 ./centrifugo -h
命令狡孔,可以查看所有的可用參數(shù),如下:
Centrifugo – real-time messaging server
Usage:
[flags]
[command]
Available Commands:
checkconfig Check configuration file
genconfig Generate simple configuration file to start with
help Help about any command
version Centrifugo version information
Flags:
-a, --address string interface address to listen on 服務(wù)的地址
--admin enable admin web interface 是否開啟admin的管理界面
--admin_insecure use insecure admin mode – no auth required for admin socket admin安全驗(yàn)證, 節(jié)點(diǎn): /, 可直接訪問admin管理界面, 默認(rèn)為false, 登錄admin需要admin_password, 反之可直接登錄
--api_insecure use insecure API mode 后臺(tái)推送安全驗(yàn)證, 節(jié)點(diǎn): /api. 默認(rèn)為 false, 此時(shí)訪問節(jié)點(diǎn)需要api_key. 當(dāng)設(shè)置為 true 后, 任何人都將可以訪問此節(jié)點(diǎn)
--client_insecure start in insecure client mode 客戶端是否需要安全驗(yàn)證, 默認(rèn)為false, 客戶端必須擁有 JWT token 才能訪問
-c, --config string path to config file (default "config.json")
--debug enable debug endpoints 是否開啟dubug節(jié)點(diǎn), 開啟后可訪問 /debug/pprof/ 查看一些Centrifugo的網(wǎng)絡(luò)狀態(tài)
-e, --engine string engine to use: memory or redis (default "memory") 消息存儲(chǔ)引擎, 默認(rèn)為內(nèi)存, 部署多個(gè)實(shí)例會(huì)造成數(shù)據(jù)不同步, 因此推薦使用 redis
--grpc_api enable GRPC API server 是否開啟grpc api, 默認(rèn)使用 http_api
-h, --help help for this command
--internal_port string custom port for internal endpoints 開啟自定義默認(rèn)端口, 默認(rèn)為8000, 開啟額外的端口為admin訪問
--log_file string optional log file - if not specified logs go to STDOUT log輸出文件
--log_level string set the log level: debug, info, error, fatal or none (default "info") log級(jí)別
-n, --name string unique node name 命名空間名稱, 此空間下的屬性會(huì)覆蓋common部分, 但不會(huì)繼承
--pid_file string optional path to create PID file
-p, --port string port to bind HTTP server to (default "8000") 服務(wù)端口號(hào)
--prometheus enable Prometheus metrics endpoint
--redis_db int Redis database (Redis engine)
--redis_host string Redis host (Redis engine) (default "127.0.0.1")
--redis_master_name string name of Redis master Sentinel monitors (Redis engine)
--redis_password string Redis auth password (Redis engine)
--redis_port string Redis port (Redis engine) (default "6379")
--redis_sentinels string comma-separated list of Sentinel addresses (Redis engine)
--redis_tls enable Redis TLS connection
--redis_tls_skip_verify disable Redis TLS host verification
--redis_url string Redis connection URL in format redis://:password@hostname:port/db (Redis engine)
--tls enable TLS, requires an X509 certificate and a key file
--tls_cert string path to an X509 certificate file
--tls_key string path to an X509 certificate key