@@ -7,122 +7,139 @@ sidebar_position: 6
77
88## 集群部署说明
99
10- ` open-im-server ` 支持集群部署。以下是集群部署的步骤 :
10+ ` open-im-server ` 支持集群部署。以下是 ** 源码集群 ** 部署的步骤 :
1111
12- 1 . 克隆仓库,源码部署:
13- ``` bash
14- git clone https://github.com/openimsdk/open-im-server && cd open-im-server
15- ```
16-
17- docker部署:
18- ``` bash
19- git clone https://github.com/openimsdk/openim-docker
20- ```
12+ 场景:两台机器部署` open-im-server ` ,分别为机器A和机器B,假设两台机器在同一内网中。
2113
22- 2 . 修改` kafka ` 、` minio ` 、` mongodb ` 、` etcd ` (默认服务发现方式)、` redis ` 中的地址,将其配置到正确的组件地址中。** 保证连接组件的各个端口可访问(关注防火墙规则是否允许端口被访问)。**
23- 源码部署修改方式:修改` open-im-server/config ` 目录下,对应组件的地址。
24- 对应配置文件字段为:
14+ 机器A:部署` open-im-server ` 、` nginx ` 、` mongo ` 、` redis ` 、` etcd ` 、` kafka ` 、` minio ` 、` prometheus ` 、` grafana ` 、` aleartmanager ` 。
2515
26- - ` kafka ` : ` kafka.yml:address `
27- - ` minio ` : ` minio.yml ` ,` internalAddress ` 配置内部服务访问地址,` externalAddress ` 配置外部访问` minio ` 的地址。
28- - ` mongo ` :` mongodb.yml:address `
29- - ` etcd ` : ` discovery.yml:etcd.address `
30- - ` redis ` : ` redis.yml:address `
16+ 机器B:部署` open-im-server ` 。
3117
32- 3 . 修改` kafka ` 广播的端口。如果使用` open-im-server ` 中的` docker-compose.yml ` 部署,修改` service.kafka.environment.KAFKA_CFG_ADVERTISED_LISTENERS ` 中的` EXTERNAL ` 为访问` kafka ` 组件的地址。其他部署方式请自行修改。
33- 例如:` KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,EXTERNAL://192.168.2.36:19094 ` 。
18+ 组件` mongo ` 、` redis ` 、` kafka ` 、` etcd ` 均支持集群部署,以下场景默认每个组件集群部署3台。
3419
35- 4 . 如果使用内网部署集群,可将需要暴露端口的组件中的` autoSetPorts ` 配置设为` true ` (默认即为` true ` ),可实现自动配置端口。
36- 如果集群机器** 不在内网中** ,需要将` autoSetPorts ` 设置为` false ` ,并修改各个` rpc ` 组件的` registerIP ` 为设置部署` etcd ` 的服务器可访问的` ip ` 地址,** 并保证各个端口可被访问** 。如需启用` prometheus ` ,还需要保证各个组件的` prometheus.port ` 端口可被访问。
37- 拥有` autoSetPorts ` 配置的组件如下:
20+ 1 . 机器A、B:克隆仓库:
3821
39- - ` openim-api.yml:prometheus.autoSetPorts `
40- - ` openim-msggateway.yml:rpc.autoSetPorts `
41- - ` openim-msgtransfer.yml:prometheus.autoSetPorts `
42- - ` openim-push.yml:rpc.autoSetPorts `
43- - ` openim-rpc-auth.yml:rpc.autoSetPorts `
44- - ` openim-rpc-conversation.yml:rpc.autoSetPorts `
45- - ` openim-rpc-friend.yml:rpc.autoSetPorts `
46- - ` openim-rpc-group.yml:rpc.autoSetPorts `
47- - ` openim-rpc-msg.yml:rpc.autoSetPorts `
48- - ` openim-rpc-third.yml:rpc.autoSetPorts `
49- - ` openim-rpc-user.yml:rpc.autoSetPorts `
22+ ``` bash
23+ git clone https://github.com/openimsdk/open-im-server && cd open-im-server
24+ ```
5025
51- ![ rpc0] ( ./cluster.assets/rpc0.png )
26+ 2 . 机器A:修改` kafka ` 、` minio ` 、` mongodb ` 、` etcd ` (默认服务发现方式)、` redis ` 中的地址,将其配置到正确的组件地址中。** 保证连接组件的各个端口可访问(关注防火墙规则是否允许端口被访问)。**
27+ 修改` open-im-server/config ` 目录下,对应组件的地址。
28+ 对应配置文件字段为:
5229
53- 非内网部署时,如需启用` prometheus ` ,部署` promtheus ` 的机器需要修改` prometheus.yml ` ,将其中的所有` http_sd_configs ` 配置项及其子配置项去掉,加上` static_configs ` 配置项,并将其中的` targets ` 改为对应的服务的端口。
54- 例如:` openimserver-openim-api ` 表示` api ` 组件的` prometheus ` 数据抓取,则其` target ` 中的端口地址应和` openim-api.yml ` 中的` prometheus.ports ` 一致。
30+ - ` kafka ` : ` kafka.yml:address ` ,内容为` [ kafkaAddr1, kafkaAddr2, kafkaAddr3 ] `
31+ - ` minio ` : ` minio.yml ` ,` internalAddress ` 配置内部服务访问地址,` externalAddress ` 配置外部访问` minio ` 的地址。
32+ - ` mongo ` :` mongodb.yml:address ` ,内容为` [ mongoAddr1, mongoAddr2, mongoAddr3 ] `
33+ - ` etcd ` : ` discovery.yml:etcd.address ` ,内容为` [ etcdAddr1, etcdAddr2, etcdAddr3 ] `
34+ - ` redis ` : ` redis.yml:address ` ,内容为` [ redis1, redis2, redis3 ] ` ,并将` redis.yml ` 中的` clusterMode ` 设置为` true ` (单机部署不需要)。
5535
56- ![ prometheus0 ] ( ./cluster.assets/prometheus0.png )
36+ 3 . 在 ` open-im-server/start-config.yml ` 中根据需求修改各个组件数量。
5737
58- 5 . 在 ` open-im-server/start-config.yml ` 中根据需求修改各个组件数量。
38+ 4 . 机器A部署 ` nginx ` ,参考配置如下:
5939
60- 6 . 如果通过集群模式部署 ` redis ` ,则需将 ` redis.yml ` 中的 ` clusterMode ` 设置为 ` true ` 。
40+ > 🚀 ** 提示 ** : 确保替换成您的实际域名、SSL 证书路径和 SSL 密钥 。
6141
62- 7 . 如果部署了多个` api ` 或者` WebSocket ` 监听端口,推荐使用` nginx ` 方便快捷地实现负载均衡,参考配置如下:
6342 ``` yaml
64- # nginx.conf
65-
6643 events {
6744 worker_connections 1024;
6845 }
6946
7047 http {
71- upstream websocket_cluster {
72- server 192.168.2.10:10101; # WebSocket address
73- server 192.168.2.11:10001;
48+ # open-im-server chat Corresponding deployment address and port
49+ upstream msg_gateway{
50+ # IM Message server address Multiple can be specified according to the deployment
51+ server 127.0.0.1:10001;
52+ server 192.168.2.36:10001;
7453 }
75-
76- upstream api_cluster {
77- server 192.168.2.10:10102; # api address
78- server 192.168.2.11:10002;
54+ upstream im_api{
55+ # IM Group user api server address Multiple can be specified according to the deployment
56+ server 127.0.0.1:10002;
57+ server 192.168.2.36:10001;
58+ }
59+ upstream minio_s3_2{
60+ # Minio address can be assigned to multiple modules dependingon deployment
61+ server 127.0.0.1:10005;
7962 }
80-
81- # WebSocket
8263 server {
83- listen 10001;
64+ listen 443; # Listening on port 443
65+ server_name web.xx.xx; # Your domain name
66+ ssl on;
67+ # Path of pem file for ssl certificate
68+ ssl_certificate /usr/local/nginx/conf/ssh/web.xx.xx_bundle.pem;
69+ # Key file path of ssl certificate
70+ ssl_certificate_key /usr/local/nginx/conf/ssh/web.xx.xx.key;
8471
85- server_name _;
72+ gzip on;
73+ gzip_min_length 1k;
74+ gzip_buffers 4 16k;
75+ gzip_comp_level 2;
76+ gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/wasm;
77+ gzip_vary off;
78+ gzip_disable "MSIE [1-6]\.";
8679
87- location / {
88- proxy_pass http://websocket_cluster;
80+ location ^~/api/{
8981 proxy_http_version 1.1;
9082 proxy_set_header Upgrade $http_upgrade;
91- proxy_set_header Connection 'upgrade';
92- proxy_set_header Host $host;
93- proxy_cache_bypass $http_upgrade;
83+ proxy_set_header Connection "Upgrade";
84+ proxy_set_header X-real-ip $remote_addr;
85+ proxy_set_header X-Forwarded-For $remote_addr;
86+ proxy_set_header X-Request-Api $scheme://$host/api;
87+ proxy_pass http://im_api/;
9488 }
95- }
96-
97- # API
98- server {
99- listen 10002;
10089
101- server_name _;
90+ location /msg_gateway{
91+ proxy_http_version 1.1;
92+ proxy_set_header Upgrade $http_upgrade;
93+ proxy_set_header Connection "Upgrade";
94+ proxy_set_header X-real-ip $remote_addr;
95+ proxy_set_header X-Forwarded-For $remote_addr;
96+ proxy_pass http://msg_gateway/;
97+ }
10298
103- location / {
104- proxy_pass http://api_cluster;
105- proxy_set_header Host $host;
99+ location ^~/im-minio-api/ {
100+ proxy_set_header Host $http_host;
106101 proxy_set_header X-Real-IP $remote_addr;
107102 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
108103 proxy_set_header X-Forwarded-Proto $scheme;
104+ proxy_connect_timeout 300;
105+
106+ proxy_http_version 1.1;
107+ proxy_set_header Connection "";
108+ chunked_transfer_encoding off;
109+ proxy_pass http://minio_s3_2/;
109110 }
110111 }
111112 }
112-
113113 ```
114114
115- 如果决定使用 ` docker ` 启动 ` nginx ` ,可参考以下命令(linux系统):
115+ 5 . ` mage ` 编译, ` mage start ` 启动服务。
116116
117- ``` shell
118- docker run --name nginx -p 10001:10001 -p 10002:10002 -v $( pwd) /nginx-config:/etc/nginx/conf.d -v $( pwd) /nginx.conf:/etc/nginx/nginx.conf -d nginx
119- ```
117+ 6 . 初始化` sdk ` 时,配置` api ` 连接地址` http://web.xx.xx/api ` ,` ws ` 连接地址` http://web.xx.xx/msg_gateway ` ,` minio ` 地址为` http://web.xx.xx/im-minio-api ` 。
120118
121- 如使用不同系统修改挂载的目录即可。
119+ ## ** 常见问题/注意事项 **
122120
123- 8 . 使用` docker compose up -d ` 启动组件。如果采用源码部署,使用` mage ` 编译,` mage start ` 启动服务。
121+ 1 . 部署` kafka ` 时,需要修改` kafka ` 广播的端口。如果使用` open-im-server ` 中的` docker-compose.yml ` 部署,修改` service.kafka.environment.KAFKA_CFG_ADVERTISED_LISTENERS ` 中的` EXTERNAL ` 为访问` kafka ` 组件的地址。其他部署方式请自行修改。
122+ 例如:` KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,EXTERNAL://192.168.2.36:19094 ` 。
123+ 2 . 多台机器部署需要保证时钟一致,服务才可正常运行。例如` token ` 的签发允许各个机器的时钟误差在` 5s ` 以内。
124+ 3 . 组件端口无法访问:通过回环地址检测组件启动是否正常,若回环地址可访问,则检查是否被防火墙规则过滤。
125+ 4 . 如果集群机器** 不在内网中** ,需要将` autoSetPorts ` 设置为` false ` ,并修改各个` rpc ` 组件的` registerIP ` 为设置部署` etcd ` 的服务器可访问的` ip ` 地址,** 并保证各个端口可被访问** 。如需启用` prometheus ` ,还需要保证各个组件的` prometheus.port ` 端口可被访问。
126+ 拥有` autoSetPorts ` 配置的组件如下:
124127
125- ## ** 常见问题/注意事项**
128+ - ` openim-api.yml:prometheus.autoSetPorts `
129+ - ` openim-msggateway.yml:rpc.autoSetPorts `
130+ - ` openim-msgtransfer.yml:prometheus.autoSetPorts `
131+ - ` openim-push.yml:rpc.autoSetPorts `
132+ - ` openim-rpc-auth.yml:rpc.autoSetPorts `
133+ - ` openim-rpc-conversation.yml:rpc.autoSetPorts `
134+ - ` openim-rpc-friend.yml:rpc.autoSetPorts `
135+ - ` openim-rpc-group.yml:rpc.autoSetPorts `
136+ - ` openim-rpc-msg.yml:rpc.autoSetPorts `
137+ - ` openim-rpc-third.yml:rpc.autoSetPorts `
138+ - ` openim-rpc-user.yml:rpc.autoSetPorts `
139+
140+ ![ rpc0] ( ./cluster.assets/rpc0.png )
126141
127- 1 . 多台机器部署需要保证时钟一致,服务才可正常运行。例如` token ` 的签发允许各个机器的时钟误差在` 5s ` 以内。
128- 2 . 组件端口无法访问:通过回环地址检测组件启动是否正常,若回环地址可访问,则检查是否被防火墙规则过滤。
142+ 此外,机器A还需要修改` prometheus.yml ` ,将其中的所有` http_sd_configs ` 配置项及其子配置项去掉,加上` static_configs ` 配置项,并将其中的` targets ` 改为对应的服务的端口。
143+ 例如:` openimserver-openim-api ` 表示` api ` 组件的` prometheus ` 数据抓取,则其` target ` 中的端口地址应和` openim-api.yml ` 中的` prometheus.ports ` 一致。
144+
145+ ![ prometheus0] ( ./cluster.assets/prometheus0.png )
0 commit comments