Netty實戰:Netty優雅的創建高性能TCP服務器(附源碼)

 


前言

Springboot使用Netty優雅、快速的創建高性能TCP服務器,適合作爲開發腳手架進行二次開發。

1. 前置準備

  • 引入依賴
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--      netty包  -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.75.Final</version>
        </dependency>
         <!--   常用JSON工具包 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.80</version>
        </dependency>
  • 編寫yml配置文件
# tcp
netty:
  server:
    host: 127.0.0.1
    port: 20000
    # 傳輸模式linux上開啓會有更高的性能
    use-epoll: false

# 日記配置
logging:
  level:
    # 開啓debug日記打印
    com.netty: debug
  • 讀取YML中的服務配置
/**
 * 讀取YML中的服務配置
 *
 * @author ding
 */
@Configuration
@ConfigurationProperties(prefix = ServerProperties.PREFIX)
@Data
public class ServerProperties {

    public static final String PREFIX = "netty.server";

    /**
     * 服務器ip
     */
    private String ip;

    /**
     * 服務器端口
     */
    private Integer port;

    /**
     * 傳輸模式linux上開啓會有更高的性能
     */
    private boolean useEpoll;

}

2. 消息處理器

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * 消息處理,單例啓動
 *
 * @author qiding
 */
@Slf4j
@Component
@ChannelHandler.Sharable
@RequiredArgsConstructor
public class MessageHandler extends SimpleChannelInboundHandler<String> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
        log.debug("\n");
        log.debug("channelId:" + ctx.channel().id());
        log.debug("收到消息:{}", message);
        // 回覆客戶端
        ctx.writeAndFlush("服務器接收成功!");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        log.debug("\n");
        log.debug("開始連接");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.debug("\n");
        log.debug("成功建立連接,channelId:{}", ctx.channel().id());
        super.channelActive(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        log.debug("心跳事件時觸發");
        if (evt instanceof IdleStateEvent) {
            log.debug("發送心跳");
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}

3. 重寫通道初始化類

添加我們需要的解碼器,這裏添加了String解碼器和編碼器

import com.netty.server.handler.MessageHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/**
 * Netty 通道初始化
 *
 * @author qiding
 */
@Component
@RequiredArgsConstructor
public class ChannelInit extends ChannelInitializer<SocketChannel> {

    private final MessageHandler messageHandler;

    @Override
    protected void initChannel(SocketChannel channel) {
        channel.pipeline()
                // 心跳時間
                .addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS))
                // 添加解碼器
                .addLast(new StringDecoder())
                // 添加編碼器
                .addLast(new StringEncoder())
                // 添加消息處理器
                .addLast("messageHandler", messageHandler);
    }

}

4. 核心服務

  • 接口
public interface ITcpServer {

    /**
     * 主啓動程序,初始化參數
     *
     * @throws Exception 初始化異常
     */
    void start() throws Exception;

    /**
     * 優雅的結束服務器
     *
     * @throws InterruptedException 提前中斷異常
     */
    @PreDestroy
    void destroy() throws InterruptedException;
}
  • 服務實現
