Microservices Using Spring Boot and Spring Cloud #1

Amigoscode
30 Nov 202151:16

Summary

TLDRIn this informative video, the host introduces a series on microservices, explaining their benefits over monolithic applications, such as easier deployment and scalability. The tutorial demonstrates setting up a microservice using Spring Boot and Maven, covering aspects like creating a RESTful API, configuring a PostgreSQL database, and connecting the service to it. The host also touches on advanced topics like Eureka server for service discovery and Kubernetes for container orchestration, promising further in-depth coverage in subsequent videos.

Takeaways

  • πŸ˜€ The video is an introduction to a series on microservices, focusing on teaching the concept and its implementation.
  • πŸ“ˆ The presenter plans to cover the use of Eureka server and Config server, and how they transition to Kubernetes for service management.
  • πŸ” The script explains the shift from monolithic applications to microservices for easier deployment, independent iteration, and the use of different databases and technologies.
  • 🌐 It outlines a basic microservices architecture with a load balancer, internal services like customer and fraud checks, and a notification service.
  • πŸ› οΈ The video demonstrates setting up a microservice using Spring Boot and Maven, including creating a parent project with dependency management.
  • πŸ”¨ The presenter guides through the installation of Maven and setting up a Spring Boot project with a specific group ID and artifact ID.
  • πŸ”‘ The importance of using Lombok for reducing boilerplate code and Spring Boot's starter dependencies for building RESTful APIs is highlighted.
  • πŸ“ The process of creating a customer microservice with a RESTful endpoint, using Spring annotations, and setting up a database connection is detailed.
  • πŸ’Ύ The script includes instructions for setting up a PostgreSQL database using Docker Compose and configuring the Spring application to connect to it.
  • πŸ”„ The video shows how to implement a simple customer registration API endpoint and the corresponding service and repository layers.
  • πŸš€ The final part of the script demonstrates how to test the microservice by sending a POST request using Postman and verifying the result in the database.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is an introduction to microservices architecture, focusing on setting up a microservices project using Spring Boot and Spring Cloud.

  • Why might someone want to use microservices instead of a monolithic application?

    -Microservices allow for easier deployment, quicker iteration, and the ability to work with different databases and technologies independently, making them suitable for large applications with many contributors.

  • What is the role of a load balancer in a microservices architecture?

    -A load balancer serves as the main entry point for client requests, routing them to the appropriate microservice based on the request path, ensuring efficient distribution of traffic.

  • What is the purpose of the Eureka server in the context of microservices?

    -The Eureka server is used for service discovery, allowing microservices to register themselves and discover other services without dealing with specific ports, simplifying inter-service communication.

  • What is the config server used for in a microservices setup?

    -The config server is used for centralizing the configuration of microservices, making it easier to manage different configurations for various environments like development, testing, and production.

  • How does the speaker plan to demonstrate the setup of microservices?

    -The speaker plans to demonstrate the setup by showing how to use tools like Eureka server, config server, and by transitioning to Kubernetes, which can simplify the management of microservices.

  • What is the significance of using Spring Cloud in the video?

    -Spring Cloud is used to facilitate the development of distributed systems, providing tools for service discovery, configuration, circuit breakers, tracing, and more, which are essential for microservices architecture.

  • Why might one choose to use Kubernetes for deploying microservices?

    -Kubernetes can manage the deployment, scaling, and operation of application containers across clusters, eliminating the need for manual management of services like load balancers and Eureka server.

  • What is the role of Spring Data in the microservices discussed in the video?

    -Spring Data is used for database interaction within microservices, providing abstraction and utilities for data access, making it easier to work with different databases.

  • How does the speaker intend to structure the microservices project?

    -The speaker intends to use Maven as the build tool and organize the project using a multi-module structure, allowing for shared dependencies and individual module configurations.

  • What is the first step in creating a new microservice module in the video?

    -The first step is to create a new Maven module with the specified group ID and artifact ID, and then configure it as a Spring Boot application with the necessary dependencies.

  • What is the purpose of the 'banner.txt' file created in the project?

    -The 'banner.txt' file is used to create a custom banner for the Spring Boot application, which is displayed when the application starts, adding a personal touch to the startup process.

  • How does the video script describe the process of creating a RESTful API endpoint?

    -The script describes creating a RESTful API endpoint by using the @RestController annotation, mapping the endpoint using @RequestMapping, and handling the request with a method annotated with @PostMapping.

  • What is the reason for using Lombok in the video?

    -Lombok is used to reduce boilerplate code, providing annotations like @Data and @Builder that automatically generate common methods like getters, setters, constructors, and toString.

  • How is the customer microservice connected to a database in the video?

    -The customer microservice is connected to a PostgreSQL database using Spring Data JPA, configured with connection details in the application.yaml file and the necessary dependencies included in the Maven pom.xml.

  • What is the purpose of Docker Compose in the context of this video?

    -Docker Compose is used to define and run multi-container Docker applications, in this case, to set up a PostgreSQL database and pgAdmin for the microservices project.

  • What does the video suggest for handling database migrations and schema changes?

    -The video suggests using Spring Boot's 'spring.jpa.hibernate.ddl-auto' property in the application.yaml file to handle database schema updates automatically.

  • How can one test if a customer has been successfully saved to the database?

    -One can test if a customer has been saved by sending a POST request to the customer registration endpoint using a tool like Postman and then checking the database for the new entry.

  • What is the next step after setting up the customer microservice in the video?

    -The next steps include setting up additional microservices for fraud detection and notifications, integrating distributed tracing, and building a client to interact with the microservices.

Outlines

00:00

πŸ“š Introduction to Microservices Series

The speaker begins by greeting the audience and introducing a new series on microservices, a topic frequently requested by viewers. They encourage viewers to subscribe to the channel and interact through likes, comments, and suggestions for future content. The speaker aims to explain microservices, their benefits over monolithic applications, and how they enable independent deployment and scalability. The first part of the tutorial will cover the use of Eureka server and config server, while later segments will transition to Kubernetes, emphasizing the reduced need for additional services and the focus on business logic within microservices.

05:01

πŸš€ Overview of Microservices Architecture and Tools

The speaker provides an overview of microservices architecture, discussing its components such as service discovery, API gateways, cloud configuration, and distributed tracing. They mention the use of Spring Cloud and Spring Boot for building microservices and highlight the benefits of using Kubernetes for orchestration, which simplifies infrastructure management. The speaker also touches on the importance of selecting only necessary components for a microservices setup and introduces the Spring Cloud sub-projects, emphasizing the ease of building and running applications with Spring Boot.

10:03

πŸ› οΈ Setting Up the Development Environment with Maven

The speaker guides viewers through setting up the development environment for microservices using Maven as the build tool. They demonstrate the installation of Maven on a Mac using Homebrew and verify the installation by checking the version. The speaker then uses a Maven archetype to create a new project and explains the structure of a Maven project. They also discuss the use of IntelliJ as the IDE and provide a brief overview of setting up the project in IntelliJ, including configuring the Java version.

15:03

πŸ”§ Configuring Maven for Microservices Development

The speaker delves into configuring the Maven parent project to manage dependencies and plugins for microservices development. They explain the use of dependency management to streamline the inclusion of common libraries across different microservices. The speaker also details the setup of the Spring Boot Maven plugin and the importance of consistent versions across the project. The process involves creating a parent POM file that defines common configurations and dependencies for all submodules.

