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

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.

Related

Springboot standalone application deploy in production

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

How to start grail application in production mode without terminate session when close command windows?

I have a grails 2.4.3 application run on centos 7. My problem is that I can't start the application via terminal, because when I close terminal, the session is terminated.
My command to start program is:
export PATH=$PATH:/opt/setup/grails-2.4.4/bin
export JAVA_HOME=/usr/java/jdk1.7.0_80/
grails prod run-app
I have already try many ways:
i.e: nohup grails prod run-app (nothing change after running command),
setsid prod run-app (program terminated by accidentally after a time running).
Both the ways are not worked.
Please help me find a way to start Grails application in production precisely.
Thanks.
Do not use grails run-app in production. From the Grails 2.4.4 documentation
NEVER deploy Grails using the run-app command as this command sets Grails up for auto-reloading at runtime which has a severe performance and scalability implications
In Grails 2.x (for production), you need to deploy your application to a supported Java EE Container (Tomcat, Jetty, etc), which are listed on the same page in the documentation.
In Grails 3.x you can package your web app as a jar and run it like any other jar because it is built on top of Spring Boot and the container was packaged into the jar.
On linux, you can use the nohup (no hang-up) command to make sure that the command will not be killed when you close the terminal.
Create a sh (startup.sh) file containing your startup statements:
echo "export PATH=$PATH:/opt/setup/grails-2.4.4/bin
export JAVA_HOME=/usr/java/jdk1.7.0_80/
grails prod run-app" > startup.sh
make sure the script is executable
chmod +x startup.sh
then
nohup ./startup.sh

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

Typesafe Activator on Heroku Error Binding to PORT

I just updated our old Play 2.1 app to the new Typesafe Activator with Play 2.3.8. I noticed that the app now appears to have an activator launcher in the root of the project. I'm trying to deploy the app on Heroku and get this error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
My Procfile looks like:
web: ./activator "-Dhttp.port=${PORT} ${JAVA_OPTS} -Dconfig.file=${CONFIG_RESOURCE}" run
The app is very very small and I'm nearly certain there should be any reason it would take longer than 60 seconds to boot and bind to the port. Locally it binds immediately with that exact same command.
Any ideas what could cause the issue?
I would recommend not using activator to run your app in production.
Heroku runs the sbt stage command against your application, which creates a target/universal/stage/bin/<app-name> script that can be used to launch your app. This is a better way of running in production because it eliminates any overhead or potential quirks do to activator and/or sbt.
Your Procfile should probably look like this:
web: target/universal/stage/bin/<app-name> -Dhttp.port=${PORT} -Dconfig.file=${CONFIG_RESOURCE}
Of course, replace <app-name> with the name of your app in build.sbt. the JAVA_OPTS will be picked up automatically.

Heroku + Java + WAR deployment + New Relic

Heroku don't allow to upload files, just git repository(we can't share sources) and WAR files. We using WAR.
So, how i can use(my own newrelic.yml and newrelic-agent.jar) new relic with war deployments on heroku?
update.
Ok, i add newrelic add-on to application, and now can set some environment variables:
heroku config:set NEW_RELIC_APP_NAME="name"
heroku config:set NEW_RELIC_LICENSE_KEY="key"
But it's very basic and not working :). How to pass own config file to add-on?
update.
Find similar question: Heroku : Using NewRelic with heroku deploy:war approach
The simplest way to get application monitoring working with New Relic is by using the add-on as described here: https://addons.heroku.com/newrelic
Something like this should work:
heroku addons:add newrelic:stark
Heroku has some documentation about how to monitor Java applications using New Relic here: https://devcenter.heroku.com/articles/newrelic#java-configuration
There are also some other similar Stack Overflow questions about Heroku, New Relic and Java. For example:
adding newrelic addon heroku java
You can upload a .war file to Heroku using either the Heroku toolbelt:
heroku plugins:install https://github.com/heroku/heroku-deploy
heroku deploy:war --war <path_to_war_file> --app <app_name>
Or directly using curl:
curl -X POST -F war=#mywebapp.war -i https://:<API_KEY>#direct-to.herokuapp.com/direct/mywebapp/war

Categories