好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springBoot server.port=-1的含义说明

springBoot server.port=-1的含义

今天遇到这种新奇的写法,项目是可以正常启动的。

然而http的端口有范围:1~65535。-1是访问不了的

而且只要是负数,最终启动日志打印的都是

Tomcat started on port(s): -1 (http) with context path ''

那springBoot放开负一端口的含义是什么,直接到官方文档中找答案:

明确说明了,放开-1是为了:完全关闭HTTP端点,但仍创建一个WebApplicationContext

还发现了另外一个好玩的配置:server.port=0

含义是:

扫描可用端口(使用OS本机来防止冲突)

也就是说,配置了server.port=0,项目启动时会自动扫描可用端口,然后启动=w=

Springboot的server.port和server.http.port

需求

最近springboot项目为了安全启用了https,但是项目中还写了接口供其他程序调用,这个接口必须是http的。研究发现原来一个springboot项目是可以有一个http端口和一个https端口的。

正文

配置文件如下:

?

1

2

3

4

#http port

server.http.port= 1234

#https port

server.port= 1233

项目启动的时候使用的是server.port端口。

配置的http端口要想使用需要写下面这样一个配置类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import org.apache.catalina.connector.Connector;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;

import org.springframework.boot.web.servlet.server.ServletWebServerFactory;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

@Configuration

public class HttpsConfig {

     @Value ( "${server.http.port}" )

     private Integer httpPort;

     @Bean

     public ServletWebServerFactory serverFactory() {

         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();

         tomcat.addAdditionalTomcatConnectors(createStandardConnector());

         return tomcat;

     }

     /**

      * 配置http

      * @return

      */

     private Connector createStandardConnector() {

         Connector connector = new Connector( "org.apache.coyote.http11.Http11NioProtocol" );

         connector.setPort(httpPort);

         return connector;

     }

}

之后写接口的时候便可以使用这个端口了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/Dawn_Bells/article/details/103873501

查看更多关于springBoot server.port=-1的含义说明的详细内容...

  阅读:26次