20:03

🌟 Creating a Customer Microservice with Spring Boot

The speaker begins the process of creating a customer microservice, starting with the setup of the project structure and the inclusion of necessary dependencies. They demonstrate the creation of a main application class annotated with Spring Boot annotations and the configuration of the application properties. The speaker also adds a custom banner to the Spring Boot application for a personalized startup message and ensures that the project is set up for further development of the microservice.

25:04

πŸ›‘ Error Resolution and Dependency Management in Maven

The speaker encounters and resolves an error related to missing type information in the Maven dependency configuration. They correct the issue by specifying the type as 'pom' for the dependencies, allowing the project to recognize and manage the dependencies properly. The speaker then reloads the Maven project to reflect the changes and verify that the dependencies, including Lombok and Spring Boot Starter Test, are correctly included.

30:05

πŸ”¨ Building the Customer Microservice Model and Controller

The speaker continues the development of the customer microservice by creating a model class using Lombok annotations for data encapsulation and a controller class to handle RESTful API requests. They demonstrate the use of a record instead of a traditional class for immutability and simplicity. The controller is set up to accept POST requests that correspond to customer registration, and the speaker outlines the process of logging and handling these requests within the service layer.

35:05

πŸ—ƒοΈ Integrating a Database with the Customer Microservice

The speaker sets up a PostgreSQL database for the customer microservice using Docker Compose, providing a quick way to spin up the necessary database services. They guide viewers through configuring the database connection in the application properties and modifying the entity class to include JPA annotations for database table mapping. The speaker also demonstrates the creation of a repository interface for database operations and updates the service class to save customer data to the database.

40:05

πŸ—οΈ Setting Up the Database and Testing the Microservice

The speaker configures the customer microservice to connect to the newly set up PostgreSQL database. They use pgAdmin to create a new database and establish a connection to it. The speaker then updates the application's YAML configuration file to include the database connection details and adds the necessary dependencies for Spring Data JPA and the PostgreSQL driver in the Maven POM file. They also demonstrate how to generate a table based on the entity class and test the microservice's ability to save customer data to the database using Postman.

45:06

πŸ”„ Planning for Future Microservices and Closing Remarks

The speaker wraps up the current segment by summarizing the progress made in setting up the customer microservice and outlines plans for future videos. They mention the upcoming setup of the fraud and notification microservices, as well as the integration of distributed tracing to visualize the flow of requests between services. The speaker invites viewer feedback and encourages joining their community on Discord and Facebook, signaling the end of the current tutorial session.

Mindmap

Keywords

πŸ’‘Microservices

Microservices is an architectural style that structures an application as a collection of small, modular services that run in their own process and communicate with lightweight mechanisms, often an HTTP resource API. In the video, the theme revolves around teaching viewers how to build and understand microservices, starting from a monolithic application and moving towards a more flexible and scalable system where each microservice handles a specific business capability.

πŸ’‘Eureka Server

The Eureka Server is a service in the Netflix OSS ecosystem that is primarily used for locating services for the purpose of load balancing and failover in a distributed system. In the context of the video, the Eureka Server is introduced as a way to manage service discovery within a microservices architecture, allowing services to register themselves and discover other services without hard-coded connections.

πŸ’‘Config Server

The Config Server is a component in the Spring Cloud ecosystem that provides centralized external configuration management for applications across all environments. The video mentions the Config Server as a service for storing configuration properties, which can be crucial for different deployment environments such as development, testing, and production.

πŸ’‘Kubernetes

Kubernetes is an open-source container-orchestration system for automating application deployment, scaling, and management. In the script, Kubernetes is highlighted as a modern solution that can simplify the deployment and operation of microservices by handling service discovery, load balancing, and configuration management without the need for additional services like Eureka or Config Server.

πŸ’‘Load Balancer

A load balancer is a component that distributes network or application traffic across a number of servers. In the video, the load balancer is described as the main entry point for client requests in a microservices architecture, routing the requests to the appropriate microservice based on the path, thus ensuring efficient traffic management and fault tolerance.

πŸ’‘Spring Boot

Spring Boot is an open-source Java-based framework used to create stand-alone, production-grade Spring-based applications. The video script emphasizes the ease with which Spring Boot facilitates the building of Java applications, particularly microservices, by providing a simple way to get an application up and running with minimal upfront configuration.

πŸ’‘Spring Cloud

Spring Cloud is a set of sub-projects that extend the core Spring Boot functionality with features needed to develop distributed systems. The script mentions Spring Cloud as the means to build a fully-fledged system where different microservices can communicate with their own databases and other services, providing a more resilient and flexible architecture.

πŸ’‘Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. In the video, Docker Compose is used to set up a PostgreSQL database and a pgAdmin interface, which are essential components for the microservices to connect and manage their data, showcasing the ease of setting up a development environment with Docker.

πŸ’‘RESTful API

A RESTful API (Representational State Transfer) is an architectural style for networked hypermedia systems that uses HTTP requests to access and use data. The script describes building a RESTful API using Spring Boot, which allows for the creation of endpoints that can receive and send data in a standardized way, facilitating communication between the client and the microservices.

πŸ’‘Lombok

Lombok is a Java library that automatically plugs into your editor and build tools, spicing up your Java code with additional functionality such as handling getters, setters, toString, hashCode, equals, and more without modifying the actual code. The video mentions Lombok as a dependency included in the microservices to simplify the code and reduce boilerplate.

πŸ’‘Distributed Tracing

Distributed tracing is a method of tracking a request as it flows through a distributed system. The script refers to distributed tracing with Zipkin, which helps in understanding the flow of a request as it moves through various microservices, identifying bottlenecks and ensuring system reliability.

Highlights

Introduction to a series on microservices architecture.

Explanation of the shift from monolithic to microservices for easier deployment and scalability.

Discussion on the benefits of microservices such as independent deployment and technology diversity.

Overview of the components involved in a microservices architecture, including Eureka server and Config server.

Introduction to Kubernetes as an alternative to traditional service management tools.

Explanation of load balancers and their role in routing requests to appropriate microservices.

Demonstration of setting up a basic microservice using Spring Boot.

Guide on creating a Maven multi-module project for microservices.

Use of Lombok for reducing boilerplate code in Java classes.

Setup of a RESTful API endpoint for customer registration.

Integration of a PostgreSQL database using Docker and Docker Compose.

Configuration of Spring Data JPA for database interaction.

Implementation of a customer entity with JPA annotations for database mapping.

Creation of a repository interface for database access using Spring Data JPA.

Service layer implementation for handling business logic and data persistence.

Testing the microservice with Postman to ensure successful customer registration.

Conclusion and preview of upcoming topics including fraud and notification microservices, and distributed tracing.

Transcripts

play00:00

what's going on guys assalamu alaikum

play00:02

welcome to amigos code and today because

play00:04

you guys have been asking it for

play00:07

so long i decided to record a series on

play00:10

microservices yes microservices if

play00:13

you're into my channel go ahead and

play00:15

literally subscribe and also you'll see

play00:18

that like button just take two seconds

play00:21

and smash it literally just take two

play00:23

seconds and give me a thumbs up and

play00:25

comment down below and let me know what

play00:27

other videos that you want to see

play00:29

