Springboot standalone application deploy in production - java

I am trying to deploy my springboot standalone application in production in linux server.I am trying to use the out of the box tomcat server.
'java -jar jarname.jar'
This works fine , but if I close my putty session the application is unistalled.
Now I used
'nohup java -jar jarname.jar &'
this works good and my application is not shutdown even if my putty session is closed.But the logs will not roll correctly(I have configured log4j to create a new log file for each day) in this case. So was wondering if this is the right way to do this.
I have searched several documentation but was not able to find a correct solution for this problem.
Please help.
Thanks

Well I think it's better to use Linux services for running application, you can read here for example
And if you want to collect logs - better write them to the file.
Spring provides build-in me mechanics to do that
logging:
level:
root: INFO
file:
clean-history-on-start: false
max-history: 7
max-size: 10MB
name: some-name
path: /path/log/dir
total-size-cap: 0B

Why not package it into a Docker image and run that on the server?
Here are a few ideas:
https://medium.com/swlh/deploying-spring-boot-applications-15e14db25ff0

You can run your spring boot application as jar, but you need to create a service so you can execute your spring boot as daemon.
https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu
With this, you can start or stop your application like
$ sudo service myspringbootapp stop
$ sudo service myspringbootapp start

Related

How to make sure that environment variable placeholders are substituted in a Spring Boot application running in Apache Tomcat?

