使用两个端口配置 Spring Boot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36357135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configure Spring Boot with two ports
提问by nole
I'm trying configure an application in Spring Boot with two differents ports, but I haven't got still. My first aproximation has been with two controllers and I have defined a @Bean inside the two controller with container.setPort(8080); And my second aproximation has been add the actuator dependency and change the port of the managament, but my application don't run. "Address already in use: bind", How can I confiure an application with two ports? I want one port for admin and the other port is for consults of my api.
我正在尝试使用两个不同的端口在 Spring Boot 中配置一个应用程序,但我还没有。我的第一个近似是使用两个控制器,我在两个控制器中定义了一个 @Bean,容器为 container.setPort(8080); 我的第二个近似是添加执行器依赖项并更改管理端口,但我的应用程序没有运行。“地址已在使用:绑定”,如何配置具有两个端口的应用程序?我想要一个端口用于管理员,另一个端口用于咨询我的 api。
回答by ootero
As is has been mentioned before, server.portand management.portalong with management.context-pathproperties could be set to make the embedded container to listen on different ports (management-related properties to access Actuatorendpoints).
正如前面已经提到的,server.port并management.port沿management.context-path特性可以被设置为使嵌入容器监听不同的端口(管理相关的属性来访问Actuator端点)。
To listen on ports other than server.portand management.port:
侦听server.port和以外的端口management.port:
@Configuration
public class EmbeddedTomcatConfiguration {
@Value("${server.additionalPorts}")
private String additionalPorts;
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
Connector[] additionalConnectors = this.additionalConnector();
if (additionalConnectors != null && additionalConnectors.length > 0) {
tomcat.addAdditionalTomcatConnectors(additionalConnectors);
}
return tomcat;
}
private Connector[] additionalConnector() {
if (StringUtils.isBlank(this.additionalPorts)) {
return null;
}
String[] ports = this.additionalPorts.split(",");
List<Connector> result = new ArrayList<>();
for (String port : ports) {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(Integer.valueOf(port));
result.add(connector);
}
return result.toArray(new Connector[] {});
}
}
application.yml
应用程序.yml
server:
port: ${appPort:8800}
additionalPorts: 8881,8882
Application.java
应用程序.java
@SpringBootApplication
@ComponentScan(...)
@Import(EmbeddedTomcatConfiguration.class)
public Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
I recently blogged about this topic at http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html
我最近在http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html 上写了关于这个话题的博客
回答by Camille Vienot
Since springboot 2, EmbeddedServletContainerFactorymentioned in ootero solutionis no longer available, so you should use either TomcatServletWebServerFactoryor TomcatReactiveWebServerFactoryaccording to your context.
由于ootero 解决方案中EmbeddedServletContainerFactory提到的springboot 2不再可用,因此您应该使用或根据您的上下文。TomcatServletWebServerFactoryTomcatReactiveWebServerFactory
The solution stays the same aside from the factory injection :
除了工厂注入之外,解决方案保持不变:
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
Connector[] additionalConnectors = this.additionalConnector();
if (additionalConnectors != null && additionalConnectors.length > 0) {
tomcat.addAdditionalTomcatConnectors(additionalConnectors);
}
return tomcat;
}
回答by Dennis R
回答by Agung Pramono
To run 2 or more applications within a single project or change the default port, you can perform the action like this
要在单个项目中运行 2 个或更多应用程序或更改默认端口,您可以执行如下操作
@SpringBootApplication
public class NewApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(NewApplication .class);
app.setDefaultProperties(Collections.singletonMap("server.port", "8083"));
app.run(args);
}
}

