springboot不使用內置tomcat啓動,用jetty或undertow

Spring Boot啓動程序一般使用Tomcat做爲默認的嵌入式服務器若是須要更改 - 您能夠排除Tomcat依賴項並改成包含Jetty或Undertow:java

jetty配置:web

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

 

@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
    JettyEmbeddedServletContainerFactory jettyContainer = 
      new JettyEmbeddedServletContainerFactory();
     
    jettyContainer.setPort(9000);
    jettyContainer.setContextPath("/springbootapp");
    return jettyContainer;
}

undertow配置:spring

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
    UndertowEmbeddedServletContainerFactory factory = 
      new UndertowEmbeddedServletContainerFactory();
     
    factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
        @Override
        public void customize(io.undertow.Undertow.Builder builder) {
            builder.addHttpListener(8080, "0.0.0.0");
        }
    });
     
    return factory;
}

 

 

9.在引導應用程序中配置Jetty或Undertow

Spring Boot啓動程序一般使用Tomcat做爲默認的嵌入式服務器若是須要更改 - 您能夠排除Tomcat依賴項並改成包含Jetty或Undertow:tomcat

配置Jettyspringboot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< dependency >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-web</ artifactId >
     < exclusions >
         < exclusion >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-tomcat</ artifactId >
         </ exclusion >
     </ exclusions >
</ dependency >
< dependency >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-jetty</ artifactId >
</ dependency >
1
2
3
4
5
6
7
8
9
@Bean
public JettyEmbeddedServletContainerFactory  jettyEmbeddedServletContainerFactory() {
     JettyEmbeddedServletContainerFactory jettyContainer =
       new JettyEmbeddedServletContainerFactory();
     
     jettyContainer.setPort( 9000 );
     jettyContainer.setContextPath( "/springbootapp" );
     return jettyContainer;
}

配置Undertow服務器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< dependency >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-web</ artifactId >
     < exclusions >
         < exclusion >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-tomcat</ artifactId >
         </ exclusion >
     </ exclusions >
</ dependency >
< dependency >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-undertow</ artifactId >
</ dependency >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
     UndertowEmbeddedServletContainerFactory factory =
       new UndertowEmbeddedServletContainerFactory();
     
     factory.addBuilderCustomizers( new UndertowBuilderCustomizer() {
         @Override
         public void customize(io.undertow.Undertow.Builder builder) {
             builder.addHttpListener( 8080 , "0.0.0.0" );
         }
     });
     
     return factory;
}
相關文章
相關標籤/搜索