feiyu02
2025-07-23 517296b16b1faf07bc389809387b1937f9415746
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.flightfeather.supervision.infrastructure.config;
 
import cn.flightfeather.supervision.infrastructure.service.NamedThreadFactory;
import cn.flightfeather.supervision.infrastructure.service.NettyConfig;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.HashMap;
import java.util.Map;
 
@Configuration
public class TCPConfig {
    @Bean
    public NamedThreadFactory bossThreadFactory() {
        return new NamedThreadFactory("Server-Boss");
    }
 
    @Bean
    public NamedThreadFactory workerThreadFactory() {
        return new NamedThreadFactory("Server-Worker");
    }
 
    @Bean(destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup workerGroup() {
        return new NioEventLoopGroup(8,workerThreadFactory());
    }
 
    @Bean(destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup bossGroup() {
        return new NioEventLoopGroup(2,bossThreadFactory());
    }
 
    @Bean("nettyConfig")
    public NettyConfig nettyConfig(){
        NettyConfig config = new NettyConfig();
        Map<ChannelOption<?>,Object> tcpChannelOptions = new HashMap<>();
        tcpChannelOptions.put(ChannelOption.SO_KEEPALIVE,true);
        tcpChannelOptions.put(ChannelOption.SO_BACKLOG,100);
        config.setChannelOptions(tcpChannelOptions);
        config.setBossGroup(bossGroup());
        config.setWorkerGroup(workerGroup());
        config.setPortNumber(10086);
        return config;
    }
}