Debugging two spring web services in vscode fails - spring

I have created two simple Spring Boot web services in vscode, and one of the web services calls the other so I need to run them both at the same time while debugging at least one of them. Both of them work if I debug them independently, for example if I just right click the main class and select the Debug command from the menu. For example, I do this sequence:
Start Service1 in Debug
Test Service1 by making a call to the URL from the browser and verify it works (it does)
Start Service2 in Debug
Check to see that Service2 is running (it is)
Try to test Service2 by making a call to its URL from the browser. It crashes (disappears from running processes)
Stop all debugging
Start Service2 in Debug
Start Service1 in Debug
Try to test Service1 by making a call to its URL from the browser. It crashes.
I've added the two services as compounds in launch.json but that didn't help. The first service that starts works while the second one crashes.
Does anyone have any recommended configuration settings that I should check?

The problem was that I had both services listening on the same port.

Related

OpenFaas : getting Upstream HTTP request error: Post http://127.0.0.1:8082/: EOF while deploying long running springboot service

I am trying to deploy a long running Springboot service on OpenFaas and facing Upstream HTTP request error: Post http://127.0.0.1:8082/: EOF
I have a springboot service which exposes API's.
To deploy this service on openfaas the following steps were performed.
the service depends on external dependencies which are placed in a folder in root project.
Updated build.gradle with implementation fileTree("$folderPath") to include external dependencies.
Started the Springboot Application run process in Handler class, but getting Upstream HTTP request error: Post http://127.0.0.1:8082/: EOF while we try to invoke the Handler.
Tried increasing the exec_time, but it dint help.
How do we run a long running springboot process in OpenFaas?
If the dependencies are loaded while building, how are they resolved at runtime inside docker container ?
Go http clients usually return EOF in case of request timeouts. OpenFaas watchdog, gateway, queue workers, all use GO http clients internally. Most probably your OpenFaas installation is not configured properly for long running functions.
You can double check this by making your Springboot API return immediately and see if that works.
You can refer to this to configure your OpenFaas to run long running functions. This is an excellent sample function for long running functions.
For your second question, if your faas build is succeeding (and i am assuming you are working with standard OpenFaas Java template), it packages the complete output of gradle build inside the docker container, which should carry all your dependency files as well.

`ApplicationRunner` wait until other service is up

We have an ApplicationRunner to create an admin user on the first start of a spring boot application.
To create the user, it must connect to an auth server (we use Keycloak). However, if the service is deployed together with the main application (via docker-compose up -d) it will take some time until the auth server is available, actually, too long. The ApplicationRunner will fail with a 502 Bad Gateway exception, because it is executed before the auth server is up and running.
How can the ApplicationRunner delay creating the admin until the auth server is up?
Ideally, the ApplicationRunner should delay everything and provide some information about the "waiting state" during startup. If after e.g. 1 min or so the auth server is not available during startup, the application run should fail.
Notes
We are using docker-compose version 3. We are looking for an application level solution, because docker-compose docs state that this should be handled on the application level.
As long as you use Docker-Compose, you can Control startup and shutdown order in Compose.
my-service:
image: my-company/my-service:1.0.0
container_name: my-service
restart: on-failure
depends_on:
my-auth-server-service:
condition: service_healthy
ports:
- 8080:8080
my-auth-server-service:
image: ...
container_name: ...
...
However, it doesn't guarantee the "readiness" of the service itself so an application-specific health or readiness check would be required (also described in the link above).
You might want define HEALTHCHECK in the Dockerfile of the authorization my-auth-server-service service (feel free to do in every service) to detect the health/readiness through a REST API call. As long as you use Spring, the actuator endpoint is suitable.
Here is a short example, however, you might want to define additional/custom logic being able to detect the complete readiness.
RUN apk --no-cache add curl
HEALTHCHECK --interval=20s --timeout=3s --retries=20 \
CMD curl --fail http://localhost:8080/actuator/health || exit 1
Conceptually, the startup procedure may just fail the complete application run, because it is an initialization script. Without success of this initialization the complete application cannot be used. The context / deployment should handle when to (re)start what.
If the initialization script fails (and consequently the complete application run), then using docker-compose's restart: unless-stopped configuration on the main application will just retry starting it. Eventually, the auth server gets up in the meantime and finally the main service is up running.
Turns out that implementing ApplicationRunner it is not as easy to stop the complete application context during startup. Instead, implementing SmartLifecycle and moving the logic into start() is a better idea. The method may just throw an exception to fail the application run on startup.

Usecase for start & stop methods of Spring Lifecycle

What is a good usecase for start & stop methods of spring Lifecycle interface?
I see that listening & stopping to listen on a port while the application deployment is going on is a usecase for this, but this is not a good usecase. This is my reasoning. If the application is being redeployed, the context cannot be maintained as the new app might have a different code base than the current code in the context
Appreciate if someone can help me with a good usecase for Lifecycle interface
Quick ones - You had app running as a Service but server restarted for Software patch/upgrade OR worst Server crashed OR you want to do fire some event at start/shutdown.
You will restart and expect all services to start as well. You don't want to create a runbook and run them manually in some order...

Spring Boot (Tomcat) based application as daemon - howto stop?

I wrote a Spring Boot webservice that uses an embedded tomcat as container.
In case the system reboots I want to backup some information to a mysql database.
In my webservice I use #Scheduled() and #PreDestroy to run the backup.
This goes well when I stop the server with ^C.
But when I kill the process with an sysV skript (/etc/init.d) and the kill command - even though the daemon has a dependency on mysql, the mysql server is shut down before the backup is finished (resulting in SQL Exceptions in my log).
The reason for this is of course, that kill only sends a signal to stop the process.
How can I (from my sysv skript) synchroneously stop the running spring boot tomcat server?
If you include spring-boot-starter-actuator then that provides a REST endpoint for management. One of the endpoints provided is /shutdown. By hitting that endpoint, you will get a controlled shutdown of all resources, which ensures that #PreDestroy will be called. As this could be dangerous to have enabled by default, to use it you will need to add the following to your application.properties file:
endpoints.shutdown.enabled=true
Of course, once you have exposed that endpoint you need to ensure that there's a teeny bit of security applied to prevent just anybody shutting down your server.
On a related note, you may find my answer to Spring Boot application as a Service useful, where I provided the code for a full init.d script which makes use of this.
As an alternative to the "/shutdown" endpoint the Actuator also has an ApplicationPidListener (not enabled by default) that you can use to create a pidfile (which is commonly used in "init.d" style scripts to kill a process when you want to stop it). The JVM should respond to a kill (sigint) and Spring will shutdown gracefully.

Spring server start-up event

i am facing a problem. I am using tomcat server for my spring maven project and i want to subscribe my application to facebook once when server starts up. I have tried ApplicationListener ContextRefreshedEvent. It gets invoked on application context initialization, but the issue is at that time my server has not completely started and hence my application is not publically accessible which is required in my case as facebook subscription requires verification using application public URL. So i want to do subscription when my server gets started completey and not on application context initialization. Any idea how can i do it? Do i have nay application level event listener that can tell me that server has started completely?
Regards jia
You can annotate a bean method like so :
#PostConstruct
public void yrSubscribeLogic() {}
the app context can only be loaded after the server has fully started.

Resources