
Netty 라이브러리를 사용하여 확장 가능한 서버를 설정하는 Java 프로그램 입니다.
기본이 되는 이 클래스에서는 데이터를 주고 받는 HTTPS 두 개의 포트에 대한 서버를 설정하고 초기화합니다.
public class HttpTestStart {
public static int HTTPS_PORT = 1234;
public static int HTTPS_Clinet_PORT = 1235;
public static void main(String[] args) throws Exception {
// Netty 이벤트 루프 그룹 초기화
EventLoopGroup masterGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// SSL 설정 초기화
SSLHandler.initSSLContext();
// HTTPS 서버 설정
ServerBootstrap serverBootstrapHttps = new ServerBootstrap();
serverBootstrapHttps.group(masterGroup, workerGroup);
serverBootstrapHttps.channel(NioServerSocketChannel.class);
serverBootstrapHttps.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 7000);
serverBootstrapHttps.localAddress(new InetSocketAddress(HttpTestStart.HTTPS_PORT));
serverBootstrapHttps.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
// 채널 파이프라인 초기화
socketChannel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(10));
socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 5));
socketChannel.pipeline().addLast(new HttpTestHandler());
}
});
ChannelFuture channelFutureHttps = serverBootstrapHttps.bind().sync();
// 클라이언트 HTTPS 서버 설정
ServerBootstrap serverBootstrapKTest = new ServerBootstrap();
serverBootstrapKTest.group(masterGroup, workerGroup);
serverBootstrapKTest.channel(NioServerSocketChannel.class);
serverBootstrapKTest.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 7000);
serverBootstrapKTest.localAddress(new InetSocketAddress(HttpTestStart.HTTPS_K_PORT));
serverBootstrapKTest.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
// 채널 파이프라인 초기화
socketChannel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(10));
socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 5));
socketChannel.pipeline().addLast(new HttpTestHandler());
}
});
ChannelFuture channelFutureKTest = serverBootstrapKTest.bind().sync();
// 기타 로그 및 초기화 작업
// ...
// 서버 종료 대기
channelFutureHttps.channel().closeFuture().sync();
channelFutureKTest.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 이벤트 루프 그룹 종료
masterGroup.shutdownGracefully().sync();
workerGroup.shutdownGracefully().sync();
}
}
}
EventLoopGroup masterGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
masterGroup 및 workerGroup은 Netty에서 이벤트 루프 그룹으로, 각각 마스터 이벤트 루프와 워커 이벤트 루프 역할을 합니다.
SSLHandler.initSSLContext();
SSLHandler.initSSLContext()는 SSL 설정을 초기화하는 메서드로, 안전한 통신을 위한 SSL 컨텍스트를 초기화합니다.
ServerBootstrap serverBootstrapHttps = new ServerBootstrap();
serverBootstrapHttps.group(masterGroup, workerGroup);
serverBootstrapHttps.channel(NioServerSocketChannel.class);
serverBootstrapHttps.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 7000);
serverBootstrapHttps.localAddress(new InetSocketAddress(HttpTestStart.HTTPS_PORT));
serverBootstrapHttps.childHandler(new ChannelInitializer<SocketChannel>() { ... });
ChannelFuture channelFutureHttps = serverBootstrapHttps.bind().sync();
bind 메서드로 서버를 시작하고, sync 메서드로 바인딩이 완료될 때까지 대기합니다.
비동기 결과는 channelFutureHttps에 저장됩니다.
Netty는 안전하고 효율적인 비동기 서버를 위한 기본 구성을 제공하는 프레임워크입니다.
Netty를 활용하면 통신 서버를 구축할 때 간편하고 강력한 솔루션을 제공한다는것을 알게되었습니다.