Search icon CANCEL
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
Spring 5.0 By Example

You're reading from   Spring 5.0 By Example Grasp the fundamentals of Spring 5.0 to build modern, robust, and scalable Java applications

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher Packt
ISBN-13 9781788624398
Length 356 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Claudio Eduardo de Oliveira Claudio Eduardo de Oliveira
Author Profile Icon Claudio Eduardo de Oliveira
Claudio Eduardo de Oliveira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Journey to the Spring World FREE CHAPTER 2. Starting in the Spring World – the CMS Application 3. Persistence with Spring Data and Reactive Fashion 4. Kotlin Basics and Spring Data Redis 5. Reactive Web Clients 6. Playing with Server-Sent Events 7. Airline Ticket System 8. Circuit Breakers and Security 9. Putting It All Together 10. Other Books You May Enjoy

Setting up our development environment

Before we start, we need to set up our development environment. Our development environment consists of the following four tools:

  • JDK
  • Build tool
  • IDE 
  • Docker

We will install JDK version 8.0. This version is fully supported in Spring Framework 5. We will present the steps to install Maven 3.3.9, the most famous build tool for Java development, and in the last part, we will show you some detailed instructions on how to install IntelliJ IDEA Community Edition. We will use Ubuntu 16.04, but you can use your favorite OS. The installation steps are easy.

Installing OpenJDK

OpenJDK is a stable, free, and open source Java development kit. This package will be required for everything related to code compilation and runtime environments. 

Also, it is possible to use an Oracle JDK, but you should pay attention to the License and Agreements. 

To install OpenJDK, we will open a terminal and run the following command:

