<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Murat Artim on Medium]]></title>
        <description><![CDATA[Stories by Murat Artim on Medium]]></description>
        <link>https://medium.com/@muratartim_5366?source=rss-cb40de5ff6d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*telDvOGdSvDjp5J-FlBxjw.jpeg</url>
            <title>Stories by Murat Artim on Medium</title>
            <link>https://medium.com/@muratartim_5366?source=rss-cb40de5ff6d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 07:06:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@muratartim_5366/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploying Spring Boot Applications]]></title>
            <link>https://medium.com/swlh/deploying-spring-boot-applications-15e14db25ff0?source=rss-cb40de5ff6d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/15e14db25ff0</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Murat Artim]]></dc:creator>
            <pubDate>Wed, 29 May 2019 15:47:56 GMT</pubDate>
            <atom:updated>2019-05-30T14:05:14.342Z</atom:updated>
            <content:encoded><![CDATA[<p>Spring Boot applications can be deployed into production systems with various methods. In this article, we will go through step by step deployment of Spring Boot applications via the following 5 methods:</p><ul><li>Deploying in Java Archive (JAR) as a standalone application,</li><li>Deploying as Web Application Archive (WAR) into a servlet container,</li><li>Deploying in Docker Container,</li><li>Deploying behind NGINX web server — direct setup,</li><li>Deploying behind NGINX web server — containerized setup.</li></ul><h4>Deploying in Java Archive (JAR) as a standalone application</h4><p>Spring Boot applications can easily be packaged into JAR files and deployed as standalone applications. This is done by the <em>spring-boot-maven-plugin</em>. The plugin is automatically added to pom.xml once the Spring project is created via <a href="https://start.spring.io/">Spring Initializr</a> as a Maven project.</p><pre>&lt;build&gt;<br>  &lt;plugins&gt;<br>    &lt;plugin&gt;<br>      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;<br>    &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br>&lt;/build&gt;</pre><p>In order to package the application in a single (fat) jar file, run the maven command mvn package under project directory. This will package the application inside an executable jar file with all its dependencies (including the embedded servlet container — if its a web application). To run the jar file, use the following standard JVM command java -jar &lt;jar-file-name&gt;.jar.</p><h4>Deploying as Web Application Archive (WAR) into a servlet container</h4><p>Spring Boot applications can be packaged into WAR files to be deployed into existing servlet containers (such as Tomcat, Jetty etc.). This can be done as follows:</p><p>Specify WAR packaging in pom.xml file via &lt;packaging&gt;war&lt;/packaging&gt;. This will package the application into a WAR file (instead of JAR). On the second step, set the scope of Tomcat (servlet container) dependency to provided (so that it is not deployed into WAR file):</p><pre>&lt;dependency&gt;<br>  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>  &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId<br>  &lt;scope&gt;provided&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p>Initialise the Servlet context required by Tomcat by extending <em>SpringBootServletInitializer</em> and overriding <em>configure</em> method as follows:</p><pre>@SpringBootApplication<br>public class DemoApp extends SpringBootServletInitializer {</pre><pre>  @Override<br>  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {<br>    return builder.sources(DemoApp.class);<br>  }</pre><pre>  public static void main(String[] args) {<br>    SpringApplication.run(DemoApp.class, args);<br>  }<br>}</pre><p>In order to package the application in a war file, run the standard maven command mvn clean package under project directory. This will generate the WAR package which can be deployed into a servlet container. To run the application inside an existing Tomcat container, copy the generated WAR file to tomcat/webapps/ directory.</p><h4>Deploying in Docker Container</h4><p>Before deploying the application into a Docker container, we will first package the application in a (fat) JAR file. This process is previously explained, therefore I will assume we have a jar file.</p><p>On the first step, we need to build a container image. For this, we start with creating a <em>Dockerfile</em> in the project root directory as follows:</p><pre># latest oracle openjdk is the basis<br>FROM openjdk:oracle</pre><pre># copy jar file into container image under app directory<br>COPY target/demoApp.jar app/demoApp.jar</pre><pre># expose server port accept connections<br>EXPOSE 8080</pre><pre># start application<br>CMD [&quot;java&quot;, &quot;-jar&quot;, &quot;app/demoApp.jar&quot;]</pre><p>Note that, in the above snippet, we assumed that the application JAR file ‘<em>demoApp.jar’</em> is located under the target directory of our project. We also assumed that the embedded servlet port is 8080 (which is the default case for Tomcat).</p><p>We can now build the Docker image with the following command (from where the Dockerfile is located):</p><pre>docker image build -t demo-app:latest .</pre><p>where -t is the name and tag of the image to be built. Once the image is built, we can create and run the container via:</p><pre>docker container run -p 8080:8080 -d --name app-container demo-app</pre><p>where -p publishes (maps) host port to container port (in this case both are 8080). The option -d (detach) runs the container in background, and --name specifies the name of the container.</p><h4>Deploying behind NGINX web server — direct setup</h4><p>Configuring servlet containers (such as Tomcat or Jetty) for real production (i.e. running on port 80, without root user and with SSL) may not be straight forward (but doable). Therefore, it is recommended to use a web server (such as Nginx) in front of your Spring Boot applications. This can be done in two ways; direct setup or containerized setup. In this section, we will demonstrate the direct setup.</p><p>In direct setup, we run the Nginx web server and the Spring Boot applications directly on localhost (on different ports of course). And we let Ngnix proxy REST requests to Spring Boot applications. For this:</p><ol><li>Install Nginx web server on Linux via sudo apt-get install nginx,</li><li>Open the file /etc/ngnix/sites-available/default with a text editor,</li><li>Say, we have two Spring Boot applications to be proxied. Then replace the <em>‘location’</em> block in the file with following blocks for two Spring Boot applications. Note that, all Nginx-Java configs can be found <a href="https://www.nginx.com/resources/wiki/start/topics/examples/javaservers/">here</a>.</li></ol><pre>location /app1 {<br>  proxy_pass http://localhost:8080;<br>}</pre><pre>location /app2 {<br>  proxy_pass http://localhost:9000;<br>}</pre><p>based on which the requests coming to http://localhost/app1/ will be directed to /http://localhost:8080/, and requests coming to http://localhost/app2/ will be directed to /http://localhost:9000/.</p><h4>Load balancing</h4><p>If you are running multiple instances of a Spring Boot application, you can enable Nginx to apply <strong>load-balancing</strong>. For example, if we have 3 instances of app1 running on ports 8080, 8081 and 8082. We can load-balance among these servers as follows:</p><p>Open the file /etc/ngnix/sites-available/default and add the following block at the top of the file (before server block):</p><pre># configure load-balancing<br>upstream backend {<br>  server localhost:8080;<br>  server localhost:8081;<br>  server localhost:8082;<br>}</pre><p>Modify the <em>proxy_pass</em> parameter of app1 as follows:</p><pre>location /app1 {<br>  proxy_pass http://backend;<br>}</pre><p>based on which the requests coming to http://localhost/app1/ will be directed to one of/http://localhost:8080/, /http://localhost:8081/ or /http://localhost:8082/.</p><h4>Deploying behind NGINX web server — containerized setup</h4><p>In containerized setup, we deploy the Nginx web server and all the Spring Boot applications on separate Docker containers. And we let Nginx (running in its own container) proxy REST requests to Spring Boot application containers.</p><p>We start by packaging all Spring Boot applications in (fat) jar files (which is previously explained). At this point, pay attention to setting individual server ports and root context paths to each Spring Boot application by adding following lines to <em>application.properties</em> (or <em>application.yml</em>) files:</p><pre>server.port=8082<br>server.servlet.context-path=/search-service</pre><p>Then we deploy the generated jar packages in separate Docker containers (which is also previously explained).</p><p>As an example, we deploy four Spring Boot applications; single instance of ‘analysis-service’ application and three instances of ‘search-service’ application. The three instances of search-service application will be load-balanced by Nginx. Our basic architecture looks like follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jUMOMnBN3ha_FWaWKrXXag.png" /></figure><p>We create a Nginx configuration file <strong>nginx.conf</strong> based on the default configurations. We add load-balancing and proxy information for each service as follows:</p><pre>http {<br><br>  upstream backend {<br>    server search-service-1:8080;<br>    server search-service-2:8081;<br>    server search-service-3:8082;<br>  }</pre><pre>  server {</pre><pre>    listen 80 default_server;<br>    listen [::]:80 default_server;</pre><pre>    root /var/www/html;</pre><pre>    server_name _;</pre><pre>    location /search-service {<br>      proxy_pass http://backend/search-service;<br>    }</pre><pre>    location /analysis-service {<br>      proxy_pass http://analysis-service:8083/analysis-service;<br>    }<br>  }<br>}<br>events { worker_connections 1024; }</pre><p>based on which the requests coming to http://localhost/search-service/ will be directed to one of/http://search-service-1:8080/search-service/, /http://search-service-2:8081/search-service/ and /http://search-service-3:8082/search-service/, and requests coming to http://localhost/analysis-service/ will be directed to /http://analysis-service:8083/analysis-service/.</p><p>After the configuration file (nginx.conf) is created, we will deploy the Nginx web server in a Docker container. For this, we create a <em>Dockerfile</em> as follows:</p><pre># latest nginx<br>FROM nginx</pre><pre># copy custom configuration file<br>COPY nginx.conf /etc/nginx/nginx.conf</pre><pre># expose server port<br>EXPOSE 80</pre><pre># start server<br>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</pre><p>And we build a Docker image for Nginx web server as follows:</p><pre>docker image build -t custom-nginx:latest .</pre><p>Once all Docker images are built, all system can be deployed by running docker-compose up command on the following <em>docker-compose.yml</em> file:</p><pre><strong>version: </strong>&#39;3.7&#39;</pre><pre><strong>services:<br>  nginx_server:<br>    image: </strong>custom-nginx<br>    <strong>ports:<br>      </strong>- &#39;80:80&#39;<br>    <strong>networks:<br>      </strong>- demo-network<br>    <strong>depends_on:<br>      </strong>- &quot;search-service-1&quot;<br>      - &quot;search-service-2&quot;<br>      - &quot;search-service-3&quot;<br>      - &quot;analysis-service&quot;</pre><pre><strong>search-service-1:<br>  image: </strong>search-service-1<br>  <strong>ports:<br>    </strong>- &#39;8080:8080&#39;<br>  <strong>networks:<br>    </strong>- demo-network</pre><pre><strong>search-service-2:<br>  image: </strong>search-service-2<br>  <strong>ports:<br>    </strong>- &#39;8081:8081&#39;<br>  <strong>networks:<br>    </strong>- demo-network</pre><pre><strong>search-service-3:<br>  image: </strong>search-service-3<br>  <strong>ports:<br>    </strong>- &#39;8082:8082&#39;<br>  <strong>networks:<br>    </strong>- demo-network</pre><pre><strong>analysis-service:<br>  image: </strong>analysis-service<br>  <strong>ports:<br>    </strong>- &#39;8083:8083&#39;<br>  <strong>networks:<br>    </strong>- demo-network</pre><pre><strong>networks:<br>  demo-network:<br>    name: </strong>demo-network</pre><p>With this, we demonstrated five ways of deploying Spring Boot applications. The choice of deployment is based on the overall solution architecture, requirements of target platform such as security and available resources.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15e14db25ff0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/deploying-spring-boot-applications-15e14db25ff0">Deploying Spring Boot Applications</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>