Files
jsowell-charger-web/jsowell-netty/src/main/java/com/jsowell/netty/server/NettyServerManager.java

167 lines
8.0 KiB
Java
Raw Normal View History

2024-07-15 11:33:11 +08:00
package com.jsowell.netty.server;
import com.jsowell.common.constant.Constants;
2024-07-15 13:49:40 +08:00
import com.jsowell.netty.server.electricbicycles.ElectricBicyclesServerChannelInitializer;
2024-07-15 11:54:13 +08:00
import com.jsowell.netty.server.mqtt.BootNettyMqttChannelInboundHandler;
2024-07-15 11:33:11 +08:00
import com.jsowell.netty.server.yunkuaichong.NettyServerChannelInitializer;
import io.netty.bootstrap.ServerBootstrap;
2024-07-15 11:54:13 +08:00
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
2024-07-15 11:33:11 +08:00
import io.netty.channel.nio.NioEventLoopGroup;
2024-07-15 11:54:13 +08:00
import io.netty.channel.socket.SocketChannel;
2024-07-15 11:33:11 +08:00
import io.netty.channel.socket.nio.NioServerSocketChannel;
2024-07-15 11:54:13 +08:00
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
2024-07-15 11:33:11 +08:00
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
2024-07-15 11:54:13 +08:00
import io.netty.handler.timeout.IdleStateHandler;
2024-07-15 11:33:11 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
@Slf4j
@Component
public class NettyServerManager implements CommandLineRunner {
@Resource
private NettyServerChannelInitializer nettyServerChannelInitializer;
2024-07-15 13:49:40 +08:00
@Resource
private ElectricBicyclesServerChannelInitializer electricBicyclesServerChannelInitializer;
2024-12-15 14:33:29 +08:00
private int bossGroupSize = Runtime.getRuntime().availableProcessors();
private int workerGroupSize = bossGroupSize * 2;
2024-07-15 11:33:11 +08:00
@Override
public void run(String... args) throws Exception {
startNettyServer(Constants.SOCKET_IP, 9011);
startElectricBikeNettyServer(Constants.SOCKET_IP, 9012);
2024-07-15 16:05:15 +08:00
// startMqttSever(Constants.SOCKET_IP, 1883);
2024-07-15 11:33:11 +08:00
}
public void startNettyServer(String host, int port) {
new Thread(() -> {
2024-12-15 14:33:29 +08:00
EventLoopGroup bossGroup = new NioEventLoopGroup(bossGroupSize);
EventLoopGroup workerGroup = new NioEventLoopGroup(workerGroupSize);
2024-07-15 11:33:11 +08:00
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
// .option(ChannelOption.SO_BACKLOG, 128) // 默认128
2024-11-28 10:09:51 +08:00
.option(ChannelOption.SO_BACKLOG, 1024)
2024-12-15 14:33:29 +08:00
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
2024-11-28 10:09:51 +08:00
// .option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true) // 保持连接
2024-12-15 14:33:29 +08:00
// .childOption(ChannelOption.SO_RCVBUF, 64 * 1024) // 接收缓冲区
.childOption(ChannelOption.SO_RCVBUF, 1024 * 1024) // 接收缓冲区
// .childOption(ChannelOption.SO_SNDBUF, 64 * 1024) // 发送缓冲区
.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024) // 发送缓冲区
2024-11-28 10:09:51 +08:00
.childOption(ChannelOption.TCP_NODELAY, true) // 禁用 Nagle 算法
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 64 * 1024)) // 写缓冲水位
2024-07-15 11:33:11 +08:00
.childOption(ChannelOption.SO_REUSEADDR, true)
2024-12-15 14:33:29 +08:00
// .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
2024-07-15 11:33:11 +08:00
.childHandler(nettyServerChannelInitializer)
.localAddress(new InetSocketAddress(host, port));
ChannelFuture future = bootstrap.bind(port).sync();
if (future.isSuccess()) {
log.info("NettyServer启动成功, 开始监听端口:{}", port);
} else {
log.error("NettyServer启动失败", future.cause());
}
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("NettyServer.start error", e);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}
public void startElectricBikeNettyServer(String host, int port) {
new Thread(() -> {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
2024-12-29 11:24:50 +08:00
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
2024-07-15 11:33:11 +08:00
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true)
2024-07-15 13:49:40 +08:00
.childHandler(electricBicyclesServerChannelInitializer)
2024-07-15 11:33:11 +08:00
.localAddress(new InetSocketAddress(host, port));
ChannelFuture future = bootstrap.bind(port).sync();
if (future.isSuccess()) {
log.info("ElectricBikeNettyServer启动成功, 开始监听端口:{}", port);
} else {
log.error("ElectricBikeNettyServer启动失败", future.cause());
}
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("ElectricBikeNettyServer.start error", e);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}
2024-07-15 11:54:13 +08:00
public void startMqttSever(String host, int port) {
new Thread(() -> {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap mqttBootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.localAddress(new InetSocketAddress(host, port));
mqttBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
2024-12-15 14:33:29 +08:00
@Override
2024-07-15 11:54:13 +08:00
protected void initChannel(SocketChannel ch) {
ChannelPipeline channelPipeline = ch.pipeline();
// 设置读写空闲超时时间
channelPipeline.addLast(new IdleStateHandler(600, 600, 1200));
channelPipeline.addLast("encoder", MqttEncoder.INSTANCE);
channelPipeline.addLast("decoder", new MqttDecoder());
channelPipeline.addLast(new BootNettyMqttChannelInboundHandler());
}
});
ChannelFuture future = mqttBootstrap.bind(port).sync();
if (future.isSuccess()) {
log.info("MqttServer启动成功, 开始监听端口:{}", port);
} else {
log.error("MqttServer启动失败", future.cause());
}
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("MqttServer.start error", e);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}
2024-07-15 11:33:11 +08:00
}