Springboot+netty+websocket 实现单聊群聊及用户鉴权

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

技术栈: SpringBoot+netty+websocket+mybatis-plus


一、配置

Maven

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.5</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>


        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>12.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.75.Final</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.80</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>

spring配置

server:
  port: 10086
netty:
  host: 127.0.0.1
  port: 10010
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test_one?useUnicode=true&useSSL=false
    username: xzq
    password: root
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
  type-aliases-package: com.xzq.netty.websocket.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

二、Netty服务

1. WsServer构建

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WsServer {
    @Value("${netty.host}")
    public String host;
    @Value("${netty.port}")
    public Integer port;
    @Autowired
    private WsChannelInitializer channelInitializer;

    private NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    private NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    public void listener() {
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap()
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    //指定服务端连接队列长度,也就是服务端处理线程全部繁忙,并且队列长度已达到1024个,后续请求将会拒绝
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(channelInitializer);

            log.info("Netty start successful " + host + ":" + port);
            ChannelFuture f = serverBootstrap.bind(host, port).sync();
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            log.info("Ws服务启动失败:" + e);
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public void destroy() {
        log.info("JVM close,WsServer close");
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

2. ChannelInitializer构建

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WsChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private AuthHandler authHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        //http 协议编解码器
        ch.pipeline().addLast("http-codec", new HttpServerCodec());
        //http请求聚合处理,多个HTTP请求或响应聚合为一个FullHtppRequest或FullHttpResponse
        ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        //鉴权处理器
        ch.pipeline().addLast("auth", authHandler);
        //大数据的分区传输
        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        //websocket协议处理器
        ch.pipeline().addLast("websocket", new WebSocketServerProtocolHandler("/im"));
        //自定义消息处理器
        ch.pipeline().addLast("my-handler", new WsChannelHandler());
    }
}

3. 自定义消息处理器

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xzq.netty.websocket.message.ClientMessage;
import com.xzq.netty.websocket.util.MsgUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WsChannelHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("有客户端建立连接");
        log.info("客户端address: " + channel.remoteAddress().toString());
        log.info("客户端channel Id:" + channel.id().toString());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        String body = msg.text();
        ClientMessage clientMessage = JSONObject.parseObject(body, ClientMessage.class);
        if (clientMessage.getType() == 1) {
            WsChannelGroup.userChannelGroup.get(clientMessage.getTo()).writeAndFlush(MsgUtil.buildSingleMsg(ctx.channel().id().toString(), clientMessage.getMsgInfo()));
            return;
        }
        WsChannelGroup.channelGroup.writeAndFlush(MsgUtil.buildGroupMsg(ctx.channel().id().toString(),clientMessage.getMsgInfo()));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("客户端断开连接... 客户端 address: " + channel.remoteAddress());
        WsChannelGroup.channelGroup.remove(channel);
        WsChannelGroup.userChannelGroup.remove(channel.id().toString(), channel);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (cause instanceof JSONException) {
            ctx.channel().writeAndFlush(new TextWebSocketFrame("服务端仅支持JSON格式体消息"));
            return;
        }
        Channel channel = ctx.channel();
        log.info(channel.remoteAddress()+" 连接异常,断开连接...");
        cause.printStackTrace();
        ctx.channel().writeAndFlush(new TextWebSocketFrame("服务端500 关闭连接"));
        ctx.channel().closeFuture();
        WsChannelGroup.channelGroup.remove(channel);
        WsChannelGroup.userChannelGroup.remove(channel.id().toString(), channel);
    }
}

4. 全局用户Session管理

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WsChannelGroup  {
    public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    public static Map<String, Channel> userChannelGroup = new ConcurrentHashMap<>();
}

5. 客户端与服务端消息实体

@Data
public class ClientMessage implements Serializable {
    /**
     *  消息类型:  1 单聊  2,群聊
     */
    private int type;

    /**
     * 消息内容
     */
    private String msgInfo;

    /**
     *  消息发送方 (目前只在单聊中体现,【群聊暂时没有分组处理】)
     */
    private String to;

}

@Data
public class ServerMessage implements Serializable {
    /**
     * 消息发送方
     */
    private String from;
    /**
     * 消息内容
     */
    private String msgInfo;

    /**
     * 时间
     */
    private String date;

    public ServerMessage(String from, String msgInfo) {
        this.from = from;
        this.msgInfo = msgInfo;
        this.date = LocalDateTime.now().toString();
    }
}

6. 消息构建工具类

import com.alibaba.fastjson.JSONObject;
import com.xzq.netty.websocket.message.ServerMessage;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;


public class MsgUtil {

    public static TextWebSocketFrame buildSingleMsg(String from, String msgInfo) {
        return new TextWebSocketFrame(JSONObject.toJSONString(new ServerMessage(from, msgInfo)));
    }

    public static TextWebSocketFrame buildGroupMsg(String from,String msgInfo) {
        return new TextWebSocketFrame(JSONObject.toJSONString(new ServerMessage(from, msgInfo)));
    }
}

7. 鉴权处理器

import cn.hutool.core.util.StrUtil;
import com.xzq.netty.websocket.entity.TestUser;
import com.xzq.netty.websocket.mapper.TestUserMapper;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
public class AuthHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    private TestUserMapper userMapper;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof FullHttpRequest) {
            FullHttpRequest msg1 = (FullHttpRequest) msg;
            //根据请求头的 auth-token 进行鉴权操作
            String authToken = msg1.headers().get("auth-token");
            System.out.println(">>>>>>>>>>>>鉴权操作");
            if (StrUtil.isEmpty(authToken)) {
                refuseChannel(ctx);
                return;
            }
            //查询数据库是否存在该用户
            TestUser testUser = userMapper.selectById(authToken);
            if (testUser == null) {
                refuseChannel(ctx);
            }
            //鉴权成功,添加channel用户组
            WsChannelGroup.channelGroup.add(ctx.channel());
            WsChannelGroup.userChannelGroup.put(testUser.getName(), ctx.channel());
        }
        ctx.fireChannelRead(msg);
    }

    private void refuseChannel(ChannelHandlerContext ctx) {
        ctx.channel().writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED));
        ctx.channel().close();
    }
}

三, 整合spring

mport com.xzq.netty.websocket.server.WsServer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.xzq.netty.websocket.mapper")
public class WsApplication implements CommandLineRunner {

    @Autowired
    private WsServer wsServer;


    public static void main(String[] args) {
        SpringApplication.run(WsApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        wsServer.listener();
        //钩子函数,虚拟机正常关闭调用
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            wsServer.destroy();
        }));
    }
}
```

# 四,测试
开启三个websocket客户端并都建立连接
![在这里插入图片描述](https://img-blog.csdnimg.cn/045f579ec75b4cae9b358eb073cef88d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQ29jb3h6cTAwMA==,size_20,color_FFFFFF,t_70,g_se,x_16)
发送消息
客户端2收到消息
![在这里插入图片描述](https://img-blog.csdnimg.cn/ebc7637bc17c4f54aae9d142b2e76f5f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQ29jb3h6cTAwMA==,size_20,color_FFFFFF,t_70,g_se,x_16)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值