業務核心部分都進行完畢了,就剩下 Netty 組件關係的配置 以及 NettyServer 的定義了。
package houlei.net.tcp.cfg;
import houlei.net.tcp.codec.GenericPackageClassifierCodec;
import houlei.net.tcp.codec.GenericPackageCodec;
import houlei.net.tcp.hdr.NettyServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
@Configuration
public class NettyConfigure {
@Value("${netty.boss.thread.count}")
private int bossCount;
@Value("${netty.worker.thread.count}")
private int workerCount;
@Value("${netty.server.port}")
private int serverPort;
@Value("${netty.so.backlog}")
private int backlog;
@Value("${netty.io.idle.readTimeout}")
private int readerIdleTimeSeconds;
@Resource
private GenericPackageClassifierCodec classifierCodec;
@Resource
private NettyServerHandler serverHandler;
@Bean
public InetSocketAddress serverSocketAddress() {
return new InetSocketAddress(serverPort);
}
@Bean(name = "bossGroup", destroyMethod = "shutdownGracefully")
public NioEventLoopGroup bossGroup() {
return new NioEventLoopGroup(bossCount);
}
@Bean(name = "workerGroup", destroyMethod = "shutdownGracefully")
public NioEventLoopGroup workerGroup() {
return new NioEventLoopGroup(workerCount);
}
@Bean
public ServerBootstrap serverBootstrap() {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup(), workerGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("logging", new LoggingHandler(LogLevel.DEBUG));
pipeline.addLast("generic-codec", new GenericPackageCodec());
pipeline.addLast("classifier-codec", classifierCodec);
pipeline.addLast("server-handler", serverHandler);
}
});
b.option(ChannelOption.SO_BACKLOG, backlog);
return b;
}
}
package houlei.net.tcp;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
@Component
public class NettyServer {
@Resource
private ServerBootstrap serverBootstrap;
@Resource
private InetSocketAddress serverSocketAddress;
private ChannelFuture serverChannelFuture;
public void start() throws Exception {
System.out.println("Starting server at " + serverSocketAddress);
serverChannelFuture = serverBootstrap.bind(serverSocketAddress).sync();
}
public void stop() throws Exception {
serverChannelFuture.channel().closeFuture().sync();
}
}
## application.properties
netty.server.port= 6060
netty.boss.thread.count= 1
netty.worker.thread.count= 8
netty.so.backlog= 100
netty.io.idle.readTimeout= 45
action.executor.corePoolSize=8
action.executor.maxPoolSize=16
action.executor.keepAliveTime=10000
spring.application.admin.enabled=false
spring.jmx.enabled=false
server.port=9090
logging.level.root= info
logging.level.org.springframework.boot.autoconfigure= ERROR
logging.level.io.netty= debug
服務端的Demo展示,到此結束。歡迎大家討論研究。
請不要拿我寫的樣例代碼去盈利或者完成作業,謝謝大家了。