csjoz11

导航

修改Springboot启动端口号

前言
Springboot启动的时候,端口的设定默认是8080,这肯定是不行的,我们需要自己定义端口,Springboot提供了两种方式,第一种,我们可以通过application.yml配置文件配置,第二种,可以通过代码里面指定,在开发中,建议使用修改application.yml的方式来修改端口。
 

一、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.guoxin</groupId>
    <artifactId>SpringbootTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>SpringBootTest</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
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
28
29
30
31
32
33
34
35
36
37
38
39
 

二、修改配置文件改端口
application.yml和properties配置文件一样,但是相对于properties文件更加简约一些。
application.yml:

server:
  #端口号
  port: 8888
  #项目名,如果不设定,默认是 /
  context-path: /demo
1
2
3
4
5
application.properties:

server.port=8081
server.context-path=/demo
1
2


三、代码指定端口
方法一:
这种方式,是通过编码的方式来硬性的指定了端口的配置

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class ConfigMain extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {  

    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
        return builder.sources(ConfigMain.class);  
    }  

    @Override  
    public void customize(ConfigurableEmbeddedServletContainer container) {
        //指定项目名称
        container.setContextPath("/demo");
        //指定端口地址
        container.setPort(8081);  
    }

    public static void main(String[] args) {
        SpringApplication.run(ConfigMain.class, args);
    }
}
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
方法二:
这种方法有springboot版本的限制,1.3.x即以前的好使,1.4.x就不建议使用了(import org.springframework.boot.context.embedded.ServletContextInitializer;ServletContextInitializer被划了黑色的横线,后来发现虽然没报错,但是访问页面却没有,说明不能正常使用),1.5.x即以后就已经没法用了(前面引的包会报错)

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.stereotype.Component;

@Component
public class MyEmbeddedServletContainerFactory extends TomcatEmbeddedServletContainerFactory {
    public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
        this.setPort(8081);
        this.setContextPath("/demo");
        return super.getEmbeddedServletContainer(initializers);
    }

//    protected void customizeConnector(Connector connector) {
//        super.customizeConnector(connector);
//        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
//        // 设置最大连接数
//        protocol.setMaxConnections(2000);
//        // 设置最大线程数
//        protocol.setMaxThreads(2000);
//        protocol.setConnectionTimeout(30000);
//    }
}
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
 

四、使用启动命令
nohup java -jar -Dspring.config.location=application.properties -Dserver.port=8897 dataMq.jar &
注:即使application.properties文件中设置了server.port=8081,启动端口也是8897
————————————————
版权声明:本文为CSDN博主「小强签名设计」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37739193/article/details/83388271

posted on 2021-09-15 15:16  csjoz11  阅读(3020)  评论(0编辑  收藏  举报