hyuko

Netty 클라이언트와 서버의 차이점 본문

Springboot

Netty 클라이언트와 서버의 차이점

hyuko12 2024. 7. 19. 10:08
728x90
반응형

- 큰 차이점이라고 한다면 bootstrap 설정에서의 group이 하나만 들어간다.

- channel 설정이 NioSocketChannel.class 이다.

- 네티 클라이언트의 연결의 경우에는 bootstrap을 통해 bind 하는 것이 아닌 connect를 써서 진행

 

아래는 해당 차이점에 대한 변경들을 코드로 보여준다.

@Slf4j
@Configuration
@RequiredArgsConstructor
public class NettyClientConfig {

    @Bean
    public Bootstrap readBootstrap(ClientChannelInitializer nettyChannelInitializer) {
        log.info("bootstrap create for client");
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group()).channel(NioSocketChannel.class).handler(nettyChannelInitializer);

        return bootstrap;
    }


    @Bean(destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup group() {
        return new NioEventLoopGroup();
    }


}

 

private void connectToServer(ModbusRTUServerInfo info) {
        log.info("connect to server");
        bootstrap.connect(info.getHost(), info.getPort())
                .addListener((ChannelFutureListener) future -> {
                    if (future.isSuccess()) {
                        handleConnectionSuccess(info);
                    } else {
                        handleConnectionFailure(info);
                    }
                });
    }
728x90
반응형

'Springboot' 카테고리의 다른 글

Spring Cloud Stream  (0) 2024.07.19
Netty Client  (0) 2024.07.19
네티 서버 구현  (0) 2024.07.19
Netty  (0) 2024.07.19
spring security / 전체적인 흐름  (0) 2023.04.29