starting a Spring Boot application - java

I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat and package as an executable JAR file.
I started my application on Ubuntu 14.04 LTSwith
java -DAPPKEY=oracle -Dspring.profiles.active=oracle -jar licence-0.0.1-SNAPSHOT.jar
But after some hours the application was down, the process was not running anymore. Was this a proper way to start the application ?
I have no clue why the process shutdown

if you need to run your application as a service take a look at the installation guide in the spring boot doc:
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
As you can see if you are using init.d services you can link the jar inside the init.d directory.
The fat jar contains the init scripts:
sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

Related

Deploy a JAR file to a VPS server (Ubuntu Server 16.04)

I need to deploy my spring boot rest api to my VPS server, to do so I installed JAVA and MAVEN. I ran it using sudo java -jar myfile.jar it is working but the problem is that after sometimes it stops and my webservice send me error 503.
I would like to know how to deploy it and run without it stopping.
PS : There is no page ".html" to be accessed that's the reason I want to deploy as JAR

How can I deploy a java web app to heroku?

I have a Java web app with Tomcat embedded in my jar file. I can containerize the app with Docker and run it with command java -jar -myapp.jar, but I can't run that container in Heroku. I tried using heroku CLI to dockerize and deploy, but Heroku gives me an error of "can't access jarfile".
I tried to fix this by using heroku deploy:jar to deploy my fat jar, but this erroneously gives me an error:
heroku deploy:jar target/*.jar -a medscanner2
-----> Packaging application...
- app: medscanner2
- including: target/medscanner2.jar
! ERROR: Could not get API key! Please install the Heroku CLI and run
`heroku login` or set the HEROKU_API_KEY environment variable.
! Re-run with HEROKU_DEBUG=1 for more info.
!There was a problem deploying to medscanner2.
!Make sure you have permission to deploy by running: heroku apps:info -a
medscanner2
I am signed into Heroku and I can use heroku auth:whoami to verify that, I can push containers and deploy them, so this error doesn't make any sense. I reran with HEROKU_DEBUG=1 and it did not return any more info.
I further tried to set the HEROKU_API_KEY variable in the CLI with a token I got from Heroku and this still caused the same error when I try to deploy the jar.
I am using a Procfile (although I am not sure it is necessary):
web: java -Dserver.port=$PORT -jar target/medscanner2.jar
Since the issue seems to be indicating there is an issue with access I don't see how the Procfile could be influencing it.
What is the best way for me to deploy a Java web app that does not using Spring Boot to Heroku? I have separately deployed the docker container successfully to Google app engine, so all this work for Heroku is very frustrating.
I ended up fixing this by using webapp-runner to deploy my app. It runs the webapp-runner jar which can run your .war files. This required adding the heroku-maven-plugin and maven-dependency-plugin.
I could then add a Procfile: web: java -jar target/dependency/webapp-runner.jar target/*.war --port $PORT
and use the Heroku CLI to add the app using git. The link with webapp-runner is a guide to deploying tomcat java apps with webapp-runner.

How do I remove jar filename before endpoint's url?

When I compile my maven-based project I have the resulting jar file in 'target/my-application'. When I run locally my application (I use Payara Micro if this is important) I have the following in log:
Payara Micro URLs
http://localhost:8080/my-application-1.0-SNAPSHOT
How do I get rid of my-application-1.0-SNAPSHOT from URL address?
As per request, I'd say that:
What type of application is that? EAR/WAR?
war application (however, maven creates for me "payara-uber-jar"
and I run the application via java -jar filename.jar
What application server is it running on?
Payara micro
Are you using maven?
Yes, I am.
Hello according to Paraya documentation what you need to do is:
java -jar payara-micro.jar --deploy NAMETHATYOUWANT.war
And then the context path will be /NAMETHATYOUWANT
https://www.davidsalter.co.uk/payara-micro-context-path/

spring.cloud.inetutils.ignoredInterfaces ignored in profiles

I'm running a Spring Boot application and using the Netflix OSS Spring Cloud framework. We are running a Eureka instance and have a service that is trying to register. When our service registers to Eureka it uses IP of the wrong port name. To fix this we have added:
spring.cloud.inetutils.ignoredInterfaces=eth0
This works great when we pass this from the command line, but when we move this into a profile configuration it doesn't work but all other configuration of the profile is picked up.
So for example this will work:
java -jar service.jar --spring.cloud.inetutils.ignoredInterfaces=eth0
and this will NOT work:
java -jar service.jar --spring.profiles.active=localvm
where application-localvm.properites contains:
spring.cloud.inetutils.ignoredInterfaces=eth0
Look you have to add -D argument before the main class or jar archive.
So try this:
java -jar -Dspring.profiles.active=localvm service.jar
For more details check this doc about how to set the active Spring profiles.

How do I run a spring boot executable jar in a Production environment?

Spring boot's preferred deployment method is via a executable jar file which contains tomcat inside.
It is started with a simple java -jar myapp.jar.
Now, I want to deploy that jar to my linux server on EC2, am I missing something or do I really need to create a init script to properly start the application as a daemon?
If I simply call java -jar the application dies when I log out.
I could start it in screen or nohup but that is not very elegant and a restart in my server would force me to log in and start the process manually.
So, is there something already for the task in spring boot?
Please note that since Spring Boot 1.3.0.M1, you are able to build fully executable jars using Maven and Gradle.
For Maven, just include the following in your pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
For Gradle add the following snippet to your build.gradle:
springBoot {
executable = true
}
The fully executable jar contains an extra script at the front of the file, which allows you to just symlink your Spring Boot jar to init.d or use a systemd script.
init.d example:
$ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
This allows you to start, stop and restart your application like:
$/etc/init.d/yourapp start|stop|restart
Or use a systemd script:
[Unit]
Description=yourapp
After=syslog.target
[Service]
ExecStart=/var/yourapp/yourapp.jar
User=yourapp
WorkingDirectory=/var/yourapp
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
More information at the following links:
Installation as an init.d service
Installation as a systemd service
By far the most easiest and reliable way to run Spring Boot applications in production is with Docker. Use Docker Compose, Docker Swarm or Kubernetes if you need to use multiple connected services.
Here's a simple Dockerfile from the official Spring Boot Docker guide to get you started:
FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
An even better approach for building Docker images is to use Jib, an open-source Java tool maintained by Google for building Docker images of Java applications. Jib does not need a Dockerfile, you just invoke it with Maven (official quickstart here) or Gradle (official quickstart here).
Here's a sample command line to run the container as a daemon:
docker run \
-d --restart=always \
-e "SPRING_PROFILES_ACTIVE=prod" \
-p 8080:8080 \
prefix/imagename
My Spring boot application has two initializers. One for development and another for production. For development, I use the main method like this:
#SpringBootApplication
public class MyAppInitializer {
public static void main(String[] args) {
SpringApplication.run(MyAppInitializer .class, args);
}
}
My Initializer for production environment extends the SpringBootServletInitializer and looks like this:
#SpringBootApplication
public class MyAppInitializerServlet extends SpringBootServletInitializer{
private static final Logger log = Logger
.getLogger(SpringBootServletInitializer.class);
#Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
log.trace("Initializing the application");
return builder.sources(MyAppInitializerServlet .class);
}
}
I use gradle and my build.gradle file applies 'WAR' plugin. When I run it in the development environment, I use bootrun task. Where as when I want to deploy it to production, I use assemble task to generate the WAR and deploy.
I can run like a normal spring application in production without discounting the advantages provided by the inbuilt tomcat while developing. Hope this helps.
On Windows OS without Service.
start.bat
#ECHO OFF
call run.bat start
stop.bat:
#ECHO OFF
call run.bat stop
run.bat
#ECHO OFF
IF "%1"=="start" (
ECHO start myapp
start "myapp" java -jar -Dspring.profiles.active=staging myapp.jar
) ELSE IF "%1"=="stop" (
ECHO stop myapp
TASKKILL /FI "WINDOWTITLE eq myapp"
) ELSE (
ECHO please, use "run.bat start" or "run.bat stop"
)
pause
In a production environment you want your app to be started again on a machine restart etc, creating a /etc/init.d/ script and linking to the appropriate runlevel to start and stop it is the correct approach. Spring Boot will not extend to covering this as it is a operating system specific setup and the are tonnes of other options, do you want it running in a chroot jail, does it need to stop / start before some other software etc.
You can use the application called Supervisor.
In supervisor config you can define multiple services and ways to execute the same.
For Java and Spring boot applications the command would be
java -jar springbootapp.jar.
Options can be provided to keep the application running always.So if the EC2 restart then Supervisor will restart you application
I found Supervisor easy to use compared to putting startup scripts in /etc/init.d/.The startup scripts would hang or go into waiting state in case of errors .
If you are using gradle you can just add this to your build.gradle
springBoot {
executable = true
}
You can then run your application by typing ./your-app.jar
Also, you can find a complete guide here to set up your app as a service
56.1.1 Installation as an init.d service (System V)
http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
cheers
I start applications that I want to run persistently or at least semi-permanently via screen -dmS NAME /path/to/script. As far as I am informed this is the most elegant solution.
This is a simple, you can use spring boot maven plugin to finish your code deploy.
the plugin config like:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debug.port}
</jvmArguments>
<profiles>
<profile>test</profile>
</profiles>
<executable>true</executable>
</configuration>
</plugin>
And, the jvmArtuments is add for you jvm. profiles will choose a profile to start your app. executable can make your app driectly run.
and if you add mvnw to your project, or you have a maven enveriment. You can just call./mvnw spring-boot:run for mvnw or mvn spring-boot:run for maven.

Categories

Resources