I have a Spring Boot application, which runs in an Apache Tomcat server. In application.yaml I have, among others, following entries:
mail:
pop3Host: ${MAIL_HOSTNAME}
inboxFolder: ${MAIL_INBOX}
hostName: ${MAIL_HOSTNAME}
port: ${MAIL_PORT}
userName: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}
The application is deployed to Tomcat from within IntelliJ Idea so I can debug it.
I start Tomcat using the following command:
export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start
However, after I
start Tomcat using the above script,
deploy the Spring Boot application from IntelliJ Idea, and
make sure that the code where those values are used is executed,
I get the exception indicating that the placeholders have not been substituted.
How can I fix it, i. e. make sure that I can specify some information (like user name and password) in application.yaml via environment variables (so that I don't include the actual credentials in application.yaml)?
export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start
Add export MAIL_HOSTNAME= etc. to the above lines, or create a setenv.sh file with such lines (in the same directory as catalina.sh file).
Using setenv.sh is documented in RUNNING.txt file of Apache Tomcat.
It is not possible to pass JVM arguments to a process running in a remote machine from an IDE. The Spring Boot params will be loaded from the JVM params when the process is started in the remote machine. I am sorry to say that it won't work.
An alternate solution i suggest is, use spring config server to create a separate profile for remote debugging config ( like we will have for Dev, QA environments etc ). When you try to debug the application, please restart the remote application using jenkins job ( I am assuming you don't have remote acccess to the box where the app is running) by passing the profile name in the jenkins job so that the values you wanted will be picked up. Please let me know if you need more details.

How to deploy Spring Boot application in Google Cloud Compute Engine?

I am a new to Google Cloud platform. I want to deploy my Spring Boot project in Compute Engine because deploying in App Engine costs more than the Compute Engine. There are a lot of videos/articles are available in YouTube/Websites for deploying in App Engine but I did not find any tutorial on deploying Spring Boot app in Compute Engine.
Here is the very good blog written on How to deploy Spring Boot app in Google Cloud Compute Engine with embedded tomcat?. I am just briefing here.
If you want to use an embedded tomcat server then PM2 is the best tool for managing the deployment in Compute Engine. PM2 is a process manager for the JavaScript runtime Node.js. Actually without PM2 also you can deploy Spring Boot app directly by executing command mvn spring--boot:run but the problem is when you exit the terminal then your server will also go down. Here you can use excute the command in background by using setsid mvn spring-boot:run. This will execute your Spring Boot app in background but when you need to restart the server then you will the error like Web server failed to start. Port 8080 was already in use
Hence you need to kill the existing running app by finding what is the processId. This is somehow a headache. Hence I will also recommend you to use PM2. To install PM2 you can use these commands in ubuntu.
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
sudo npm install -g pm2
If you want test your Spring Boot application, you can clone or pull the project from your Github repository or you can clone the spring-boot-test project from here.
git clone https://github.com/altafjava/spring-boot-test.git
cd spring-boot-test/
You will have to create one .sh file which will be used by the PM2 to restart the server. In your .sh file you need to write the command which is used to execute the spring boot project that is nothing but mvn spring-boot:run
echo "mvn spring-boot:run">server.sh
chmod +x server.sh
Finally, restart the server by using the command pm2 restart server. You can even check the logs on runtime by using pm2 logs.

How can I restart a Java application when VM (virtual machine) is restart/start?

I am using java 1.8 and spring-boot-starter-parent 1.5.6.RELEASE and swagger2 2.6.1. We run the application in the VM using the following command,
nohup java -jar myApplication.jar &
My application is getting stopped when the VM is stopped. So manually I need to start my application. I don't want to do that manually. I need to restart my application programmatically or any script will do this that is also fine for me.
How do I restart my application when VM is started??
Kindly provide your inputs.
In a Linux distribution which uses systemd (official docs here) (such as Debian, Ubuntu, or Fedora), creating a service is simple:
We'll need to create a service file which tells systemd how to start your application. Create a file in /etc/systemd/system named something like myApplication.service containing these lines:
[Unit]
Description="A description of what my application does"
[Service]
ExecStart=/path/to/java -jar /path/to/your/myApplication.jar
[Install]
WantedBy=multi-user.target
(Derived from an example in the docs.)
Then run systemctl enable myApplication.service as root to enable it, which will make it run on boot.
There's lots more you can learn; the docs for systemd are quite good. You might take a look at the blog story which introduced systemd as well, as that provides something of a "sales pitch" for what it can do.

Spring Boot init.d not Not running (process not found)

I was trying to follow the instructions from here, where trying to run the Spring Boot app as init.d service but could not successfully.
I created the fully executable jar (myapp.jar) as mentioned and also created the symlink to /etc/init.d/myapp When I run the java -jar myapp.jar I could see the application start up successfully.
But when I try to use
service myapp status it says Not running (process not found)
service myapp start it says Failed to start
the documentation says "Assuming that you have a Spring Boot application installed in /var/myapp" I don't understand this point quite well. I copied the executable jar (via Jenkins) to /var/myapp. so it contains only one file which is jar. does this create the problem?
Any suggestions are appreciated.
Environment:
springBootVersion = '1.3.2.RELEASE'
JDK6 (yes it is)
Ubuntu 12.04
Sometimes, you may need to run: sudo systemctl daemon-reload for your new service to be loaded.
You must register your jar as service. Look at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#deployment-initd-service
In ubuntu you need reload deamon with sudo systemctl daemon-reload

CLI Java program as a service

I am planning to make a CLI Java program/server that will run as a service on Raspbian and it will start at boot...
I would like to make it as MySQL service for example...
MySQL I can start by command:
service mysqld start
or stop it by:
service mysqld stop
I found Apache Commons Daemon lib which can help me with that...
But what I need there in addition is that I can work with the instance, created by the service, in (different) command line, same as with MySQL for example:
mysql --version
mysql --help
etc
So any ideas how to make it? You can post some useful articles... I came across some articles but I didn't find how can I access the instance in command line...
Everywhere is just how to start a service...
Answer 1
You can do this by having your java application start/stop by a script Ex: shell or perl
In the script file you can build the command to start/stop your service [app]
Say your application can be started using java -cp "." com.test.Main
In startService.sh
...
// build classpath, specify heap, perm gen space, encoding
...
java <classpath> <vm_args> <memory> com.test.Main
...
Answer 2
There is a way in spring-boot to configure telnet/ssh, also to write own commands to interact with it.
need to add the artifact spring-boot-starter-remote-shell, authentication also possible
This doc reference might help SSH or Telent Spring Boot

Categories