How to Configure Embedded Tomcat Integrated with Spring to Listen Requests to Ip Address, Besides Localhost

How to configure embedded Tomcat integrated with Spring to listen requests to IP address, besides localhost?

In order to specify a which IP you want Tomcat to bind too, I believe you can simply add the following to your application.properties:

server.address=<your_ip>
server.port=<your_port>

Replacing <your_ip> with the IP address you want it to listen on. This, and other basic properties, can be found in the Spring Boot Reference Guide, Appendix A.

The other way to configure the embedded Tomcat is to create a custom configurer in code by implementing the EmbeddedServletContainerCustomizer interface. You can read more about this in the Spring Boot Reference Guide, Section 55.5-55.8.

How to configure low-level socket properties for a web server (Tomcat) embedded in my spring-boot application?

This answer is Tomcat-specific

Turns out this is possible through TomcatConnectorCustomizer beans, e.g.

@Bean
public TomcatConnectorCustomizer tomcatConnectorCustomizer() {
return connector -> connector.setProperty("socket.soReuseAddress", "true");
}

How does it work?

connector.setProperty is implemented with IntrospectionUtils.setProperty which looks for bean property with a matching name, and failing that, uses a getProperty method in the connector's ProtocolHandler.

In my case, the ProtocolHandler was org.apache.coyote.http11.Http11NioProtocol which has a setProperty in its superclass org.apache.coyote.AbstractProtocol, that in the end invokes org.apache.tomcat.util.net.AbstractEndpoint's setProperty.

That method then differentiates properties with socket. prefix, and again reflectively matches it to org.apache.tomcat.util.net.SocketProperties accessors (in our case setSoReuseAddress).

In the end, when the sockets get created, the stored configuration is used when configuring a new socket.


It should be also noted (again, this is Tomcat specific) that Tomcat also uses JVM defaults (link).

How do you configure tomcat to bind to a single ip address (localhost) instead of all addresses?

Several connectors are configured, and each connector has an optional "address" attribute where you can set the IP address.

  1. Edit tomcat/conf/server.xml.
  2. Specify a bind address for that connector:

    <Connector
    port="8080"
    protocol="HTTP/1.1"
    address="127.0.0.1"
    connectionTimeout="20000"
    redirectPort="8443"
    />

Configure Spring Boot to listen for request beside localhost

as @Robert Masen suggested problem was dormitory network blocking connection on 8080 port.



Related Topics



Leave a reply



Submit