SoFunction
Updated on 2025-04-13

How to open two ports in JAVA springboot

JAVA springboot opens two ports

  • Application scenarios:
  • WeChat applet requires an https port, and the normal background is the http port

1. Modify the configuration file (.yml)

#Development environment configurationserver:
    # The server's HTTP port, default is 8080    port: 8080
# Add start    # https certificate configuration, if you don't write it, just two http ports    # Put the certificate in src/main/resources    ssl:
        key-store: classpath:Certificate name.pfx
        key-alias: alias
        key-store-password: Key
        key-store-type: PKCS12
# Add end    servlet:
        # The access path of the application        context-path: /
    tomcat:
        # tomcat's URI encoding        uri-encoding: UTF-8
        # The number of queues after the number of connections is full, the default is 100        accept-count: 1000
        threads:
            # tomcat maximum number of threads, default is 200            max: 800
            # Tomcat starts initialization number, default value is 10            min-spare: 100
 
# Added, you can not add, and the new port can be written directly into the startup classhttp: # Add a new http port number configuration    port: 8081

2. Modify the startup class (XxApplication)

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
/**
  * Start the program
  *
  * @author
  */
@SpringBootApplication(exclude = {  })
public class XxApplication
{
 
//New start    @Value("${}")
    private Integer port;
 
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        (createStandardConnector()); // Add http        return tomcat;
    }
 
    // Configure http    private Connector createStandardConnector() {
        Connector connector = new Connector(".http11.Http11NioProtocol");
        (port);
        return connector;
    }
//New end 
    public static void main(String[] args)
    {
        // ("", "false");
        (, args);
       
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.