Skip to content

Instantly share code, notes, and snippets.

@yingzhuo
Created September 26, 2019 03:08
Show Gist options
  • Save yingzhuo/135ee2248d2c1e5e2a8446235baf524c to your computer and use it in GitHub Desktop.
Save yingzhuo/135ee2248d2c1e5e2a8446235baf524c to your computer and use it in GitHub Desktop.
基于SpringBoot的Web程序监听多个端口 #spring boot
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