Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Effortless Cloud-Native App Development Using Skaffold

You're reading from   Effortless Cloud-Native App Development Using Skaffold Simplify the development and deployment of cloud-native Spring Boot applications on Kubernetes with Skaffold

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801077118
Length 272 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Ashish Choudhary Ashish Choudhary
Author Profile Icon Ashish Choudhary
Ashish Choudhary
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Section 1: The Kubernetes Nightmare – Skaffold to the Rescue
2. Chapter 1: Code, Build, Test, and Repeat – The Application Development Inner Loop FREE CHAPTER 3. Chapter 2: Developing Cloud-Native Applications with Kubernetes – A Developer's Nightmare 4. Chapter 3: Skaffold — Easy-Peasy Cloud-Native Kubernetes Application Development 5. Section 2: Getting Started with Skaffold
6. Chapter 4: Understanding Skaffold's Features and Architecture 7. Chapter 5: Installing Skaffold and Demystifying Its Pipeline Stages 8. Chapter 6: Working with Skaffold Container Image Builders and Deployers 9. Section 3: Building and Deploying Cloud-Native Spring Boot Applications with Skaffold
10. Chapter 7: Building and Deploying a Spring Boot Application with the Cloud Code Plugin 11. Chapter 8: Deploying a Spring Boot Application to the Google Kubernetes Engine Using Skaffold 12. Chapter 9: Creating a Production-Ready CI/CD Pipeline with Skaffold 13. Chapter 10: Exploring Skaffold Alternatives, Best Practices, and Pitfalls 14. Other Books You May Enjoy

Exploring the traditional application development inner loop

Before containers were cool, we were spoilt by the choices we have for the inner development loop. Your IDE can run builds for you in the background, and then you can deploy your application and test your changes locally. A typical traditional application development inner loop involves steps such as the following:

  1. A developer making code changes in an IDE
  2. Building and packaging the application
  3. Deploying and then running locally on a server
  4. Finally, testing the changes and repeating the step

    Here is a visualization of the traditional application development inner loop:

Figure 1.3 – Traditional application development inner loop

Figure 1.3 – Traditional application development inner loop

For Java developers, there are many options available to automate this process. Some of the most popular options are as follows:

  • Spring Boot Developer Tools
  • JRebel

Let's discuss these options briefly.

Spring Boot Developer Tools

