Giter Club home page Giter Club logo

zjp-chatserver's Introduction

ZJP-ChatServer


基本描述

C++基于控制台的集群聊天服务器:以配置nginx负载均衡模块分发客户端连接、基于发布-订阅的redis作为服务器集群消息队列,以mysql数据库存储用户数据

环境

  • Ubuntu 18
  • gcc 7.5.0
  • cmake 3.24.0

技术

  • 使用Json序列化和反序列化发送的数据
  • muduo网络库的开发基本使用
  • MySQL数据库编程
  • ngix tcp模块配置编译实现负载均衡
  • 发布-订阅消息的redis实现不同服务器上客户连接的消息发送

文件目录结构

root@iZwz959g1zzxerwo9dtx98Z:~/test/chat# tree .
.
├── bin
├── build
├── build.sh
├── CMakeLists.txt
├── include
│?? ├── public.hpp
│?? └── server
│??     ├── chatserver.hpp
│??     ├── chatservice.hpp
│??     ├── db
│??     │?? └── db.hpp
│??     ├── group.hpp
│??     ├── groupuser.hpp
│??     ├── model
│??     │?? ├── friendmodel.hpp
│??     │?? ├── groupmodel.hpp
│??     │?? ├── offlinemsgmodel.hpp
│??     │?? └── usermodel.hpp
│??     ├── redis
│??     │?? └── redis.hpp
│??     └── user.hpp
├── src
│?? ├── client
│?? │?? ├── CMakeLists.txt
│?? │?? └── main.cpp
│?? ├── CMakeLists.txt
│?? └── server
│??     ├── chatserver.cpp
│??     ├── chatservice.cpp
│??     ├── CMakeLists.txt
│??     ├── db
│??     │?? └── db.cpp
│??     ├── main.cpp
│??     ├── model
│??     │?? ├── friendmodel.cpp
│??     │?? ├── groupmodel.cpp
│??     │?? ├── offlinemsgmodel.cpp
│??     │?? └── usermodel.cpp
│??     └── redis
│??         └── redis.cpp
├── test
│?? ├── CMakeLists.txt
│?? ├── test_build
│?? └── test.cpp
└── thirdparty
    └── json.hpp

16 directories, 30 files


编译和配置

编译

  1. 可以使用一键编译:

    sh build.sh
  2. 可以进入到build文件中:

    cmake ..
    make

MySQL

  1. 表设计

    user

    friend

    groupuser

    allgroup

    offlinemessage

Nginx

./configuer --with-stream
make && make install
cd /usr/local/nginx/

修改配置文件:

cd conf/
vim nginx.conf

加入模块:

# nginx tcp loadbalance config
stream {
    upstream MyServer {
        server 127.0.0.1:6000 weight=1 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:6002 weight=1 max_fails=3 fail_timeout=30s;
    }

    server {
        proxy_connect_timeout 1s;
        listen 8000;
        proxy_pass MyServer;
        tcp_nodelay on;
    }
}

启动nginx:

pwd
/usr/local/nginx/conf
cd ../sbin/
./nginx

重新启动和停止服务:

nginx -s reload 重新加载配置文件启动
nginx -s stop 停止nginx服务

检查nginx状态:

root@iZwz959g1zzxerwo9dtx98Z:/usr/local/nginx/conf# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      15454/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15454/nginx: master 

Redis

root@iZwz959g1zzxerwo9dtx98Z:/usr/local/nginx/conf# netstat -anpt | grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      598/redis-server 12 
tcp6       0      0 ::1:6379                :::*                    LISTEN      598/redis-server 12 

zjp-chatserver's People

Contributors

mo-xiaoxiu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.