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.
- Edit
tomcat/conf/server.xml
. - 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
Apache Httpclient 4.0.3 - How to Set Cookie with Sessionid for Post Request
Java, Pass-By-Value, Reference Variables
Java Erasure with Generic Overloading (Not Overriding)
Why Are Variables Declared with Their Interface Name in Java
Java: Set Timeout on a Certain Block of Code
Override "Private" Method in Java
Any Recommended Java Profiling Tutorial
Adding Jradiobutton into Jtable
Remove Duplicates from Arraylists
When Using == for a Primitive and a Boxed Value, Is Autoboxing Done, or Is Unboxing Done
Passing an Array or List to @Pathvariable - Spring/Java
Netbeans - Error: Could Not Find or Load Main Class
Spring Web MVC - Validate Individual Request Params
Hashcode and Equals for Hashset