Communication between spring boot dockerized apps - java

I new using spring boot and docker and I faced a problem running the docker containers.
On debug mode, there is no problem on applications boot, but when I run them as a container, there is something wrong.
For example, I have my server config with all the yml files, also eureka properties.
The config server boot perfectly, but not the eureka server, it must look for it`s configuration to the config server becouse of these:
uri: ${vcap.services.config-service.credentials.uri:http://127.0.0.1:8888}
In the eureka`s log I can found:
Could not locate PropertySource: I/O error on GET request for
"http://127.0.0.1:8888/server-eureka/default":Connection refused;
nested exception is java.net.ConnectException: Connection refused
So I see that eureka cant connect to the config server for a reason I cant understund.
Maybe I miss something in my docker file.

If you are not using docker linked containers you'll have to use only the public ip addresses. Docker will assign every running container an own ip address which is per default not accessible. Only when you start to expose ports there will be an entry to iptables that is linking the hosts public ip address and given port to the internal used port and (dynamically assigned) ip address of the docker container. This is also why 127.0.0.1 does not work cause it would look into the containers local context but tgere the service is not running.

Related

Configuring server and port for Tomcat Spring Boot application

I am deploying my Spring Boot application via uploading a JAR to Elastic Beanstalk.
I am getting the following error:
*1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.43.15, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.42.2"
and
I do not understand which configurations are missing. Here it says:
The Tomcat platform uses nginx (the default) or Apache HTTP Server as the reverse proxy to relay requests from port 80 on the instance to your Tomcat web container listening on port 8080. Elastic Beanstalk provides a default proxy configuration that you can extend or override completely with your own configuration.
I feel like this should give me some hint, but I still don't get what exactly I need to change.
Similar issue: 502 bad gateway Elastic Beanstalk Spring Boot
From the suggestions there I tried:
defining server.port=8080 in my application.properties
adding PORT or SERVER_PORT environment properties directly in EB
changing Java to version 8 in pom.xml
changing load balancers ports here in EB to 8080:
I might be changing things to a wrong port, or maybe in an unfortunate combination - but nothing seems to work for me.
I never had to do such things, so for me it's all super unclear and I would be thankful for all kinds of help.

URL for webapp Docker Tomcat IntelliJ

I'm using a Tomcat 9 server in a Docker container to deploy locally a webapp for development purposes. I can connect to my Tomcat using http://localhost:8080/ But I can't find my webapp URL anywhere. My Docker container is deployed from IntelliJ, and I have no URL field in the configuration of the Container.
Does anyone know where to find/set the URL ?
There is no URL to be set, at least not explicitly.
Once you have your application started in a container (either started through IntelliJ IDEA, Docker Desktop (for), command line...) with the port binding configuration (the Bind ports config section in your screenshot), you are only left with the application as if it was started on the host on the mapped port (the first section before the colon : in the port binding).
Which means you can simply access your application on:
http://localhost:8080
following the pattern for a URL: (protocol)://(host)(:port) where:
protocol is HTTP since you mentioned using Tomcat as a web server
host being you local station where the docker daemon is running on
port being the port you chose to map to the started container port

Spring boot admin with docker resolves docker hostnames and IPs