but in this video we're going to learn

play00:31

about

play00:33

so hopefully i will get the chance to

play00:34

teach you all of this that you see in

play00:37

here

play00:37

and to be honest this is quite

play00:39

interesting and um yeah so this is what

play00:42

professionals do and hopefully i will

play00:45

try my best to teach you everything in

play00:47

the best manner now what i want to do

play00:49

first is show you how you're going to

play00:52

use for example

play00:53

eureka server and config server

play00:57

but then i will show you as we

play00:58

transition to kubernetes that we

play01:01

literally don't have to have some extra

play01:04

services and we can just focus on what

play01:07

is

play01:08

our own domain which is our own

play01:10

microservices that perform our own

play01:13

business logic in this video i don't

play01:15

want to go too much in depth what a

play01:17

microservice is

play01:18

um if you want to know just let me know

play01:20

i can i can do an in-depth video on it

play01:23

but just imagine where you kind of start

play01:25

with the monolithic application

play01:27

and as the application grows

play01:29

then it's more difficult for you to

play01:32

deploy your application also if you have

play01:36

a lot of people working on the same code

play01:38

base then if my team wants to take

play01:41

something to production it means that it

play01:44

means that i will also be taking the

play01:46

other team's changes to production right

play01:49

now the microservices world pretty much

play01:53

just decouples things and we can just

play01:56

focus on a subset of the entire

play01:59

application and we can deploy things

play02:01

independently we can iterate quicker

play02:03

things are not coupled if you want to

play02:05

use a different database you can use a

play02:07

different database different technology

play02:09

so on and so forth right

play02:11

so what i want to teach you is so right

play02:14

here this is the public internet

play02:17

and

play02:18

as the clients send requests we'll have

play02:21

a load balancer in here so this will

play02:24

serve as the main entry point so we

play02:27

usually when you build applications you

play02:29

don't have the client talking directly

play02:32

to these services right here right

play02:35

because these services or everything in

play02:37

here

play02:38

this should be within your private

play02:40

network

play02:42

and then the load balancer is

play02:43

responsible to perform the routing to

play02:47

the corresponding microservice based on

play02:50

the path so you'll learn all of this

play02:52

later in this series

play02:55

but in a nutshell so you have the

play02:58

load balancer it sends requests added to

play03:00

these two services and i've called it

play03:02

customer and fraud and we also have

play03:05

notification right here so the idea is

play03:07

that

play03:08

um if

play03:09

a a client wants to register a customer

play03:12

for example we check whether that

play03:14

customer is fraudulent and this blue

play03:16

arrow indicates that this is an internal

play03:19

communication between these two services

play03:22

so if the customer is not fraudulent so

play03:24

if the customer is not fraudulent then

play03:26

we save the customer and then we

play03:29

push a notification into the message

play03:32

queue right here which this notification

play03:34

microservice can use and then perhaps it

play03:37

might use twilio or firebase to send

play03:39

sms's or push notifications

play03:42

now all of this in here is powered by a

play03:46

eureka server so

play03:48

as you'll see

play03:49

usually you don't want to be dealing

play03:51

with ports right so calling a service on

play03:53

a specific port because if you have

play03:56

multiple instances of it

play03:58

then how do you keep track

play04:00

of all those ports and basically you

play04:03

just want to have a centralized place

play04:05

where

play04:06

all the clients register to the server

play04:09

so in this case the erica server and

play04:11

then if you want to communicate with

play04:13

them you just need the service name and

play04:16

i'm going to show you how to set all of

play04:17

this you also have the config server

play04:20

right here so config server this is

play04:21

mainly for storing your

play04:24

configuration so whether it's

play04:26

development

play04:27

testing environment production

play04:30

uh pre-prod so on and so forth right so

play04:33

you'll see uh that interior we don't

play04:36

need even the config server

play04:38

uh but i'll show you what it is so on

play04:41

and so forth then we also have sleuth

play04:43

and distributed tracing

play04:45

so uh with zipkin so this is so that if

play04:48

a request comes so let's say that

play04:51

request gets to customer

play04:53

then it flows through

play04:55

fraud

play04:56

and then it goes to notification

play04:58

then with you should be retracing we we

play05:01

know exactly the entire flow and where

play05:04

perhaps is our bottle neck

play05:07

so this is pretty much

play05:09

the overall view

play05:11

now i'm going to be honest with you here

play05:13

so

play05:14

there is a lot to learn literally there

play05:16

is a lot to learn and usually you should

play05:19

just take what you need instead of

play05:22

implementing for example let's say

play05:23

config server or eureka server right

play05:26

because there are better ways of doing

play05:27

this these days usually

play05:30

if you want to take your application to

play05:31

production these days

play05:32

kubernetes all the way right so then you

play05:35

kind of don't need

play05:37

eureka server nor config server and also

play05:40

you'll see that you know the spring

play05:43

framework they have their own load

play05:45

balancer and also i would say like i'm

play05:48

going to show you exactly how to set it

play05:49

up but usually you would never never

play05:52

want to manage

play05:54

a load balance yourself and instead

play05:56

you

play05:57

pay someone else to

play05:59

manage the load balancer and make sure

play06:00

that it's

play06:02

you know always available it can also

play06:04

work multi-region and ssl search are

play06:08

done for you so on and so forth so that

play06:10

you just focus on your domain right so

play06:14

your own business logic the same with

play06:16

databases right so databases usually

play06:19

never

play06:20

host or you never

play06:22

um provision a database yourself right

play06:25

so you never have a server which you

play06:27

look after usually just have a database

play06:29

as a service

play06:30

and then you use it so the same will be

play06:32

for the load balancer um eureka server

play06:36

config server this is taken by

play06:37

kubernetes zipkin there's other things

play06:40

that you can use but i'm going to show

play06:42

you how to implement all of this so that

play06:44

you learn and then i'll show you better

play06:46

ways of doing things so that's all for

play06:48

now catch me on the next one

play06:57

by the end you should have a complete

play06:58

understanding on how to pull all of this

play07:01

together

play07:02

now how exactly are we going to achieve

play07:05

this

play07:06

if you've been following my courses on

play07:07

springboot then you should know by now

play07:09

that springbook makes it super easy for

play07:11

you to build java applications so in

play07:14

here in the official page of spring.io

play07:17

you can see that basically uh what you

play07:20

can do with spring you can write

play07:22

microservices

play07:23

reactive programming with asynchronous

play07:26

cloud web apps serverless event driven

play07:29

batch so on and so forth

play07:31

so you can go and basically see how to

play07:34

get an application up and running super

play07:36

easy with springbook

play07:38

but in this course you'll learn all of

play07:40

this

play07:41

and what i want to show you is so right

play07:43

here because we are learning about

play07:45

microservices let's just see what they

play07:47

have to offer

play07:48

so in here microservice architecture are

play07:51

the new normal

play07:52

building small self-contained ready to

play07:55

run applications that can bring great

play07:57

flexibility

play07:58

and added resilience to your code

play08:01

so basically you can read more about

play08:04

this um just to have a an overview

play08:07

but in a nhl in here the way that we

play08:11

build microservices in the spring world

play08:14

is using spring cloud

play08:16

so this diagram right here

play08:19

is the representation of this diagram

play08:22

right here

play08:23

and basically i've put everything

play08:25

together because i want to build a fully

