使用两个端口配置Spring Boot [英] Configure Spring Boot with two ports
问题描述
我正在尝试使用两个不同的端口在Spring Boot中配置一个应用程序,但是我还没停下来. 我的第一个代理是使用两个控制器,我在两个控制器内使用container.setPort(8080);定义了一个@Bean. 我的第二个替代方法是添加执行器依赖性并更改管理端口,但是我的应用程序无法运行. 地址已在使用中:绑定", 如何配置具有两个端口的应用程序?我想要一个端口用于管理员,而另一个端口则用于我的api的咨询.
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.
推荐答案
如前所述,可以设置server.port
和management.port
以及management.context-path
属性,以使嵌入式容器在不同的端口上侦听(与管理相关的属性以访问Actuator
端点).
As is has been mentioned before, server.port
and management.port
along with management.context-path
properties could be set to make the embedded container to listen on different ports (management-related properties to access Actuator
endpoints).
要在server.port
和management.port
以外的端口上侦听:
To listen on ports other than server.port
and 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
application.yml
server:
port: ${appPort:8800}
additionalPorts: 8881,8882
Application.java
Application.java
@SpringBootApplication
@ComponentScan(...)
@Import(EmbeddedTomcatConfiguration.class)
public Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
我最近在 查看全文