一、LumenIM介绍

1.LumenIM简介

Lumen IM 是一个网页版在线聊天项目,前端使用 Naive UI + Vue3,后端采用 GO 开发。

2.LumenIM功能

基于 WebSocket 服务做消息即时推送
支持私聊及群聊
支持多种聊天消息类型 例如:文本、代码块、图片及其它类型文件,并支持文件下载
支持聊天消息撤回、删除(批量删除)、转发消息(逐条转发、合并转发)、群禁言
支持编写个人笔记、支持笔记分享(好友或群)。

3.LumenIM项目地址


后端:https://gitee.com/gzydong/go-chat

二、本次实践介绍

1. 本次实践简介

a.本次实践部署环境为个人腾讯云的测试环境,若是生产环境请谨慎部署;

b.本次实践使用了mysql、redis、nginx以及前后端的开源包进行启动测试。

2. 本地环境规划

本次实践环境规划:通过docker启动nginx、mysql、redis,以及本地启动前后端

三、检查本地Docker环境

1. 检查本地Docker版本

检查Docker版本:docker version    

root@WellDone:/home/goodjob# docker version    
Client: Docker Engine - Community    
 Version:           25.0.0    
 API version:       1.44    
 Go version:        go1.21.6    
 Git commit:        e758fe5    
 Built:             Thu Jan 18 17:09:49 2024    
 OS/Arch:           linux/amd64    
 Context:           default    

Server: Docker Engine - Community    
 Engine:    
  Version:          25.0.0    
  API version:      1.44 (minimum version 1.24)    
  Go version:       go1.21.6    
  Git commit:       615dfdf    
  Built:            Thu Jan 18 17:09:49 2024    
  OS/Arch:          linux/amd64    
  Experimental:     false    
 containerd:    
  Version:          1.6.27    
  GitCommit:        a1496014c916f9e62104b33d1bb5bd03b0858e59    
 runc:    
  Version:          1.1.11    
  GitCommit:        v1.1.11-0-g4bccb38    
 docker-init:    
  Version:          0.19.0    
  GitCommit:        de40ad0    
root@WellDone:/home/goodjob#

2. 检查Docker服务状态

检查Docker服务状态,确保Docker服务正常运行。

systemctl status docker    

root@WellDone:/home/goodjob/Downloads# systemctl status docker    
● docker.service - Docker Application Container Engine    
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)    
     Active: active (running) since Wed 2024-01-24 08:54:31 CST; 1 day 4h ago    
TriggeredBy: ● docker.socket    
       Docs: https://docs.docker.com    
   Main PID: 1549 (dockerd)    
      Tasks: 65    
     Memory: 419.5M    
        CPU: 38.874s    
     CGroup: /system.slice/docker.service    
             ├─  1549 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock    
             ├─  2010 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 6379 -container-ip 172.17.0.2 -container-port 6379    
             ├─  2016 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 6379 -container-ip 172.17.0.2 -container-port 6379    
             ├─  2029 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 5432 -container-ip 172.17.0.3 -container-port 5432    
             ├─  2036 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 5432 -container-ip 172.17.0.3 -container-port 5432    
             ├─123083 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8888 -container-ip 172.17.0.4 -container-port 8080    
             └─123095 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8888 -container-ip 172.17.0.4 -container-port 8080    

1月 24 08:54:31 WellDone dockerd[1549]: time="2024-01-24T08:54:31.067895045+08:00" level=info msg="API listen on /run/docker.sock"    
1月 24 08:54:31 WellDone systemd[1]: Started Docker Application Container Engine.    
1月 24 16:13:48 WellDone dockerd[1549]: time="2024-01-24T16:13:48.185008219+08:00" level=info msg="ignoring event" container=8ad99af82d8a41dd661d>    
1月 24 16:13:48 WellDone dockerd[1549]: time="2024-01-24T16:13:48.189484205+08:00" level=warning msg="ShouldRestart failed, container will not be>    
1月 24 21:58:08 WellDone dockerd[1549]: time="2024-01-24T21:58:08.024915139+08:00" level=info msg="ignoring event" container=27fd28e2993f31b170b8>    
1月 24 21:58:08 WellDone dockerd[1549]: time="2024-01-24T21:58:08.029986730+08:00" level=warning msg="ShouldRestart failed, container will not be>    
1月 25 10:46:16 WellDone dockerd[1549]: time="2024-01-25T10:46:16.329180722+08:00" level=info msg="Container failed to exit within 10s of signal >    
1月 25 10:46:16 WellDone dockerd[1549]: time="2024-01-25T10:46:16.350757439+08:00" level=info msg="ignoring event" container=b1223add00f36bbb3246>    
1月 25 10:49:21 WellDone dockerd[1549]: time="2024-01-25T10:49:21.993755983+08:00" level=info msg="Container failed to exit within 10s of signal >    
1月 25 10:49:22 WellDone dockerd[1549]: time="2024-01-25T10:49:22.015822650+08:00" level=info msg="ignoring event" container=edb0e153b45dbe62acd8>    
lines 1-28/28 (END)