play08:27

fledged system that communicates to each

play08:30

other with their own databases so on and

play08:32

so forth and this diagram here is much

play08:35

easier for you to understand what is

play08:37

happening but basically at the end of

play08:39

this course you'll have an understanding

play08:40

of all of this

play08:42

but what i want to show you is if you

play08:44

click on spring cloud in here

play08:47

so they give you again definition so

play08:50

developing distributed systems can be

play08:52

challenging and this is what the spring

play08:55

cloud

play08:56

project does gives you so here you can

play08:59

see that we have projects we have spring

play09:01

boot

play09:02

the framework cloud data so and so forth

play09:05

as well as security which i have a

play09:07

course on it

play09:08

but right here so again the exact same

play09:10

architecture you can see that we have

play09:13

service discovery api gateway cloud

play09:15

configuration circuit breakers tracing

play09:19

testing so on and so forth and

play09:22

what i want to show you actually if i

play09:24

click on projects uh let's go to spring

play09:26

cloud

play09:28

and right here have a look so this is

play09:31

all the sub projects within spring cloud

play09:34

now obviously we're not going to use

play09:36

everything in here because as i said you

play09:38

should only really take what is

play09:40

necessary for you to write your own

play09:43

microservices

play09:44

and then as we move towards kubernetes

play09:46

you'll see that some of these things are

play09:48

not needed but

play09:50

go ahead and basically have some reading

play09:53

on how to get all of this up and running

play09:56

but later you'll see that how we're

play09:58

going to install

play09:59

um spring cloud so on and so forth i'll

play10:02

make it super easy for you to understand

play10:04

with maven multi modules so hopefully

play10:07

now you know about spring cloud

play10:09

and within projects

play10:11

you can see that we have spring boot so

play10:13

this is what allows us to write the

play10:14

microservices and then spring cloud

play10:16

allows us to

play10:18

bring all these microservices together

play10:20

and also you've got spring data for

play10:22

databases spring security for securing

play10:25

your microservices so on and so forth

play10:28

if you have any questions drop me a

play10:29

message otherwise let's start developing

play10:32

microservices

play10:40

for this project we're going to use

play10:41

maven as our build tool

play10:43

and in this page right here which you

play10:46

can find the link in the description of

play10:48

this video you can see that they give

play10:50

you the installation guide for the

play10:51

operating system that you have

play10:53

and then how to create a simple project

play10:55

so because i'm on a mac i'm going to use

play10:57

brew

play10:59

so within my terminal i'm going to say

play11:01

brew install and then maven so this

play11:04

should take a while now

play11:10

and there you go so now we have maven

play11:12

now if you want to make sure that maven

play11:14

was successfully installed

play11:16

just click the screen and then type

play11:19

mvn

play11:21

and then version and there we go so this

play11:23

is the version which was installed in my

play11:26

machine so now that we have maven

play11:28

installed in here in this guide you can

play11:31

see that

play11:32

we

play11:33

have this command that we can use to

play11:35

create a project

play11:37

so we say mvn arc type generate and then

play11:42

we pass a couple of flags in here

play11:44

so here the group id so this is

play11:47

basically our organization and the

play11:50

artifact id is the application name and

play11:53

then we pass some other arguments so

play11:55

copy this command and then change this

play11:57

according to your organization or

play12:00

your name and this as well and i already

play12:03

have done that so i'm just going to

play12:05

paste it

play12:06

there we go and you can see that the

play12:08

group id is com.egoscode

play12:10

and then the artifact id is amigo

play12:13

services

play12:14

and in fact before i run this let me cd

play12:17

into desktop

play12:19

and then paste the command again there

play12:20

we go

play12:21

and just give a second

play12:25

and there we go

play12:26

so now in our desktop you can see that

play12:29

uses amigos code desktop we have this

play12:33

folder right here and what i'm going to

play12:35

do is cd2 amigos code services

play12:38

and then in here i'm going to say ls and

play12:41

this is the maven file structure so let

play12:43

me see if i've got a tree in here

play12:46

no so i'm going to say brew

play12:48

install and then tree

play12:52

there we go and now i can type tree and

play12:55

check this out so this gives me now the

play12:57

fold structure in a very nice way so you

play12:59

can see that in here

play13:01

we've got the root we have the palm.xml

play13:06

src main java.com amigos code app.java

play13:11

and then the same for the test

play13:13

and we are good to go next let's open up

play13:16

this folder with intellij

play13:25

so in here i do have the intellij

play13:28

ultimate edition

play13:29

and if you want to grab it i would

play13:31

recommend you to install jetbrains

play13:33

toolbox and if you want to grab it i

play13:35

recommend you getting it through

play13:37

jetbrains toolbox because this is the

play13:39

easiest way that you can manage all of

play13:40

your ides and perform all the upgrades

play13:43

on and so forth so here i've got the

play13:45

ultimate and this is what you see in

play13:47

here now let me open up this folder with

play13:51

intellij there we go and inside of my

play13:53

desktop i do have amigo services which

play13:55

is this

play13:57

folder in here so just pretty much just

play14:00

click on the xml or the folder either

play14:03

one should work

play14:04

open

play14:05

as project and just give it a second

play14:08

while it's downloading

play14:10

whatever it has to download there we go

play14:13

that's done

play14:14

and what i want to show you is so let me

play14:17

just put this full screen

play14:18

so

play14:19

in here if i go to file

play14:22

and then project structure

play14:24

i just want to show you that i'm using

play14:26

java 17

play14:27

and in here if you want to use java 17

play14:30

as well so that everything works

play14:32

with no issues just click on edit

play14:35

and then click on plus

play14:37

and you can download the jdk just like

play14:40

17

play14:41

but in here you can see that i can

play14:42

download all the other versions in here

play14:45

so 16 15 13

play14:47

11 and 1.8 so 17 is basically the one

play14:52

which has the

play14:53

long term support as i speak

play14:56

so just u17

play14:58

and then that's it next let's bring in

play15:00

the dependencies that we need in order

play15:02

to start building our micro services

play15:05

with springboot

play15:13

all right so let me go ahead and open up

play15:14

this folder right here and this is

play15:18

basically our parent project so we're

play15:20

going to use maven multi-module so that

play15:23

we can have dependencies and then let

play15:25

all the all the sub modules choose which

play15:28

dependencies that they need to import

play15:30

and we can also enforce dependencies to

play15:32

all microservices

play15:34

so in here what i want to do is

play15:37

this folder src we not going to need it

play15:41

at this point because this is

play15:44

the parent module

play15:47

and open up the pom.xml

play15:50

and in here so we have a couple things

play15:53

so just go through it but basically

play15:55

change this according to your url if

play15:59

it's not so let me just delete that

play16:02

and leave the properties as is

play16:06

and for dependencies so instead of

play16:08

dependencies let's just get rid of this

play16:10

dependency inside

play16:12

so we're going to have our own

play16:14

dependencies in a second

play16:16

and then for the build and then plug-in

play16:19

management so you can see that we have a

play16:21

bunch of plugins inside

play16:23

so let's also just get rid of everything

play16:27

and

play16:28

there we go so let me just have the

play16:30

empty plugins and there we go now inside

play16:33

let me just have the empty plugins tag

play16:36

and we're going to fill this in in a

play16:38

second now the dependency that we need

play16:41

in here is the following so here i'm

play16:43

going to say dependency management

play16:46

and then within have

play16:48

dependencies and the dependency that i

play16:50

want in here is

play16:52

so dependency so the artifact id so this

play16:55

comes from springboard dependencies and

play16:58

this comes from

play16:59

org.springframework.boot just like so

play17:02

and then choose a version so in my case

play17:05

i'm going to pick the two five seven so

play17:08

this version right here i'm gonna show

play17:09

you in a second

play17:11

but this i think is the latest version

play17:13

as i speak so make sure that you choose

play17:15

the exact same version so that you have

play17:17

no issues

play17:18

then what i also need is to say scope

play17:22

and then import

play17:24

so this is using the bom where inside of

play17:28

this artifact there's a bunch of

play17:29

dependencies that we can use and this is

play17:31

because we're not using springboard as

play17:33

the parent project so this is our own

play17:36

parent

play17:37

module and then we have some modules

play17:40

that can use basically

play17:42

these dependencies inside so if they

play17:44

need for example the spring pool starts

play17:47

a web they can just include it right

play17:49

here so this is the beauty of having the

play17:51

dependency management whereas if we have

play17:54

dependencies in here so dependencies so

play17:56

in here let's say that we want every

play17:59

single sub module to have the lombok

play18:02

dependency so here just say lombok and

play18:04

this comes from org.project lombok

play18:08

and let's also make sure that all some

play18:10

modules they have the testing

play18:13

artifact from springboo so here i'm

play18:15

going to say springboot dash starter and

play18:19

then test and this comes from org

play18:21

springframework.boot

play18:24

and there we go so let me just put this

play18:25

full screen now finally because we are

play18:27

using spring boot let's have the plugin

play18:30

for

play18:31

building the artifacts

play18:33

so here let's have a plugin and the

play18:35

group id will be com.spring

play18:38

boot dot spring

play18:41

framework and then dot boot

play18:43

and then the artifact id

play18:45

will be springboard maven just like that

play18:49

and also let's specify the version in

play18:51

here so version

play18:52

and this will come from the property so

play18:55

this will be

play18:56

spring dot

play18:58

boot

play18:59

dot maven

play19:00

dot

play19:02

plugin dot version and if you grab all

play19:05

of this

play19:06

and then scroll up

play19:08

and inside where we have the properties

play19:11

let's just paste that and then close

play19:13

that

play19:14

and then basically what i want is to say

play19:18

2.7 in here but i just realized that

play19:21

this version right here is the exact

play19:23

same thing

play19:24

so let's just take this from here and

play19:26

then say springboot

play19:28

dependencies

play19:30

dot and then version and we can take

play19:31

this from here and then put it here

play19:34

close that and then inside we can say

play19:37

2.5.7

play19:39

and if you want you can basically

play19:41

control these independently so

play19:44

the springbook maven plugin as well has

play19:46

the exact same version

play19:48

and you can see that this now is taking

play19:50

shape so this should be https

play19:52

and also this should be java 17.

play19:56

there we go

play19:58

and

play19:59

yeah so this is now taking shape

play20:02

so

play20:03

let's just basically reload the changes

play20:06

and it looks like that we have an error

play20:07

oh no that went away

play20:09

uh but basically if i open up maven

play20:13

so this is still red

play20:16

so if i clean

play20:18

you can see that the process terminated

play20:20

so let's have a look

play20:22

so it looks like that i'm missing the

play20:25

type inside of the dependency management

play20:27

so that's fine

play20:29

so

play20:30

inside in here i'm missing the type

play20:34

that's for sure and i want this to be

play20:36

palm so this is so that we can use

play20:39

all these dependencies for our sub

play20:41

projects now let's reload

play20:44

there we go so all good and you can see

play20:46

that we have the dependencies right here

play20:48

everything has been resolved

play20:49

if i open up dependencies we have lombok

play20:53

and starter test so this is what we have

play20:56

included right for our sub projects we

play20:59

also have plugins

play21:01

and in here let's now clean so i just

play21:03

want to make sure that this runs

play21:06

and there we go so let's validate as

play21:08

well

play21:12

and also work so we haven't got anything

play21:14

to compile but you can see that this is

play21:16

working

play21:17

so obviously if you want to grab

play21:20

this file you can grab it under the

play21:22

description of this video so that you

play21:23

can follow along and all you have to do

play21:25

is just change the

play21:28

url the name the group id and the

play21:31

artifact and off you go now just to

play21:34

recap so we have some properties

play21:36

but really dependency management so this

play21:38

is so that

play21:40

our subprojects can pick whatever

play21:42

dependency that they want and you'll see

play21:44

this in a second

play21:45

and then dependencies this is so that

play21:48

all sub modules

play21:49

by default have these two dependencies

play21:52

without them explicitly imported in

play21:55

their pom.xml and then we have the exact

play21:58

same thing for the build so build

play22:00

management right here

play22:01

and basically not everything will be and

play22:04

basically not everything will need this

play22:06

springboard maven plugin and that's why

play22:08

it's inside of this plugin management if

play22:10

you have any questions drop me a message

play22:12

otherwise let's move on

play22:21

all right now let's focus on how we're

play22:23

going to build

play22:24

this microservice right here so we're

play22:26

going to start with customer

play22:28

and then we're going to have the restful

play22:31

api that allows us to post a customer

play22:34

we also will configure the database

play22:36

right here and then once we have this

play22:39

then we can move into the other

play22:40

microservices how they will communicate

play22:42

to each other so on and so forth but for

play22:45

now let's just focus on building this

play22:48

space right here

play22:49

and then we can start assembling the

play22:51

puzzle

play23:00

now that we have this palm.xml right

play23:02

here and this is the parent palm

play23:05

let's go ahead and create our very first

play23:08

micro service as a sub module

play23:11

so in here what i'm going to do is right

play23:13

click in

play23:14

this folder right here

play23:17

and this is the root so amigos services

play23:20

or whatever you have named it right

play23:22

click new

play23:24

and then module

play23:26

now

play23:26

at this point so this will be a maven

play23:29

project

play23:30

the project sdk will be driver 17

play23:34

and don't tick anything

play23:36

just say next

play23:38

and right here you pretty much name the

play23:41

microservice so in my case i'm going to

play23:44

name it as customer so this will be a

play23:47

microservice that will deal with

play23:48

customers and if i expand the artifact

play23:51

coordinates in here so the group id will

play23:53

be

play23:54

com.ego's code and then the artifact id

play23:58

is customer and then leave the version

play24:00

as is finish

play24:04

and there we go

play24:05

so now have a look so we have this new

play24:08

folder right here this is customer and

play24:11

inside so let's just

play24:14

open up the

play24:15

parent

play24:16

palm and what i want you to notice is

play24:19

that

play24:20

we have now by default this

play24:23

new section in here so modules

play24:26

and this is a list of modules so we can

play24:29

only have one module right here and this

play24:32

is customer

play24:33

now if i open up customer so this will

play24:36

again have the exact same folder

play24:37

structure

play24:38

so for any

play24:40

maven project so you have the palm.xml

play24:43

so inside

play24:44

of this pom.xml

play24:46

we have some properties

play24:48

and basically have look the parent

play24:52

you can see that we have this icon so

play24:54

this goes to

play24:56

artifact amigos code services right here

play24:59

and i usually like to flip this around

play25:02

just like that

play25:04

and then the model version and then this

play25:05

is the artifact for

play25:08

this project right here which is

play25:10

customer

play25:12

and if i open up src

play25:14

main so this is uh basically everything

play25:17

is empty and we're going to add a few

play25:19

things in a second but for now what

play25:21

we're going to do is let's just open up

play25:22

the palm.xml for our customer

play25:26

microservice

play25:27

and let me close this for a second

play25:30

and what i want to do is inside in here

play25:33

let's have so we're going to say

play25:36

basically we want to have the

play25:38

dependencies

play25:40

right here

play25:41

and now we can bring any dependency that

play25:43

we want so for now all i want to bring

play25:46

is so i'm going to say dependency and

play25:48

this has the artifact id as spring

play25:52

boot dash starter

play25:55

dash and then web

play25:57

or starter

play25:59

dash and then web just like that

play26:02

and this comes from org dot spring

play26:04

framework dot boot

play26:05

there we go

play26:07

so now so in here have a look so you can

play26:10

see that this dependency right here

play26:13

right here it comes from so if i click

play26:15

on this button in here it comes from

play26:18

springboot dependencies dot palm so 257.

play26:22

so this is what we have defined in here

play26:25

have a look

play26:27

so right here so dependency management

play26:30

right here

play26:31

we brought all of these dependencies and

play26:33

now we are letting each of these

play26:35

microservices choose whatever dependency

play26:39

that they want to use so for this

play26:41

microservice we want to be able to write

play26:43

a restful api

play26:45

so hence we are bringing in this

play26:47

dependency

play26:48

at this point this is all we need so

play26:51

what we're going to do now is let me

play26:52

just close this bomb

play26:54

and also

play26:56

this main palm

play26:57

oh actually i closed the wrong one so

play26:59

let me just open the pump for customer

play27:02

now inside of

play27:04

src so instead of customer src main java

play27:08

let's create a new

play27:09

[Music]

play27:11

package

play27:12

name it as com dot amigos code dot

play27:16

and then customer

play27:17

just like that

play27:19

and what i want to do is i want to

play27:21

basically

play27:23

create a new java class

play27:25

and then i'm going to say

play27:27

customer

play27:28

application

play27:31

and this will have a main method

play27:35

and this class we're going to annotate

play27:37

it with add and then spring boot

play27:40

application

play27:42

and here i'm going to say spring

play27:46

and then application

play27:48

dot

play27:49

run

play27:50

and the primary source will be

play27:52

customerapplication.class

play27:57

there we go

play27:58

and also we need to pause the arguments

play28:00

from the command line

play28:03

and this is pretty much what we need

play28:06

so we also need to have the

play28:08

application.yaml inside of resources so

play28:11

here file

play28:12

and then say application.yaml

play28:16

or if you prefer properties it's

play28:18

completely up to you

play28:19

enter

play28:20

and inside i'm going to have server

play28:24

and then i want to have the port so i

play28:26

want to have control of the port just

play28:29

like that so the default is 8080 so

play28:32

let's say that we want this to be 8080

play28:36

and we also want to name this

play28:37

application so spring application name

play28:41

and this will be customer

play28:43

there we go now what i want to do is i

play28:46

want to basically create a new file in

play28:48

here and i'm going to name this as

play28:50

banner.txt

play28:53

and open up google

play28:55

and search for create spring boot banner

play28:58

and click on this very first link

play29:02

and in here let's just say customer

play29:05

and

play29:06

you can change the font if you want but

play29:09

i'm just going to leave the default copy

play29:11

everything

play29:13

go back

play29:14

paste that in

play29:15

and last but not least what i want you

play29:17

to do is open up the palm.xml

play29:20

and you should see this maven button

play29:22

right here or you can press shift

play29:24

command o to load the maven changes

play29:27

so we've added this dependency so let's

play29:29

just make sure this is reflected

play29:31

in this microservice so if i open up so

play29:34

right here we've got customer

play29:37

and

play29:38

the only dependencies that customer has

play29:40

is lombok and starter test

play29:43

and this is because so if you recall

play29:46

correctly

play29:48

inside of dependencies

play29:50

we said that we want all the

play29:51

microservices or all these submodules to

play29:54

have these two dependencies right

play29:57

but we included this one right here for

play30:00

this module but it's not showing up in

play30:02

here because i need to reload in here so

play30:04

you can reload from here or from here

play30:08

either one

play30:09

and you can see that

play30:11

you now have the dependency and it's

play30:13

just taking a while to index

play30:15

and there we go

play30:16

now let's open up the customer

play30:18

application so this is our main method

play30:21

and right click

play30:23

run

play30:26

and

play30:27

there we go so you can see that so let

play30:29

me show you the logs have a look so

play30:31

we've got our custom banner in here

play30:33

which is customer

play30:35

and

play30:36

you can see that everything started

play30:38

correctly and you can see that tomcat

play30:40

started on por 8080

play30:42

so there we have it we have our first

play30:44

microservice currently it doesn't do

play30:47

anything it doesn't talk to any database

play30:49

on so forth but we'll add that in a

play30:51

second but you see how easy it was first

play30:54

to bootstrap a microservice with

play30:57

springbook

play31:05

so within this package right here

play31:07

com.amigoscode.customer

play31:09

let's go ahead and create the model for

play31:12

this microservice so this will be

play31:14

customer

play31:16

so this is a class and

play31:18

in here i'm going to use lombok so every

play31:21

submodule has lombok in it so here i'm

play31:24

going to say add

play31:25

and then data

play31:27

so this is lombok and then at and then i

play31:30

also need the builder in here

play31:34

and there we go so we're going to come

play31:35

back to this class in a second but for

play31:37

now this is all we need

play31:39

now here let's have a couple of fields

play31:41

so let's have the id as an integer let's

play31:44

also have the first name and last name

play31:46

as a string and finally let's have the

play31:49

email in here

play31:51

there we go now what we're going to do

play31:52

is so we've got the model so basically

play31:55

we're just following the entire

play31:57

architecture

play31:58

and i've got a bunch of videos covering

play32:00

all of this stuff how to properly

play32:02

organize your applications on and so

play32:04

forth so here let's start from the

play32:07

controller so i'm going to say customer

play32:10

and then controller

play32:12

so this will be at and then rest

play32:15

controller

play32:17

this will also need the at request

play32:20

mapping

play32:21

and we're going to map this to api

play32:24

forward slash v1

play32:28

slash and then customers

play32:30

and i also want to have the at sla 4j

play32:34

just like that so i can just log a

play32:36

couple of things

play32:37

and i've just realized uh instead of a

play32:39

class let's just use a record and

play32:42

because inside we're going to pass a

play32:44

couple things in a second

play32:46

so

play32:47

just leave it like that

play32:49

and now let's have a public method that

play32:52

will basically take a customer from the

play32:55

request body

play32:56

and then register a customer so public

play33:00

void

play33:01

register customer

play33:04

this will take a customer request

play33:06

and we're going to create this in a

play33:07

second

play33:09

there we go

play33:10

and this customer request will come from

play33:13

the request body so at request

play33:17

and then body

play33:19

let's log the

play33:20

customer request so

play33:22

log

play33:24

dot and then info

play33:27

so new

play33:29

customer

play33:30

registration

play33:33

and inside we're going to pass the

play33:36

customer registration just like so and

play33:39

we also need to annotate this with ads

play33:41

and then post

play33:43

and then mapping so that we can fire

play33:45

post requests against this

play33:48

endpoint in here

play33:50

now let's create this class quickly so

play33:53

here new and then java class

play33:56

this will be a record

play33:58

and let's just say customer registration

play34:03

and then request

play34:05

there we go and what we want to pass in

play34:07

here is the first name we also need the

play34:11

last name and finally the email there we

play34:14

go so this is our record

play34:16

and

play34:17

the reason why i'm using a record and

play34:19

not a class is because i get

play34:20

immutability to strings equals and all

play34:22

that stuff for free whereas in here

play34:26

i'm actually going to use jpa in a

play34:28

second

play34:29

now inside of my customer controller so

play34:32

this should be actually customer

play34:34

registration request

play34:37

just like that

play34:39

delete this

play34:40

and then let's just log that instead in

play34:43

here

play34:44

and job done

play34:47

so there we go

play34:48

and now let's create this service that

play34:50

will basically

play34:52

handle this request

play34:54

so let's just say customer and then

play34:56

service

play34:58

customer service

play35:00

and

play35:01

this customer service so let's just

play35:03

create this class

play35:05

so create class

play35:06

there we go and let's also change this

play35:08

to a record

play35:10

and

play35:13

pass nothing inside for now

play35:15

and if i go back in here

play35:17

so what i want to do is just say

play35:19

customer service dot and then register

play35:24

customer and then pause the customer

play35:26

registration request

play35:28

just like that and i need to create this

play35:31

method in here so

play35:33

there we go

play35:35

and

play35:36

voila so i've got the

play35:39

request in here now let's turn this

play35:42

request into the customer so here i'm

play35:44

going to say customer customer equals to

play35:48

and then customer.builder

play35:52

we're going to pass the first name from

play35:54

the request

play35:56

we also need a last name

play35:58

and finally email and all we need to do

play36:01

is just say build so this is the builder

play36:04

pattern

play36:05

and there we go now let me just say to

play36:08

do here and we're going to do this in a

play36:09

second

play36:10

so there you have it so

play36:12

basically right here you can see that

play36:14

how the application is taking shape

play36:16

and

play36:18

what we have to do next is just get the

play36:20

database up and running and then

play36:22

configure our application so that we can

play36:24

store the customer to our database and

play36:27

finally we need to annotate this class

play36:29

with ad service so that spring

play36:30

initializes this as a bin for us so that

play36:33

we can inject it in our controller

play36:43

let's go ahead and get our database up

play36:45

and running so that we can connect our

play36:47

microservice to it so in here what i

play36:49

want you to do is under the root folder

play36:52

of microservices

play36:54

right here so amigos services actually

play36:57

go ahead and say new and then file

play37:00

name this as docker

play37:03

dash compose

play37:05

dot and then yammo and then press enter

play37:07

now inside i'm going to paste some yaml

play37:09

configuration

play37:10

which you can find under the description

play37:12

of this video and if you want to learn

play37:14

more about docker go ahead and check my

play37:16

website i've got a course teaching you

play37:18

everything you need to know about talker

play37:20

but in a nutshell so let's just start

play37:22

from the top we've got services

play37:25

this is postgres

play37:26

and then this is my container

play37:29

right here

play37:30

the image is postgres i'm exposing the

play37:33

ports

play37:34

and then this is the network and right

play37:36

here i've got pg admin so this is the

play37:38

graphical user interface client

play37:41

so i'm giving the name and then passing

play37:43

some environment variables to connect to

play37:45

it

play37:46

and exposing the port

play37:48

5050 to 80 inside of the container

play37:53

and right here i've got the networks so

play37:55

that they can talk to each other and

play37:57

then some volumes to store some data

play38:00

so once you have this file and make sure

play38:02

it's named as docker dash composer yamo

play38:05

so you can actually run this from here

play38:08

if you have the ultimate edition and if

play38:10

not i'm going to show you how to run it

play38:11

through the terminal so in here open up

play38:14

your terminal or command line and make

play38:17

sure that you are within the project so

play38:19

i'm going to type ls and you can see

play38:21

that i do have this docker compose.yaml

play38:24

now to get things up and running just

play38:26

type docker

play38:29

space and then compose

play38:31

and then up

play38:32

and then dash d for detach

play38:36

press enter

play38:37

and you can see that it's creating the

play38:39

network

play38:44

and now it's creating the containers so

play38:46

the pg admin as well as the database

play38:49

and there we go so you can see that this

play38:52

was super quick you can type docker

play38:55

compose nnps

play38:58

and you can see that we do have

play39:00

pg admin which is listing on port 5050

play39:04

and the postgres which is listening on

play39:06

port 5432

play39:08

so now that we have this database let's

play39:10

connect to it so open up your web

play39:12

browser and in here type localhost

play39:16

and then 50

play39:18

50. press enter

play39:20

just give it a second

play39:22

and there we go so you can see that oops

play39:25

the front is too big so now we need to

play39:28

set a master password so i'm just going

play39:29

to say password right so now let's

play39:32

basically add a new server

play39:34

and in here i'm going to basically name

play39:37

this as amigos code

play39:39

and the connection so in here

play39:42

the

play39:43

host this will be post grass

play39:47

and this is because we are connecting

play39:50

from a container to another container

play39:53

right here

play39:54

so if i show you inside of services have

play39:57

a look networks postgres in here

play40:00

so

play40:02

this guy right here uses the exact same

play40:05

postgres or

play40:06

the the exact same network which is

play40:08

postgres

play40:10

so pg admin uses postgres

play40:13

and right here this is how we define the

play40:15

network so that these two containers can

play40:17

talk to each other and if pg admin

play40:19

wasn't running with docker you would

play40:21

need to say

play40:22

local and then host

play40:25

so this is postgres the port is 5432

play40:29

leave the database maintenance the

play40:31

username so this is amigos code and the

play40:34

password is

play40:36

password there we go i can save the

play40:38

password

play40:40

and to be honest this is it so click on

play40:43

save

play40:44

and you can see that we managed to

play40:46

connect to our database

play40:47

so i can click on it

play40:50

and

play40:50

data you can see that

play40:52

there is not much information in here

play40:55

but basically if i expand

play40:57

in here you can see that we have

play40:58

databases and by default we have amigos

play41:02

code and postgres so now we have a

play41:05

database that we can work with

play41:08

next let's

play41:09

configure our microservice to connect to

play41:18

it so within intellij what i want to do

play41:22

is let me just close all of these tabs

play41:24

so close all tabs

play41:26

and let's start fresh

play41:28

so inside of our customer microservice

play41:30

open up application.yaml

play41:34

in here what i'm going to do is paste

play41:36

this configuration and you can find this

play41:39

under the description of this video and

play41:41

also if you want to learn more about

play41:42

spring data jpa you can check my website

play41:46

where i've got a course on this teaching

play41:48

you everything you need to know about

play41:49

connecting to databases joins how to

play41:52

model your table queries so on and so

play41:54

forth so it's a very in-depth course

play41:57

which you should take

play41:59

so in here what i have is the data

play42:01

source key right here and within it i do

play42:04

have the username so this is

play42:06

amigos code and the url this is