Spring Boot first introduced developer tools in version 1.3. Spring Boot Developer Tools provide fast feedback loops and automatic restart of the application for any code changes done. It provides the following functionalities:

  • It provides a hot reloading feature. As soon as any file changes are done on classpath, it will automatically reboot the application. The automatic restart may differ based on your IDE. Please check the official documentation (https://docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/html/using-boot-devtools.html#using-boot-devtools-restart) for more details on this.
  • It provides integration with the LiveReload plugin (http://livereload.com) so that it can refresh the browser automatically whenever a resource is changed. Internally, Spring Boot will start an embedded LiveReload server, which will trigger a browser refresh whenever a resource is changed. The plugin is available for most popular browsers, such as Chrome, Firefox, and Safari.
  • It not only supports the local development process, but you can opt-in for updating and restarting your application running remotely on a server or cloud. You can enable remote debugging as well if you like. However, there is a security risk involved in using this feature in production.

The following is a short snippet of how to add relevant dependencies to your Maven and Gradle projects to add support for Spring Boot Developer Tools. Maven/Gradle should have an introduction section first:

Maven pom.xml

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
  </dependency>
</dependencies>

Gradle build.gradle

Here is the code for Gradle:

dependencies {
compileOnly("org.springframework.boot:spring-boot-devtools")
}

But this is not how we will add dependencies to test the auto-reload feature of developer tools. We will use the Spring Initializr website (https://start.spring.io/) to generate the project stub based on the options you choose. Here are the steps we'll follow:

  1. You can go ahead with default options or make your own choices. You can select the build tools (Maven or Gradle), language (Java, Kotlin, or Groovy), and Spring Boot version of your choice.
  2. After that, you can add necessary dependencies by clicking on the ADD DEPENDENCIES… button and selecting the dependencies required for your application.
  3. I have chosen the default options and added spring-boot-starter-web, spring-boot-dev-tools, and Thymeleaf as dependencies for my demo Hello World Spring Boot application.
  4. Now, go ahead and click on the GENERATE button to download the generated source code on your computer. Here is the screen you should see:
    Figure 1.4 – Spring Initializr home page

    Figure 1.4 – Spring Initializr home page

  5. After the download, you can import the project to your IDE.

The next logical step is to build a simple Hello World Spring Boot web application. Let's begin.

Anatomy of the Spring Boot web application

The best way to understand the working parts of the Spring Boot application is by taking a look at an example. In this example, we will create a simple Spring Web MVC application that will accept HTTP GET requests at http://localhost:8080/hello. We will get an HTML web page with "Hello, John!" in the HTML body in response. We will allow the user to customize the default response by entering the query string in the http://localhost:8080/hello?name=Jack URL so that we can change the default message. Let's begin:

  1. First, let's create a HelloController bean using the @Controller annotation for handling incoming HTTP requests. The @GetMapping annotation binds the HTTP GET request to the hello() method:
    @Controller
    public class HelloController {
       @GetMapping("/hello")
       public String hello(@RequestParam(defaultValue =     "John", name = "name", required = false) String name,     Model model) {
          model.addAttribute("name", name);
          return "index";
    }
    }

    This controller returns the name of the view, which is index in our case. The view technology we have used here is Thymeleaf, which is responsible for server-side rendering of the HTML content.

  2. In the source code template, index.html is available under the templates folder in src/main/resources/. Here are the contents of the file:
    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
          <meta charset="UTF-8"/>
          <title>Welcome</title>
    </head>
    <body>
    <p th:text="'Hello, ' + ${name} + '!'" />
    </body>
    </html>
  3. Spring Boot provides an opinionated setup for your application, which includes a main class as well:
    @SpringBootApplication
    public class Application {
       public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
       }
    }
  4. We will run our application using mvn spring-boot:run maven goal, which is provided by spring-boot-maven-plugin:
    Figure 1.5 – Output of the application

    Figure 1.5 – Spring Boot application startup logs

    Note

    To reduce the verbosity of the logs, we have trimmed them down to show only the parts that are relevant to our discussion.

    If you observe the logs carefully, we have developer tools support enabled, an embedded Tomcat server listening at port 8080, and an embedded LiveReload server running on port 35279. So far, this looks good. Once the application is started, you can access the http://localhost:8080/hello URL.

    Figure 1.6 – REST endpoint response

    Figure 1.6 – REST endpoint response

  5. Now we will do a small code change in the Java file and save it and you can see from the logs that the embedded Tomcat server was restarted. In the logs, you can also see that the thread that has spawned the application is not a main thread instead of a restartedMain thread:
    2021-02-12 16:28:54.500   INFO 53622 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]          : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-02-12 16:28:54.500   INFO 53622 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet            : Initializing Servlet 'dispatcherServlet'
    2021-02-12 16:28:54.501   INFO 53622 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet            : Completed initialization in 1 ms
    2021-02-12 16:29:48.762   INFO 53622 --- [          Thread-5] o.s.s.concurrent.ThreadPoolTaskExecutor   : Shutting down ExecutorService 'applicationTaskExecutor'
    2021-02-12 16:29:49.291   INFO 53622 --- [   restartedMain] c.e.helloworld.HelloWorldApplication       : Started HelloWorldApplication in 0.483 seconds (JVM running for 66.027)
    2021-02-12 16:29:49.298   INFO 53622 --- [   restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
    2021-02-12 16:29:49.318   INFO 53622 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]          : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-02-12 16:29:49.319   INFO 53622 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet            : Initializing Servlet 'dispatcherServlet'
    2021-02-12 16:29:49.320   INFO 53622 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet            : Completed initialization in 1 ms

This completes the demo of the auto-restart feature of the Spring Boot Developer Tools. We have not covered the LiveReload feature for brevity as it would be difficult to explain here because it all happens in real time.

JRebel

JRebel (https://www.jrebel.com/products/jrebel) is another option for Java developers for accelerating their inner loop development process. It is a JVM plugin, and it helps in reducing time for local development steps such as building and deploying. It is a paid tool developed by a company named Perforce. However, there is a free trial for 10 days if you would like to play with it. It provides the following functionalities:

  • It allows developers to skip rebuild and redeploys and see live updates of their changes by just refreshing the browser.
  • It will enable developers to be more productive while maintaining the state of their application.
  • It provides an instant feedback loop, which allows you to test and fix your issues early in your development.
  • It has good integration with popular frameworks, application servers, build tools, and IDEs.

There are many different ways to enable support for JRebel to your development process. We will consider the possibility of using it with an IDE such as Eclipse or IntelliJ. For both IDEs, you can install the plugin, and that's it. As I said earlier, this is a paid option, and you can only use it for free for 10 days.

For IntelliJ IDEA, you can install the plugin from the marketplace.

Figure 1.7 – IntelliJ IDEA installing JRebel

Figure 1.7 – IntelliJ IDEA installing JRebel

For the Eclipse IDE, you can install the plugin from Eclipse Marketplace.

Figure 1.8 – Eclipse IDE installing JRebel

Figure 1.8 – Eclipse IDE installing JRebel

Since JRebel is a paid option, we will not be exploring it in this book, but you are free to test it yourself.

We have covered the traditional application development inner loop life cycle and tools such as Spring Boot Developer Tools and JRebel, which allow rapid application development. Let's now go through the container-native application development inner loop life cycle.

You have been reading a chapter from
Effortless Cloud-Native App Development Using Skaffold
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781801077118
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image