Created
September 26, 2019 03:08
-
-
Save yingzhuo/135ee2248d2c1e5e2a8446235baf524c to your computer and use it in GitHub Desktop.
基于SpringBoot的Web程序监听多个端口 #spring boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.yingzhuo.carnival.mvc.autoconfig; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.val; | |
import org.apache.catalina.connector.Connector; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; | |
import org.springframework.context.annotation.Bean; | |
@ConditionalOnWebApplication | |
@ConditionalOnProperty(prefix = "server.http", name = "enabled", havingValue = "true") | |
@EnableConfigurationProperties(MvcBothPortAutoConfig.Props.class) | |
public class MvcBothPortAutoConfig { | |
@Autowired | |
private Props props; | |
@Bean | |
public TomcatServletWebServerFactory tomcatServletWebServerFactory() { | |
val bean = new TomcatServletWebServerFactory(); | |
bean.addAdditionalTomcatConnectors(connector()); | |
return bean; | |
} | |
private Connector connector() { | |
val connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); | |
connector.setScheme("http"); | |
connector.setPort(props.getPort()); | |
return connector; | |
} | |
@Getter | |
@Setter | |
@ConfigurationProperties(prefix = "server.http") | |
static class Props { | |
private boolean enabled = false; | |
private int port = -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment