Chapter 3
Chapter 3
Deployment
Deployment
Micro services deployment with Docker deals with 3
parts. They are:
1. Application packaging, eg: JAR.
2. Building Docker image with a JAR and dependencies
using a Docker instruction file, the Docker file, and
command “docker build”.
3. Docker container execution from this newly built
image using command “docker run”.
Setting the Development Environment
Tools & Technologies required :
1. Net Beans IDE.
2. Spring Framework.
3. Maven tool.
4. Jetty embedded server.
5. Spring Boot configuration.
6. Postman extension of Chrome.
Net Beans IDE installation and
setup
Download Net Beans IDE and its supported bundles.
The required packs and runtimes are denoted as already
installed, which is shown as:
After downloading the installation, execute the installer file.
Accept the license agreement as shown below:
Once the Net Beans IDE is installed, start the Net Beans IDE.
Net Beans IDE should look as follows:
Maven is a Java build tool available in the Net Beans IDE.
Even Gradle tool can be used.
Start a new Maven project to build our first REST
application.
The steps for creating a new empty Maven project are:
1. Click on New Project (Ctrl + Shift + N) under the File
menu. It will open the New Project wizard.
2. Select Maven from the Categories list. Then, select
POM Project from the Projects list, as shown in
following screenshot:
Now click on Next button.
3. Now, enter the project name as 6392_chapter2. Also, enter the other
properties as shown in the following screenshot:
After finishing all the preceding steps, Net Beans will
display a newly created Maven project.
You will use this project for creating the sample rest
application using Spring Boot.
4. To use Java 9 as a source, set Source/Binary Format
to 9, as shown in the following screenshot:
5. Go to Build, click Compile and make sure that Java
Platform is set as JDK 9 (Default) as follows:
6. Similarly, you can add two new modules named lib
and rest in the Modules folder by opening the right-
click menu and then selecting the Create New
Module option.
7. This time you should select Maven from the
Categories list and Java Application from Projects
list in New Project dialog box.
Spring Boot Configuration
Amazing Spring tool created by Pivotal, released in April
2014.
Spring Boot is preconfigured to make production-ready web
applications very easily.
The default packaging option is JAR, used for micro services
development.
Spring Cloud - wrapper on top of Spring Boot.
Sample REST application
Create two modules — lib and rest.
The lib module will provide the models or any supported
classes to the rest module.
The rest module will include all the classes that are required
to develop the REST application and it will also consume the
model classes defined in the lib module.
Both the lib and rest modules are maven modules and
their parent module is our main project
“6392_chapter2”.
The “module-info.java” file is an important class that
governs the access of its classes.
We'll make use of requires, opens, and exports to use
the spring modules and establish the provider-
consumer relationship between the lib and rest
modules of our REST application.
The steps for adding Spring Boot to the main project are:
1. Open the pom.xml file that is available under
6392_chapter2 | Project Files to add Spring Boot to the
sample project. It is shown as:
Contd…
Contd…
Contd…
The input field is the input parameter for the square root
function, and the content is the textual representation of the
result.
Now, we can create a resource representation class to
model the representation by using Plain Old Java
Object (POJO) with fields, constructors, setters, and
getters for the input, output, and function data.
We create it in the lib module shown as:
Writing the REST controller class
Now, we'll create a REST controller to handle the Calculation
resource.
The controller handles the HTTP requests in the Spring
RESTful web service implementation.
Here, we are exposing only the power and sqrt functions for
the Calculation resource using URI /calculation/power
and /calculation/sqrt.
Here, due to Spring's HTTP message converter support, the
Calculation object gets converted to JSON automatically.
You don't need to do this conversion manually.
Execution
Create a RestSampleApp class with the annotation
SpringBootApplication.
The main() method uses Spring Boot's SpringApplication.run()
method to launch an application.
We annotate the RestSampleApp class with the
@SpringBootApplication annotation that implicitly adds the
following tags:
The @Configuration annotation tags the class as a source of bean
definitions for the application context.
The @EnableAutoConfiguration annotation indicates that Spring
Boot is to start adding beans based on class path settings, other
beans, and various property settings.
The @EnableWebMvc annotation is added if Spring Boot finds
spring-webmvc on the class path.
It treats the application as a web application and activates key
behaviors such as setting up DispatcherServlet.
The @ComponentScan annotation tells the Spring to look for other
components, configurations, and services in the package shown
below:
This web application is 100% pure Java and we don’t deal with
configuring any plumbing or infrastructure using XML; instead, it
uses the Java annotation that is made even simpler by Spring Boot.
So, there wasn't a single line of XML except pom.xml for Maven and
even a web.xml file.
Adding a Jetty-embedded server
We need to add a Jetty application container dependency to
support the Jetty web server.
Jetty allows you to read keys or trust stores using class paths.
A relative section on pom.xml of module rest is shown below:
Running the Maven tool
The steps that should be followed to use the Maven tool to
execute the generated JAR file are:
1. Right-click on the pom.xml file .
2. Select Run Maven | Goals... from the pop-up menu, then it
will open the dialog. Now type spring-boot:run in the
Goals field.
Here we have used the released version of Spring Boot in
the code.
If you are using the snapshot release, you can check the
Update Snapshots checkbox.
To use it in the future, type spring-boot-run in the
Remember as field.
3. Now, we can directly click Run Maven | Goals | spring-
boot-run to execute the project. It is shown below:
Eureka Client :
Connection between the Eureka server and the client is
established.
Our services can use the following Spring configuration to
configure the Eureka client.
Add the following configuration in the Restaurant, Booking, and
User services (restaurantservice\src\main\resources\
application.yml):
eureka:
client:
serviceUrl:
defaultZone:http://localhost:8761/eureka/