sudo apt-get install openjdk-8-jdk -y
We can find more information on how to install Java 8 JDK in the installation section (http://openjdk.java.net/install/) of the OpenJDK page.

Check the installation using the following command:

java -version

You should see the OpenJDK version and its relevant details displayed as follows:

Now that we have installed the Java development kit, we are ready for the next step. In the real world, we must have a build tool to help developers to compile, package, and test the Java applications.

Let's install Maven in the next section.

Installing Maven

Maven is a popular build tool for Java development. Some important open source projects were built using this tool. There are features that facilitate the build process, standardize the project structure, and provide some guidelines for best practices development.

We will install Maven, but the installation step should be executed after the OpenJDK installation.

Open a terminal and execute the following:

sudo apt-get install maven -y

Check the installation using this command:

mvn -version

You should see the following output, although the version may be different for you:

Well done. Now we have Maven installed. Maven has a vibrant community that produces many plugins to help developers with important tasks. There are plugins to execute a unit test and plugins to prepare the project for the release event that can be integrated with SCM software.

We will use the spring boot maven plugin and docker maven plugin. The first converts our application to a JAR file and the second enables us to integrate with Docker Engine to create images, run containers, and much more. In the next few chapters, we will learn how to configure and interact with these plugins.

Installing IDE

The IDE is an important tool to help developers. In this book, we will use the IntelliJ IDEA as an official tool for developing our projects. There are no restrictions for other IDEs because the project will be developed using Maven as a build tool. 

The IDE is a personal choice for developers, and in general, it involves passion; what some people love, other developers hate. Please feel free to use your favorite.

IntelliJ IDEA

IntelliJ IDEA is a JetBrains product. We will use the Community Edition, which is open source and a fantastic tool with which to code Java and Kotlin. The tool offers a fantastic autocomplete feature, and also fully supports Java 8 features.

Go to https://www.jetbrains.com/idea/download/#section=linux and download the Community Edition. We can extract the tar.gz and execute it.

Spring Tools Suite

Installing Docker

Docker is an open source project that helps people to run and manage containers. For developers, Docker helps in different stages of the development lifecycle.

During the development phase, Docker enables developers to spin up different infrastructure services such as databases and service discoveries like Consul without installation in the current system operational. It helps the developers because developers do not need to install these kinds of systems in the operating system layer. Usually, this task can cause conflicts with the libraries during the installation process and consumes a lot of time.

Sometimes, developers need to install the exact version. In this case, it is necessary to reinstall the whole application on the expected version. It is not a good thing because the developer machine during this time becomes slow. The reason is quite simple, there are many applications that are used during software development.

Docker helps developers at this stage. It is quite simple to run a container with MongoDB. There is no installation and it enables developers to start the database with one line. Docker supports the image tag. This feature helps to work with different versions of the software; this is awesome for developers who need to change the software version every time.

Another advantage is that when the developers need to deliver the artifacts for test or production purposes, Docker enables these tasks via Docker images.

Docker helps people to adopt the DevOps culture and delivers amazing features to improve the performance of the whole process.

Let's install Docker.

The easiest way to install Docker is to download the script found at https://get.docker.com:

curl -fsSL get.docker.com -o get-docker.sh

After the download is completed, we will execute the script as follows:

sh get-docker.sh

Wait for the script execution and then check the Docker installation using the following command:

docker -v

The output needs to look like the following:

Sometimes, the version of Docker can be increased, and the version should be at least 17.10.0-ce.

Finally, we will add the current user to the Docker group, and this enables us to use the Docker command line without the sudo keyword. Type the following command:

sudo usermod -aG docker $USER

We need to log out to effect these changes. Confirm whether the command works as expected by typing the following. Make sure that the sudo keyword is not present:

docker ps

The output should be as follows:

Introducing Docker concepts

Now, we will introduce some Docker concepts. This book is not about Docker, but some basic instructions on how to use Docker are necessary to interact with our containers during the next few chapters. Docker is a de facto tool that is used to manage containers.

Docker images

The Docker image is like a template for a Docker container. It contains a set of folders and files that are necessary to start the Docker container. We will never have an image in execution mode. The image provides a template for Docker Engine to start up the container. We can create an analogy with object orientation to understand the process better. The image is like a class that provides an infrastructure to instantiate some objects, and instances are like a container.

Also, we have a Docker registry to store our images. These registries can be public or private. Some cloud vendors provide these private registries. The most famous is Docker Hub. It can be free, but if you choose this option, the image should be public. Of course, Docker Hub supports private images, but in this case, you have to pay for the service.

Containers

Docker containers are a lightweight virtualization. The term lightweight means that Docker uses the SO functionalities to cage the system process and manager memory, processors, and folders. This is different from virtualization with VMs because, in this mode, the technology needs to simulate the whole SO, drivers, and storage. This task consumes a lot of computational power and can sometimes be inefficient.

Docker networks

A Docker network is a layer that provides runtime isolation for containers. It is a kind of sandbox in which to run containers that are isolated from other containers. When the Docker is installed, by default it creates three networks that should not be removed. These three networks are as follows:

  • bridge
  • none
  • host

Also, Docker provides the user with an easy way to create your network. For this purpose, Docker offers two drivers—bridge and overlay.

Bridge can be used for the local environment, and it means this kind of network is allowed on a single host. It will be useful for our applications because it promotes isolation between containers regarding security. This is a good practice. The name of the container attached to this kind of network can be used as a DNS for the container. Internally, Docker will associate the container name with the container IP.

The overlay network provides the ability to connect containers to different machines. This kind of network is used by Docker Swarm to manage the container in a clustered environment. In the newest version, the Docker Compose tool natively supports Docker Swarm.

Docker volumes

Docker volumes are the suggested way to persist data outside of a container. These volumes are fully managed by Docker Engine, and these volumes can be writable and readable depending on the configuration when they are used with a Docker command line. The data of these volumes is persisted on a directory path on a host machine.

There is a command-line tool to interact with volumes. The base of this tool is the docker volume command; the --help argument on the end shows the help instructions.

Docker commands

Now we will take a look at Docker commands. These commands are used mainly in the development life cycle, commands such as spin up container, stop containers, remove, and inspect.

Docker run

docker run is the most common Docker command. This command should be used to start the containers. The basic structure of a command is as follows:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

The options arguments enable some configurations for the container, for instance, the      --name argument permits you to configure a name for a container. It is important for DNS when the container is running in a bridge network.

The network settings can be configured on the run command as well, and the parameter is  -- net. This enables us to configure the network to which the container will be attached. 

Another important option is detached. It indicates whether the container will run in the background. The -d parameter instructs Docker to run a container in the background.

Docker container

The docker container command permits you to manage the containers. There are many commands, as shown in the following list:

  • docker container attach
  • docker container commit
  • docker container cp
  • docker container create
  • docker container diff
  • docker container exec
  • docker container export
  • docker container inspect
  • docker container kill
  • docker container logs
  • docker container ls
  • docker container pause
  • docker container port
  • docker container prune
  • docker container rename
  • docker container restart
  • docker container rm
  • docker container run
  • docker container start
  • docker container stats
  • docker container stop
  • docker container top
  • docker container unpause
  • docker container update
  • docker container wait

There are some important commands here. The docker container exec permits you to run commands on a running container. This is an important task to debug or look inside the container files. The docker container prune removes the stopped containers. It is helpful in the development cycle. There are some known commands, such as docker container rm, docker container start, docker container stop, and docker container restart. These commands are self-explanatory and have similar behaviors.

Docker network

The docker network commands enable you to manage the Docker network stuff via the command line. There are six basic commands, and the commands are self-explanatory:

  • docker network create
  • docker network connect
  • docker network ls
  • docker network rm
  • docker network disconnect
  • docker network inspect

docker network create, docker network ls, and docker network rm are the main commands. It is possible to compare them with the Linux commands, where the rm command is used to remove things and the ls command is usually used to list things such as folders. The create command should be used to create networks.

The docker network connect and docker network disconnect commands allow you to connect the running container to the desired network. They may be useful in some scenarios.

Finally, the docker network inspect command provides detailed information on the requested network.

Docker volume

The docker volume command permits you to manage the Docker volumes via the command-line interface. There are five commands:

  • docker volume create
  • docker volume inspect
  • docker volume ls
  • docker volume prune
  • docker volume rm

The docker volume create, docker volume rm and docker volume ls commands are effectively used to manage the docker volume by Docker Engine. The behaviors are quite similar to those of the networks, but for volumes. The create command will create a new volume with some options allowed. The ls command lists all volumes and the rm command will remove the requested volume.

You have been reading a chapter from
Spring 5.0 By Example
Published in: Feb 2018
Publisher: Packt
ISBN-13: 9781788624398
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 €18.99/month. Cancel anytime