/**
 * 啓動 Server
 *
 * @author qiding
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class TcpServer implements ITcpServer {

    private final ChannelInit channelInit;

    private final ServerProperties serverProperties;

    private EventLoopGroup bossGroup;

    private EventLoopGroup workerGroup;

    @Override
    public void start() {
        log.info("初始化 TCP server ...");
        bossGroup = serverProperties.isUseEpoll() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
        workerGroup = serverProperties.isUseEpoll() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
        this.tcpServer();
    }


    /**
     * 初始化
     */
    private void tcpServer() {
        try {
            new ServerBootstrap()
                    .group(bossGroup, workerGroup)
                    .channel(serverProperties.isUseEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(serverProperties.getPort()))
                    // 配置 編碼器、解碼器、業務處理
                    .childHandler(channelInit)
                    // tcp緩衝區
                    .option(ChannelOption.SO_BACKLOG, 128)
                    // 將網絡數據積累到一定的數量後,服務器端才發送出去,會造成一定的延遲。希望服務是低延遲的,建議將TCP_NODELAY設置爲true
                    .childOption(ChannelOption.TCP_NODELAY, false)
                    // 保持長連接
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    // 綁定端口,開始接收進來的連接
                    .bind().sync();
            log.info("tcpServer啓動成功!開始監聽端口:{}", serverProperties.getPort());
        } catch (Exception e) {
            e.printStackTrace();
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    /**
     * 銷燬
     */
    @PreDestroy
    @Override
    public void destroy() {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

5. 效果預覽

  • 啓動類添加啓動方法
/**
 * @author ding
 */
@SpringBootApplication
@RequiredArgsConstructor
public class NettyServerApplication implements ApplicationRunner {

    private final TcpServer tcpServer;

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

    @Override
    public void run(ApplicationArguments args) throws Exception {
        tcpServer.start();
    }
}
  • 運行
    在這裏插入圖片描述

  • 打開tcp客戶端工具進行測試
    在這裏插入圖片描述

6. 添加通道管理,給指定的客戶端發送消息

爲了給指定客戶端發送消息,我們需要設置一個登錄機制,保存登錄成功的客戶端ID和頻道的關係

  • 編寫通道存儲類
/**
 * 頻道信息存儲
 * <p>
 * 封裝netty的頻道存儲,客戶端id和頻道雙向綁定
 *
 * @author qiding
 */
@Slf4j
public class ChannelStore {

    /**
     * 頻道綁定 key
     */
    private final static AttributeKey<Object> CLIENT_ID = AttributeKey.valueOf("clientId");

    /**
     * 客戶端和頻道綁定
     */
    private final static ConcurrentHashMap<String, ChannelId> CLIENT_CHANNEL_MAP = new ConcurrentHashMap<>(16);

    /**
     * 存儲頻道
     */
    public final static ChannelGroup CHANNEL_GROUP = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    /**
     * 重入鎖
     */
    private static final Lock LOCK = new ReentrantLock();

    /**
     * 獲取單機連接數量
     */
    public static int getLocalConnectCount() {
        return CHANNEL_GROUP.size();
    }

    /**
     * 獲取綁定的通道數量(測試用)
     */
    public static int getBindCount() {
        return CLIENT_CHANNEL_MAP.size();
    }

    /**
     * 綁定頻道和客戶端id
     *
     * @param ctx      連接頻道
     * @param clientId 用戶id
     */
    public static void bind(ChannelHandlerContext ctx, String clientId) {
        LOCK.lock();
        try {
            // 釋放舊的連接
            closeAndClean(clientId);
            // 綁定客戶端id到頻道上
            ctx.channel().attr(CLIENT_ID).set(clientId);
            // 雙向保存客戶端id和頻道
            CLIENT_CHANNEL_MAP.put(clientId, ctx.channel().id());
            // 保存頻道
            CHANNEL_GROUP.add(ctx.channel());
        } finally {
            LOCK.unlock();
        }
    }

    /**
     * 是否已登錄
     */
    public static boolean isAuth(ChannelHandlerContext ctx) {
        return !StringUtil.isNullOrEmpty(getClientId(ctx));
    }

    /**
     * 獲取客戶端id
     *
     * @param ctx 連接頻道
     */
    public static String getClientId(ChannelHandlerContext ctx) {
        return ctx.channel().hasAttr(CLIENT_ID) ? (String) ctx.channel().attr(CLIENT_ID).get() : "";
    }

    /**
     * 獲取頻道
     *
     * @param clientId 客戶端id
     */
    public static Channel getChannel(String clientId) {
        return Optional.of(CLIENT_CHANNEL_MAP.containsKey(clientId))
                .filter(Boolean::booleanValue)
                .map(b -> CLIENT_CHANNEL_MAP.get(clientId))
                .map(CHANNEL_GROUP::find)
                .orElse(null);
    }

    /**
     * 釋放連接和資源
     * CLIENT_CHANNEL_MAP 需要釋放
     * CHANNEL_GROUP 不需要釋放,netty會自動幫我們移除
     *
     * @param clientId 客戶端id
     */
    public static void closeAndClean(String clientId) {
        // 清除綁定關係
        Optional.of(CLIENT_CHANNEL_MAP.containsKey(clientId))
                .filter(Boolean::booleanValue)
                .ifPresent(oldChannel -> CLIENT_CHANNEL_MAP.remove(clientId));
        // 若存在舊連接,則關閉舊連接,相同clientId,不允許重複連接
        Optional.ofNullable(getChannel(clientId))
                .ifPresent(ChannelOutboundInvoker::close);
    }

    public static void closeAndClean(ChannelHandlerContext ctx) {
        closeAndClean(getClientId(ctx));
    }

}
  • 配置登錄機制

我們在消息處理器 MessageHandler 中修改channelRead0方法,模擬登錄

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
        log.debug("\n");
        log.debug("channelId:" + ctx.channel().id());
        log.debug("收到消息:{}", message);
        // 判斷是否未登錄
        if (!ChannelStore.isAuth(ctx)) {
            // 登錄邏輯自行實現,我這裏爲了演示把第一次發送的消息作爲客戶端ID
            String clientId = message.trim();
            ChannelStore.bind(ctx, clientId);
            log.debug("登錄成功");
            ctx.writeAndFlush("login successfully");
            return;
        }
        // 回覆客戶端
        ctx.writeAndFlush("ok");
    }

    /**
     * 指定客戶端發送
     *
     * @param clientId 其它已成功登錄的客戶端
     * @param message  消息
     */
    public void sendByClientId(String clientId, String message) {
        Channel channel = ChannelStore.getChannel(clientId);
        channel.writeAndFlush(message);
    }

調用sendByClientId即可給已登錄的其它客戶端發送消息了。

7. 源碼分享

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章