四、部署过程

```  
1.下载前后端程序

# 前端部分:
#到opt目录下创建该程序的存放路径
cd /opt
mkdir LumenIm  
cd LumenIm
git clone https://gitee.com/gzydong/LumenIM.git
git clone https://github.com/gzydong/go-chat.git

2.下载下来后,要部署编译,先前端:
yarn install

#需要先安装yarn
yum install npm
npm install -g yarn

3.安装依赖包后,构建生产环境配置
yarn build

4.ll -a查看LumenIm全部的配置文件,找到.env.electron,接口的地址写上,具体的ip根据你自己的服务器的公网地址去写。
ENV = 'production'

VITE_BASE=./
VITE_ROUTER_MODE=hash
VITE_BASE_API=http://43.138.153.157:9503
VITE_SOCKET_API=ws://43.138.153.157:9504

5.然后启动本地开发环境桌面客户端
yarn electron:dev


6.mysql、redis、nginx部署,我这边是直接docker compose启动的

version: '3'
services:
  redis:
        image: 'redis:latest'
        volumes:
            - '/opt/redis/data:/data'
            - '/opt/redis/conf/redis.conf:/etc/redis/redis.conf'
        privileged: true
        container_name: redis
        ports:
            - '6379:6379'
        restart: always

  mysql_dba:
        image: 'mysql:latest'
        environment:
            - MYSQL_ROOT_PASSWORD=111111
        volumes:
            - '/etc/localtime:/etc/localtime:ro'
            - '/opt/mysql/conf:/etc/mysql/conf.d'
            - '/opt/mysql/data:/var/lib/mysql'
            - '/opt/mysql/log:/var/log/mysql'
        privileged: true
        restart: always
        container_name: mysql_dba
        ports:
            - '3306:3306'

  nginx:
        image: 'nginx:latest'
        restart: always
        volumes:
            - '/etc/localtime:/etc/localtime:ro'
            - '/opt/LumenIm/LumenIM/dist:/opt/LumenIm/LumenIM/dist'
            - '/opt/nginx/nginx.conf:/etc/nginx/nginx.conf'
        ports:
            - '80:80'
        container_name: nginx


以上具体每个容器的路径自己设定

####nginx配置文件如下:
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    server {
        listen       80;
        server_name  localhost;

        root /opt/LumenIm/LumenIM/dist;
        index  index.html;

        location / {
          try_files $uri $uri/ /index.html;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
            expires 7d;
        }

        location ~ .*\.(js|css)?$ {
            expires 7d;
        }
    }

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;
}



7.后端部署:
拷贝项目根目录下 config.example.yaml 文件为 config.yaml 并正确配置相关参数
cp config.example.yaml config.yaml


我这边的配置如下:
# 项目配置信息
app:
  env: dev
  debug: false
  juhe_key: "xxxxxxx"
  admin_email:
    -
  public_key: |
    -----BEGIN PUBLIC KEY-----
    xxxxxxx
    -----END PUBLIC KEY-----
  private_key: |
    -----BEGIN RSA PRIVATE KEY-----
    xxxxx
    -----END RSA PRIVATE KEY-----

server:
  http: 9503
  websocket: 9504
  tcp: 9505

# 日志配置
log:
  # 日志文件路径 *请使用绝对路径*
  path: "/path/to/.../runtime"

# Redis 相关配置
redis:
  host: 43.138.153.157:6379
  auth: xxxxx
  database: 0

# Mysql 数据库配置
mysql:
  host: 43.138.153.157
  port: 3306
  charset: utf8mb4
  username: root
  password: 111111
  database: go_chat
  collation: utf8mb4_general_ci

# Jwt 配置
jwt:
  secret: 836c3fea9bba4e04d51bd0fbcc5
  expires_time: 3600
  buffer_time: 3600

openai:
  key: "xxxx"
  proxy: "https://api.openai.com"

# 跨域配置
cors:
  origin: "*"
  headers: "Content-Type,Cache-Control,User-Agent,Keep-Alive,DNT,AccessToken,Authorization"
  methods: "OPTIONS,GET,POST,PUT,DELETE"
  credentials: false
  max_age: 600

# 文件系统配置【oss,qiniu 暂不支持】
filesystem:
  default: local
  local:
    # 文件保存的根目录
    root: "/path/xx/lumenim/"
    # 公开的 bucket
    bucket_public: "public"
    # 私有的 bucket
    bucket_private: "private"
    endpoint: "im-cdn.gzydong.com"
    ssl: false
  minio:
    secret_id: "xxxxxx"
    secret_key: "xxxxx"
    # 公开的 bucket
    bucket_public: "im-static"
    # 私有的 bucket
bucket_private: "im-private"
    endpoint: "im-cdn.gzydong.com:9000"
    ssl: false

# 邮件配置
email:
  host: smtp.163.com
  port: 465
  username: xxxxx
  password: xxxxx
  fromname: "Lumen IM 在线聊天"


以上是配置文件内容

8.这时候要安装依赖包
go mod tidy

#但是要先安装go,以下是步骤
wget https://golang.google.cn/dl/go1.21.0.linux-amd64.tar.gz
tar -xvf go1.21.0.linux-amd64.tar.gz
vim /etc/profile
mkdir /home/gopath
source /etc/profile
go version
出来版本即可,要1.21.0
这时候执行go mod tidy,会有一段时间,后面就可以了

#其中有可能下载会遇到go 下载github包提示: dial tcp 172.217.160.81:443: i/o timeout ,这时候执行下面,然后重新安装依赖就可以
go env -w GOPROXY=https://goproxy.cn

##然后再安装依赖包
go mod tidy

9.安装相关依赖命令行工具
make install

10.安装完成后,要初始化数据库
go run ./cmd/lumenim migrate

#这时候又会遇到问题,有以下的,各自执行就行
问题: 运行electorn . 报错:error while loading shared libraries: libX11-xcb.so.1
yum install libX11-devel --nogpg

问题: error while loading shared libraries: libatk-bridge-2.0.so.0
yum install at-spi2-atk -y

问题:error while loading shared libraries: libgtk-3.so.0
yum install gtk3 -y

问题:error while loading shared libraries: libXss.so.1
yum install libXScrnSaver* -y

问题:error while loading shared libraries: libdrm.so.2
yum install libdrm*

问题:error while loading shared libraries: libgbm.so.1
yum install libgbm*

11.再次初始化数据库
go run ./cmd/lumenim migrate

##这时候可能会爆内存,我服务器比较小,4g,直接爆了,虚拟内存加了8g,然后初始化成功了

下面是加交换分区的方法
创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)。

dd if=/dev/zero of=/root/swapfile bs=1M count=1024
格式化为交换分区文件:

mkswap /root/swapfile #建立swap的文件系统
启用交换分区文件:

swapon /root/swapfile #启用swap文件
使系统开机时自启用,在文件/etc/fstab中添加一行:

/root/swapfile swap swap defaults 0 0


12.这时候完成后,启动后端即可
go run ./cmd/lumenim http      # 本地启动 http 服务
go run ./cmd/lumenim commet    # 本地启动 websocket 服务

五、访问LumenIM首页

访问地址http://43.138.153.157/auth/login?redirect=/message,这是我的测试服务器的地址,大家可以访问玩玩,账号密码:13188886666,111222

【打工日常】云原生之搭建私有化web在线聊天软件LumenIM_nginx

【打工日常】云原生之搭建私有化web在线聊天软件LumenIM_mysql_02

【打工日常】云原生之搭建私有化web在线聊天软件LumenIM_nginx_03

【打工日常】云原生之搭建私有化web在线聊天软件LumenIM_mysql_04

【打工日常】云原生之搭建私有化web在线聊天软件LumenIM_docker_05