play42:09

postgresql localhost right here and the

play42:13

reason why is localhost is because

play42:15

our application right here when we start

play42:17

it's not a container if it was a

play42:19

container then we would need to connect

play42:21

via the network

play42:23

but in here you can see we also have the

play42:26

port

play42:27

and customer so i'm going to come back

play42:29

to this in a second we've got the

play42:31

password which is password and then here

play42:33

we have some configuration to set the

play42:36

dialet

play42:37

format sql

play42:38

update when we update our entities and

play42:41

then show some sql

play42:44

now this

play42:45

customer right here so this is the name

play42:47

of the database that we have to connect

play42:51

to

play42:51

so if i go back to pg admin inside of

play42:55

databases

play42:57

create database

play42:59

and then the owner is amigos code and

play43:02

here let's just say customer

play43:04

i'm going to say save

play43:06

and there we go so now we have this

play43:09

database that we can connect to it

play43:12

now the last thing that we have to do is

play43:14

to open up the palm.xml

play43:17

and in here we brought this dependency

play43:21

so start a web so this is for restful

play43:23

apis but we also want the dependency

play43:26

that allows us to perform queries and

play43:28

interact with our database so in here

play43:30

let me just put this full screen and

play43:33

this is spring and then boot and then

play43:36

starter

play43:37

and then j

play43:39

pa

play43:41

or data jpa there we go

play43:43

and what we also need to bring in is

play43:47

the postgres driver so dependency

play43:51

and this will be post

play43:55

and this is from

play43:56

org.postgresql

play43:58

and you can see that this is coming from

play44:01

the power and palm

play44:03

and in here i'm going to say that the

play44:05

scope for this

play44:06

is runtime

play44:08

there we go

play44:09

now go ahead and basically reload so the

play44:12

changes

play44:15

are picked up

play44:17

if i open up maven in a second you

play44:19

should see that we now have

play44:21

data jpa in here

play44:23

as well as the postgresql driver

play44:26

so all good

play44:27

now let's open up the customer class

play44:31

and in here we need couple things

play44:33

so one we need the at

play44:36

and then entity annotation

play44:40

and also we need at all rx constructor

play44:44

and also no

play44:46

our constructor in here we're not done

play44:49

yet so we need to annotate this with add

play44:52

and then id and the id will be based of

play44:54

a sequence so let's just have the

play44:56

sequence generator and then import

play44:59

sequence generator and finally we need

play45:01

to have the generated value just like so

play45:05

and let's import the generated value as

play45:08

well as the

play45:10

type so i think at this point is trying

play45:13

to

play45:14

basically have a star so here let me

play45:17

just say type

play45:18

so generation type

play45:20

and we are good to go

play45:24

so with this in place let's now create a

play45:27

new interface so this will be customer

play45:31

repository this will extend and then jpa

play45:36

repository

play45:38

where the entity is customer and the

play45:41

data type for the id is an integer now

play45:44

what we need to do is open up the

play45:46

customer service

play45:48

and inside of this record we're going to

play45:51

inject it so customer repository so that

play45:54

we can basically save

play45:56

our customer and for this to do right

play45:59

here store customer in db

play46:01

let's just say

play46:02

the repository so repository dot and

play46:06

then save

play46:07

and then our customer job done and

play46:09

delete this to do obviously there are

play46:11

more checks that we have to do

play46:13

but for now this should

play46:16

basically save customers in our database

play46:19

now let's start the application

play46:23

and hopefully this works

play46:26

and there we go you can see that we have

play46:28

some logging

play46:29

and here create table

play46:32

so id email first name last name

play46:34

and then the key

play46:36

is the primary key is the id

play46:39

and we also have the sequence so this is

play46:42

good stuff

play46:43

so now what i'm going to do is to open

play46:46

up pg admin and within

play46:49

amigos code databases

play46:52

customer and then open up schemas so we

play46:54

have one schema and this is the public

play46:57

schema

play46:58

open that up

play47:00

and then

play47:01

we should have one table inside and in

play47:04

fact let me just refresh

play47:07

there we go

play47:08

and now if i open up tables have a look

play47:12

we have the customer table in here

play47:15

and also the sequence which is right

play47:17

here so customer id sequence so this is

play47:21

really cool

play47:22

so if you want you can basically uh

play47:26

count the number of rows

play47:28

so this will give you zero so there is

play47:30

nothing inside and you can even

play47:33

basically run queries

play47:35

against this so you could say for

play47:37

example select

play47:39

star from and then customer

play47:43

and then run this

play47:45

and you can see that this gives us back

play47:47

but you can see that we actually are

play47:49

connected to our database to our

play47:52

database and we have a table called

play47:54

customer based of our entity now what i

play47:57

want to do is send a post request into

play47:59

my

play48:00

api and see whether we can save a

play48:02

customer so i'm going to use postman as

play48:05

my rest client and you can use any other

play48:08

so in here within postman

play48:10

new request two so this will be a post

play48:14

and the url will be a localhost

play48:16

and then 8080 v1 customers

play48:20

select body

play48:21

raw

play48:22

from text to json and in the body let's

play48:26

have this json blob

play48:29

so first name last name email jamila

play48:32

ahmed j ahmed and this json object right

play48:36

here corresponds to

play48:38

so

play48:39

if i go back in here so corresponds to

play48:42

this guy here so if i open up the

play48:45

customer registration request

play48:47

so it maps to this record first name

play48:50

last name and email

play48:51

so if i go back

play48:53

and then let's try and send the request

play48:56

and see whether it works

play48:57

so send

play49:00

there we go 200 status code which means

play49:03

that we most likely have saved

play49:06

this customer to our database

play49:09

so here i'm just going to rerun the

play49:11

exact same query

play49:14

and have a look so now we have jamila

play49:16

ahmed in here

play49:17

so this is beautiful and you can see

play49:20

that how we now have our microservice

play49:23

connected to its own database

play49:33

okie dokie i hope that you had fun

play49:36

learning about all of these different

play49:37

things and you saw that how

play49:39

we are structuring things nicely with

play49:42

maven multi-module using dependency

play49:44

management so on and so forth now what

play49:47

we have to do is

play49:49

so we have customer right here and in

play49:52

the next video what i want to show you

play49:54

is how we're going to set up

play49:56

fraud microservice

play49:58

as well as the notification microservice

play50:01

and we'll also

play50:03

stick in the distributed tracing so you

play50:05

see how the request flows from

play50:08

these different microservices right and

play50:10

also you'll see how we're going to build

play50:12

the client so that we can

play50:14

interact with these microservices as

play50:16

well as set up the eureka server and

play50:19

setting each of these microservices to

play50:22

register as a client if you enjoy what

play50:25

you saw comment down below literally

play50:27

comment down below let me know

play50:29

uh what you liked uh any suggestions

play50:32

that you might have and also don't

play50:34

forget to smash that like button so i

play50:36

can keep on recording these videos if

play50:38

you're not part of the amigo squad

play50:40

community go ahead and join the

play50:41

communities growing on discord as well

play50:43

as private facebook group i would love

play50:45

to see you there this is all for now

play50:47

i'll catch you on the next one

play51:15

you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
MicroservicesSpring BootEureka ServerConfig ServerKubernetesDocker ComposeRESTful APIDatabase ConfigurationLombok LibraryDistributed Tracing