Loved love! SpringBoot+Netty distributed instant messaging system, with source code!

Loved love! SpringBoot+Netty distributed instant messaging system, with source code!

Recently, the company's development needs to use the IM system. If you develop it yourself, it will take more time and energy. So we considered secondary development on the basis of open source projects. After comparing more open source solutions, we chose CIM[1] .

Project Description

The CIM (CROSS-IM) project is an instant messaging system for developers. The project is based on the currently popular SpringBoot (

The Spring Boot version that the project depends on is 1.5.x, you can upgrade to the latest stable version by yourself

), network communication based on Netty. Use Redis to store the client's account/status (online or not)/route information, and use Zookeeper to complete the discovery between services.

Through CIM (CROSS-IM), you can design a horizontally scalable IM of your own.

function list

  • group chat
  • Private chat
  • Chat history query
  • AI automatic chat (a smart mode worth 200 million)
  • Delayed message
  • Client reconnects automatically
  • The server automatically removes offline clients
  • Support horizontal expansion/reduction
  • Support Protocol Buffer protocol

Project screenshot

group chat:

Private chat:

Project technology stack

  • Spring Boot
  • Zookeeper
  • Netty
  • Redis
  • ...

It can be seen that the technologies used in the project are very common technologies, and they are also abilities that we need to master. And the code written by the crossoverJie boss is also quite beautiful, it is worth learning a wave.

Let's take a brief look at the overall system architecture!

system structure

Let's take a look at a few key implementations together.

Simple code analysis

group chat

The use of group chat is very simple, just enter a message in the console and press Enter.

At this time, the group chat interface of route will be called.

The effect achieved is that one of the clients sends a message, and all other clients can receive it!

The process must be that the client sends a message to the server. After receiving it, the server traverses all Channels in the SessionSocketHolder described above and then sends the message.

The server side is a single machine, but it is now a cluster design. Therefore, all clients will be allocated to different server instances according to the previous polling algorithm.

Then the HTTP interface of the server where each client is located is called one by one to push messages.

Private chat

The same is true for private chat, but the premise is that you need to trigger the keyword; use userId;; the format of the message content will send a message to a certain user, so generally you need to use the :olu command to get it first, so it is convenient for online users to use it.

Online user view

This is an auxiliary interface that can query current online user information.

The implementation is also very simple, that is, the de-duplication set that saves the "user login status" before querying.

postscript

CIM[3]The functions of include but are not limited to this, other functions are waiting for you to experience yourself~

Thoughtful crossoverJie chiefs also wrote a lot of the project -related articles [4] , to share some of his development of the project design process issues/encounters, including a lot of thinking big brother. Friends, please make good use of it~

The following is a list of articles:

According to the vision of the crossoverjie boss, there are many todo lists in the project. Interested friends can also contribute their own strengths and actively participate in open source after learning!

If you are interested in project address, source code address, tutorial, please forward + comment, scan