Bootstrapping
8.1 ๋ถํธ์คํธ๋ฉ ํด๋์ค
๋ถํธ์คํธ๋ฉ ํด๋์ค ๊ณ์ธต ๊ตฌ์กฐ๋ ์ถ์ ๋ถ๋ชจ ํด๋์ค์ ๋ ๊ฐ์ ๊ตฌ์ฒด์ ์ธ ๋ถํธ์คํธ๋ฉ ํ์ ํด๋์ค๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
์ฆ, ์๋ฒ๋ ํด๋ผ์ด์ธํธ์ ์ฐ๊ฒฐ์ ์๋ฝํ๊ณ ๊ทธ๋ค๊ณผ ํต์ ํ๊ธฐ ์ํด ์์ ์ฑ๋์ ์์ฑํ๋ ๋ถ๋ชจ ์ฑ๋์ ์ฌ์ฉํ์ง๋ง, ํด๋ผ์ด์ธํธ๋ ๋๋ถ๋ถ์ ๋คํธ์ํฌ ์ํธ์์ฉ์ ์ํด ๋จ์ผ ๋น๋ถ๋ชจ ์ฑ๋๋ง ํ์๋ก ํฉ๋๋ค.
ํด๋ผ์ด์ธํธ ๋๋ ์๋ฒ์ ํน์ ํ ๋จ๊ณ๋ฅผ ์ ์ธํ๊ณ , ๋ถํธ์คํธ๋ฉ ๊ณผ์ ์ ๊ณตํต ๋จ๊ณ๋ AbstractBootstrap
์์ ์ฒ๋ฆฌ๋๊ณ , ๊ฐ๊ฐ Bootstrap
๋๋ ServerBootstrap
์์ ์ฒ๋ฆฌ๋ฉ๋๋ค.
8.2 ํด๋ผ์ด์ธํธ์ ๋น์ฐ๊ฒฐํ ํ๋กํ ์ฝ ๋ถํธ์คํธ๋ํ
Bootstrap
์ ํด๋ผ์ด์ธํธ ๋๋ ๋น์ฐ๊ฒฐํ ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ์ ํ๋ฆฌ์ผ์ด์
์์ ์ฌ์ฉ๋ฉ๋๋ค.
Bootstrap group(EventLoopGroup)
์ฑ๋์ ๋ชจ๋ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ EventLoopGroup
์ ์ค์ ํฉ๋๋ค.
Bootstrap channel(Class<? extends C>)
์ฑ๋ ๊ตฌํ ํด๋์ค๋ฅผ ์ง์ ํฉ๋๋ค.
Bootstrap channelFactory(ChannelFactory<? extends C>)
bind()
์ ์ํด ํธ์ถ๋ ํฉํ ๋ฆฌ ํด๋์ค๋ฅผ ์ง์ ํฉ๋๋ค.
Bootstrap localAddress(SocketAddress)
์ฑ๋์ด ๋ฐ์ธ๋๋ ๋ก์ปฌ ์ฃผ์๋ฅผ ์ง์ ํฉ๋๋ค.
<T> Bootstrap option(ChannelOption<T> option, T value)
์๋ก ์์ฑ๋ ์ฑ๋์ ChannelConfig
์ ์ ์ฉ๋ ChannelOption
์ ์ค์ ํฉ๋๋ค.
<T> Bootstrap attr(Attribute<T> key, T value)
์๋ก ์์ฑ๋ ์ฑ๋์ ์์ฑ์ ์ง์ ํฉ๋๋ค.
Bootstrap handler(ChannelHandler)
์ด๋ฒคํธ ์๋ฆผ์ ๋ฐ์ ChannelHandler
๋ฅผ ์ค์ ํฉ๋๋ค.
Bootstrap clone()
ํ์ฌ Bootstrap
์ ๋ณต์ ๋ณธ์ ์์ฑํฉ๋๋ค.
Bootstrap remoteAddress(SocketAddress)
์๊ฒฉ ์ฃผ์๋ฅผ ์ค์ ํฉ๋๋ค.
ChannelFuture connect()
์๊ฒฉ ํผ์ด์ ์ฐ๊ฒฐํ๊ณ ์ฐ๊ฒฐ ์์
์ด ์๋ฃ๋๋ฉด ChannelFuture
๋ฅผ ๋ฐํํฉ๋๋ค.
ChannelFuture bind()
์ฑ๋์ ๋ฐ์ธ๋ํ๊ณ ๋ฐ์ธ๋ ์์
์ด ์๋ฃ๋๋ฉด ChannelFuture
๋ฅผ ๋ฐํํฉ๋๋ค.
8.2.1 ํด๋ผ์ด์ธํธ ๋ถํธ์คํธ๋ํ
Bootstrap
ํด๋์ค๋ ํด๋ผ์ด์ธํธ ๋ฐ ๋น์ฐ๊ฒฐํ ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ์ ํ๋ฆฌ์ผ์ด์
์์ ์ฑ๋์ ์์ฑํ๋ ์ฑ
์์ ์ง๋๋ค.
8.2.2 ์ฑ๋๊ณผ EventLoopGroup ํธํ์ฑ
Netty์์๋ EventLoopGroup
๊ณผ Channel
๊ตฌํ์ด ํธํ๋์ด์ผ ํฉ๋๋ค.
NIO
๋ฐ OIO
์ ์ก์ ์ํ ๊ด๋ จ EventLoopGroup
๊ณผ Channel
๊ตฌํ์ด ์์ต๋๋ค.
NioEventLoopGroup
๊ณผ OioSocketChannel
์ ํผํฉํ์ฌ ์ฌ์ฉํ๋ ค๊ณ ์๋ํ๊ธฐ ๋๋ฌธ์ IllegalStateException
์ ๋ฐ์์ํต๋๋ค.
๋ถํธ์คํธ๋ํ ์, bind()
๋๋ connect()
๋ฅผ ํธ์ถํ๊ธฐ ์ ์ ๋ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํ์ํ ๊ตฌ์ฑ ์์๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.
group()
channel()
๋๋channelFactory()
handler()
์ด ์์
์ ์ํํ์ง ์์ผ๋ฉด IllegalStateException
์ด ๋ฐ์ํฉ๋๋ค. ํนํ handler()
ํธ์ถ์ ChannelPipeline
์ ๊ตฌ์ฑํ๋ ๋ฐ ํ์ํ๋ฏ๋ก ๋งค์ฐ ์ค์ํฉ๋๋ค.
8.3 ์๋ฒ ๋ถํธ์คํธ๋ํ
์๋ฒ ๋ถํธ์คํธ๋ํ์ ๊ฐ์๋ก ServerBootstrap API๋ฅผ ์ค๋ช ํ ํ, ์๋ฒ ๋ถํธ์คํธ๋ํ์ ํ์ํ ๋จ๊ณ์ ๊ด๋ จ๋ ๋ช ๊ฐ์ง ์ฃผ์ ๋ฅผ ์ค๋ช ํฉ๋๋ค. ์ฌ๊ธฐ์๋ ์๋ฒ ์ฑ๋์์ ํด๋ผ์ด์ธํธ๋ฅผ ๋ถํธ์คํธ๋ฉํ๋ ํน๋ณํ ๊ฒฝ์ฐ๋ ํฌํจ๋ฉ๋๋ค.
8.3.1 ServerBootstrap ํด๋์ค
๋ค์ ํ๋ ServerBootstrap ํด๋์ค์ ๋ฉ์๋๋ฅผ ๋์ดํฉ๋๋ค.
group
ServerBootstrap์์ ์ฌ์ฉํ EventLoopGroup์ ์ค์ ํฉ๋๋ค. ์ด EventLoopGroup์ ServerChannel๊ณผ ์๋ฝ๋ ์ฑ๋์ I/O๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
channel
์ธ์คํด์คํํ ServerChannel ํด๋์ค๋ฅผ ์ค์ ํฉ๋๋ค.
channelFactory
Channel์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํตํด ์์ฑํ ์ ์๋ ๊ฒฝ์ฐ ChannelFactory๋ฅผ ์ ๊ณตํฉ๋๋ค.
localAddress
ServerChannel์ด ๋ฐ์ธ๋ฉํ ๋ก์ปฌ ์ฃผ์๋ฅผ ์ง์ ํฉ๋๋ค. ์ง์ ํ์ง ์์ผ๋ฉด OS์์ ๋ฌด์์๋ก ํ ๋น๋ฉ๋๋ค.
option
์๋ก ์์ฑ๋ ServerChannel์ ChannelConfig์ ์ ์ฉํ ChannelOption์ ์ง์ ํฉ๋๋ค.
childOption
์๋ฝ๋ ์ฑ๋์ ChannelConfig์ ์ ์ฉํ ChannelOption์ ์ง์ ํฉ๋๋ค.
attr
ServerChannel์ ๋ํ ์์ฑ์ ์ง์ ํฉ๋๋ค. bind() ํธ์ถ ํ์๋ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
childAttr
์๋ฝ๋ ์ฑ๋์ ๋ํ ์์ฑ์ ์ ์ฉํฉ๋๋ค. ์ดํ ํธ์ถ์ ํจ๊ณผ๊ฐ ์์ต๋๋ค.
handler
ServerChannel์ ChannelPipeline์ ์ถ๊ฐํ ChannelHandler๋ฅผ ์ค์ ํฉ๋๋ค.
childHandler
์๋ฝ๋ ์ฑ๋์ ChannelPipeline์ ์ถ๊ฐํ ChannelHandler๋ฅผ ์ค์ ํฉ๋๋ค.
clone
์๋ณธ ServerBootstrap๊ณผ ๋์ผํ ์ค์ ์ผ๋ก ๋ค๋ฅธ ์๊ฒฉ ํผ์ด์ ์ฐ๊ฒฐํ๊ธฐ ์ํด ServerBootstrap์ ๋ณต์ ํฉ๋๋ค.
bind
ServerChannel์ ๋ฐ์ธ๋ฉํ๊ณ , ์ฐ๊ฒฐ ์์ ์ด ์๋ฃ๋๋ฉด ChannelFuture๋ฅผ ๋ฐํํฉ๋๋ค.
8.3.2 ์๋ฒ ๋ถํธ์คํธ๋ํ
์๋ฒ ์ฑ๋์ bind()
๊ฐ ํธ์ถ๋ ๋ ์์ฑ๋ฉ๋๋ค.
์๋ก์ด ์ฑ๋์ ServerChannel์ด ์ฐ๊ฒฐ์ ์๋ฝํ ๋ ์์ฑ๋ฉ๋๋ค.
8.5 ๋ถํธ์คํธ๋ฉ ์ค ์ฌ๋ฌ ChannelHandler ์ถ๊ฐ
Netty๋ ChannelPipeline์ ์ฌ๋ฌ ChannelHandler๋ฅผ ์ถ๊ฐํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
๋ถํธ์คํธ๋ฉ ๊ณผ์ ์์ handler()
๋๋ childHandler()
๋ฅผ ํธ์ถํ์ฌ ๋จ์ผ ChannelHandler๋ฅผ ์ถ๊ฐํ๋ ๊ฒ ์ธ์, ์ฌ๋ฌ ChannelHandler๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ChannelInitializer
ํด๋์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค.
8.6 Netty ChannelOptions ๋ฐ ์์ฑ ์ฌ์ฉํ๊ธฐ
๋ชจ๋ ์ฑ๋์ ์๋์ผ๋ก ๊ตฌ์ฑํ๋ ๊ฒ์ ๋งค์ฐ ๋ฒ๊ฑฐ๋ก์ธ ์ ์์ต๋๋ค. ๋คํํ๋, ๋ถํธ์คํธ๋ฉ์์ option()
์ ์ฌ์ฉํ์ฌ ChannelOptions์ ์ ์ฉํ ์ ์์ต๋๋ค.
์ ๊ณต๋ ๊ฐ๋ค์ ๋ถํธ์คํธ๋ฉ์์ ์์ฑ๋ ๋ชจ๋ ์ฑ๋์ ์๋์ผ๋ก ์ ์ฉ๋ฉ๋๋ค. ChannelOptions์๋ keep-alive๋ timeout ์์ฑ ๋ฐ ๋ฒํผ ์ค์ ๊ณผ ๊ฐ์ ์ ์์ค์ ์ฐ๊ฒฐ ์ธ๋ถ ์ฌํญ์ด ํฌํจ๋ฉ๋๋ค.
8.7 DatagramChannels ๋ถํธ์คํธ๋ํ
์ด์ ๋ถํธ์คํธ๋ฉ ์ฝ๋ ์์ ๋ TCP ๊ธฐ๋ฐ์ SocketChannel์ ์ฌ์ฉํ์ง๋ง, ๋ถํธ์คํธ๋ฉ์ ๋น์ฐ๊ฒฐ ํ๋กํ ์ฝ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Netty๋ ์ด ๋ชฉ์ ์ ์ํด ๋ค์ํ DatagramChannel ๊ตฌํ์ ์ ๊ณตํฉ๋๋ค. ์ ์ผํ ์ฐจ์ด์ ์ connect()
๋ฅผ ํธ์ถํ์ง ์๊ณ bind()
๋ง ํธ์ถํ๋ค๋ ๊ฒ์
๋๋ค.
8.8 Shutdown
๋ถํธ์คํธ๋ํ์ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ ์ ์์ง๋ง, ๊ฒฐ๊ตญ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ข ๋ฃํด์ผ ํ๋ ์์ ์ด ์ฌ ๊ฒ์ ๋๋ค. ๋ฌผ๋ก , JVM์ด ์ข ๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆด ์ ์์ง๋ง, ์ด๋ ์์์ ๊นจ๋ํ๊ฒ ํด์ ํ๋ ๊ฒ์ ์๋ฏธํ๋ '์ฐ์ํ ์ข ๋ฃ(graceful shutdown)'์ ์ ์์ ๋ง์ง ์์ต๋๋ค.
์ฐ์ , EventLoopGroup
์ ์ข
๋ฃํด์ผ ํฉ๋๋ค. ์ด๋ ๋ชจ๋ ๋ณด๋ฅ ์ค์ธ ์ด๋ฒคํธ์ ์์
์ ์ฒ๋ฆฌํ ํ ๋ชจ๋ ํ์ฑ ์ค๋ ๋๋ฅผ ํด์ ํฉ๋๋ค.
Last updated