My Issue/Setup
I'm trying to build a stand-alone spring boot admin application which all our instances (in a private VPC- behind a network load balancer) can register with.
But my spring boot application itself runs inside a docker container, so hostname and ip-address always defaults to the container hostname and container ip-address.
And spring-boot admin fails with
`Request failed with status code 502`
Connection refused: /1XX.17.0.2:8840
because it cannot find actual hosts with the hostname or the IP addres.
What i tried
I had the set up working with below (one instance),
`spring.boot.admin.client.instance.service-base-url`
But cannot use this for multiple clients as i cannot specify a comma separated value.
Moreover, since there can be n number of instance at any time i do not want to use the below property.
`spring.boot.admin.client.instance.service-base-url`
So i took the suggestion to omit spring.boot.admin.client.instance.service-base-url on spring-boot-admin git hub page and issues section and used below instead
`spring.boot.admin.client.instance.prefer-ip=true`
But Since my applications are running inside a docker container,
spring boot clients register itself with container id/ip-address as the hostname/ip-address.
I also tried the "--net=admin-network","--name client" parameters in my AWS beanstalk file
"containerDefinitions": [
{
"name": "cloudy-email",
"image": "xxx.dkr.ecr.us-east-2.amazonaws.com/sba-admin-repo:xx_2019-04-24_14.36_e9aax8710",
"command": [ "--net=admin-network","--name client"],
"Update": "true",
How can i overcome this issue?
Any pointers/references would be much appreciated.
You can make docker run in the host network using the --network host option to the docker run command. This will make the container assume the same IP address of the host VM. Also, check if you are exposing the admin port through Docker and the host firewall (security groups).
More info: https://docs.docker.com/network/host/

How to make Spring Boot application accessible by external IP address of the server?

I'm a newbie to Spring Boot. I have a REST API application written in Spring Boot. When I execute my Spring Boot JAR, everything is okay and I can access the REST API with the localhost address instead of the actual one:
http://localhost:8083/articles
But when I try to access the REST API by my external IP address, I can't do it:
http://100.90.80.70:8083/articles
netstat -antu command in the Linux terminal gives me the following output:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp6 0 0 :::8083 :::* LISTEN
As I understand, my app is accessible only in localhost, because it hasn't a foreign address.
My application.properties file has only this line:
server.port=8083
Also, when I try to add a server.address line to application.properties like that:
server.address=100.90.80.70
server.port=8083
I have the following Exception: Caused by: java.net.BindException: Cannot assign requested address.
So my question is: how to make Spring Boot application accessible by external IP address of the server? Thank you.
As #Mark said, the problem is in the firewall. I have opened 8083 port in the firewall settings and now I can access my REST API app by the external IP address:
http://100.90.80.70:8083/articles
Linux command to check firewall status:
sudo ufw status verbose
Open 8083 port for remote access by TCP protocol:
sudo ufw allow 8083/tcp
More settings here: https://www.cyberciti.biz/faq/how-to-open-firewall-port-on-ubuntu-linux-12-04-14-04-lts/
I fixed the same by configuring port forwarding on my router, to allow traffic from public ip
In my OpenStack environment, after much debugging, the solution was to create a new Security Group Rule, which looks like this:
Security Group Rule.
Note that my Spring Boot application was deployed on port 8080.
I also noticed that on ubuntu18 the firewall is disabled by default. It did not cause any problems.

Windows Server 2012, Apache Tomcat, Spring MVC: Websocket connection blocked for external IP

We've deployed our Spring MVC web application on Windows Server 2012. Our web-app uses Spring Websockets for updates with stomp.js and sock.js.
Our websocket configuration:
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/calcApp");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/add").setAllowedOrigins("*").withSockJS();
}
}
Websocket works on localhost and logs are following:
Opening Web Socket...
Web Socket Opened...
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:admin
connected to server undefined
>>> SUBSCRIBE
id:sub-0
destination:/topic/resident
...
Strangely, it doesn't work when I enter external ip, on same machine and browser:
Opening Web Socket...
WebSocket connection to 'ws://192.168.5.50:8080/autopark/add/629/i148hb1c/websocket' failed: WebSocket is closed before the connection is established.
Whoops! Lost connection to undefined
We thought that for external access, there is some firewall and totally disabled it:
But it didn't solve our problem.
How can we solve this issue?
I'm not really sure and not a spring expert.
but it seems you need to call the server by a domain name over its ip addr which is logical.
Since an ip would be used for more than one domain, so it seems the context need to know which context should be called(even if one) in spring context.
in other word, calling the context by ip would confuse the spring context to select/invoke which context/domain, so it refuses the connection.
have atry, binf the 192.168.5.50 to a domain name, then try to call the path using the domain(not ip). Hope it works this way.
The first step in debugging this would be verifying that your application server is actually listening on the external interface.
You can verify what IP your container is bound to by looking for 8080 entries in the output of netstat.
netstat -a -n -o | find "8080"
If you don't see an entry bound to either 0.0.0.0 or the external IP, then we know it is a configuration issue with your application server.
Example for embedded tomcat - How to set a IP address to the tomcat?
Example for standalone tomcat - How do you configure tomcat to bind to a single ip address (localhost) instead of all addresses?
The next step should be verifying an external computer can "see" the port on the external IP. There are various ways to do this, but using the telnet command will suffice.
telnet 192.168.5.50 8080
If this does not work, then we know there is something blocking communication between the two applications
If we get to this point, then there is likely an issue with the configuration of the application itself.

Categories

Resources