๐Spring WAS
์คํ๋ง ๋ถํธ ์์ฒด๋ WAS๊ฐ ์๋๋ค
๋ด์ฅ ์๋ธ๋ฆฟ ์ปจํ ์ด๋
- ์คํ๋ง ๋ถํธ๋
Tomcat ์๋ฒ
๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ด์ฅ๋์ด ์๋ค. - Tomcat์ ์๋ธ๋ฆฟ ์ปจํ ์ด๋๋ก JSP, Servlet, Http ์์ฒญ๊ณผ ์๋ต ๋ฑ์ ์ฒ๋ฆฌํ๋ค.
์๋์ผ๋ก Tomcat ์คํํด๋ณด๊ธฐ
@SpringBootApplication
public class Application {
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
Context context = tomcat.addContext("/", "/");
HttpServlet servlet = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
PrintWriter writer = resp.getWriter();
writer.println("<h1>Hello world!</h1>");
}
};
final String servletName = "myServlet";
tomcat.addServlet("/", servletName, servlet);
context.addServletMappingDecoded("/home", servletName);
try {
tomcat.start();
tomcat.getServer().await();
} catch (LifecycleException e) {
e.printStackTrace();
}
}
}
Auto Configuration์ ์ด์ฉํ ์คํ๋ง ๋ถํธ์ Tomcat ์คํ
- ์์ ๊ฐ์ ๊ณผ์ ์ ์คํ๋ง ๋ถํธ๋ ์๋์ผ๋ก ์งํํด์ค๋ค. ์ด๋ฅผ ์ํด ๋ค๋ฅธ ํฌ์คํธ๋ก๋ ์ ๋ฆฌํ
Auto Configuration
์ด ์ด์ฉ๋๋ค. ServletWebServerFactoryAutoConfiguration
์TomcatServletWebServerFactory
์์ ์์ ์๋ ์ค์ ๊ณผ ๊ฐ์ ์ผ๋ค์ด ์ผ์ด๋๊ฒ ๋๋ค.
public WebServer getWebServer(ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = this.baseDirectory != null ?
this.baseDirectory :
this.createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
this.customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
this.configureEngine(tomcat.getEngine());
}
๋ด์ฅ ์น ์๋ฒ ๋ณ๊ฒฝ
jetty
๋undertow
์ ๊ฐ์ ๋ค๋ฅธ ์น ์๋ฒ๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค.- ๊ทธ๋ฌ๊ธฐ ์ํด์๋ ์ฐ์ Tomcat์ ๋ํ ์์กด์ฑ๋ถํฐ ์ ์ธํ์ฌ์ผ ํ๋ค.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
ํฌํธ ๋ณ๊ฒฝ
resources/application.properties
ํน์application.yml
์์ ์ค์ ์ ๋ณ๊ฒฝํ ์ ์๋ค.- 0์ผ๋ก ์ค์ ์ Random ํฌํธ๊ฐ ์ง์ ๋๋ค. ์๋ฒ ์ธ์คํด์ค๋ฅผ ๋์ ์ผ๋ก ์ฌ๋ฌ ๊ฐ ๋์ธ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
server.port=8443
- ์คํ๋ง ๋ถํธ์ ๊ธฐ๋์ ๋ก๊ทธ๋ฅผ ํตํด์๋ ํฌํธ๋ฅผ ํ์ธํ ์ ์์ง๋ง, ์ฝ๋๋ฅผ ํตํด์๋ ์๋์ ๊ฐ์ด ํ์ธ์ด ๊ฐ๋ฅํ๋ค.
ApplicationListener
์onApplicationEvent
๋ฉ์๋๋ Application์ด ์์ฑ๋๋ฉด ํธ์ถ๋๋ Callback method์ด๋ค.
// ApplicationListener๋ ๋น์ฐํ์ง๋ง Bean์ด์ด์ผ ํธ์ถ๋๋ค.
@Component
public class PortListener implements ApplicationListener<ServletWebServerInitializedEvent> {
@Override
public void onApplicationEvent(ServletWebServerInitializedEvent servletWebServerInitializedEvent) {
WebServer webServer = servletWebServerInitializedEvent.getApplicationContext().getWebServer();
System.out.println("port: " + webServer.getPort());
}
}
HTTPS ์๋ฒ ๊ธฐ๋
- HTTPS๋ ์๋ค์ํผ ๋น๋์นญํค๋ฅผ ์ด์ฉํ ๋ณด์ ํ๋กํ ์ฝ์ด๋ค. ๋จผ์ ํค๋ฅผ ์์ฑํด์ผ ํ๋ค.
keytool -genkey -alias { ๋ณ๋ช
} -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 4000
์คํ๋ง ๋ถํธ์ HTTPS ์ค์ ํ๊ธฐ
application.properties
ํน์application.yml
ํ์ผ์ ์๋์ ๊ฐ์ด ์ค์ ํ๋ค.
server.ssl.key-store=keystore.p12
server.ssl.key-store-password={ ํค ์์ฑ์ ์ค์ ํ ๋น๋ฐ๋ฒํธ }
server.ssl.key-store-type=PKCS12
server.ssl.key-alias={ ํค ์์ฑ์ ์ค์ ํ ๋ณ๋ช
}
- ์ด๋ ๊ฒ ์ค์ ์ HTTPS(443 ํฌํธ)๋ก ์ ์ํ ์ ์๋ค. ๋ฌผ๋ก ๊ณต์ธ์ธ์ฆ์๊ฐ ์๋๊ธฐ ๋๋ฌธ์ ํฌ๋กฌ ๋ฑ์ ๋ธ๋ผ์ฐ์ ์์ ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ๋ํ๋ด๊ธด ํ ๊ฒ์ด๋ค.
- ๊ทธ๋ฆฌ๊ณ ๊ธฐ์กด์ HTTP(80 ํฌํธ)๋ ์ ์์ด ๋์ง ์๋๋ค. ํ์ํ๋ค๋ฉด ์๋์ ๊ฐ์ด
Connector
๋ฅผ ์ถ๊ฐํ์ฌ ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
@SpringBootApplication
public class Application {
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8080); // HTTPS์ฉ ํฌํธ์ ๋ค๋ฅด๊ฒ ์ค์ ํด์ผ ํ๋ค.
return connector;
}
public static void main(String[] args) {
// SpringApplication.run(Application.class, args);
new SpringApplicationBuilder() // ์ฌ์กฑ์ผ๋ก ๋น๋ ํจํด์ ์ด์ฉํด ์คํ๋ง ๊ธฐ๋๋ ๊ฐ๋ฅํ๋ค.
.sources(Application.class)
.bannerMode(Banner.Mode.OFF) // ์์์ ์ถ๋ ฅ๋๋ ๋ฐฐ๋๋ ๋ ์ ์๋ค.
.run(args);
}
}
ServletWebServerFactoryAutoConfiguration
์์๋ ์ฝ๋๋ฅผ ๋ค์ ์ดํด๋ณด๋ฉด Connector๋ฅผ ์ค์ ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
HTTP/2 ํ์ฑํ
application.properties
์server.http2.enabled=true
๋ก ์ค์ ํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.- ๋ฌผ๋ก ์ ์ ๋ก HTTPS๊ฐ ์ค์ ๋์ด ์์ด์ผ ํ๋ค.
[1] ๋ฐฑ๊ธฐ์ , ์คํ๋ง ๋ถํธ ๊ฐ๋ ๊ณผ ํ์ฉ, 3๋ถ ์คํ๋ง ๋ถํธ ์๋ฆฌ