SPRING-BOOT-MICROSERVICES

SpringBoot:

Why should we use Spring Boot Framework?

We should use Spring Boot Framework because:

There are the following Spring sister projects are as follows:

It facilitates integration with other enterprise applications using lightweight messaging and declarative adapters.

Advantages of Spring Boot:

Limitations of Spring Boot:

Spring Boot can use dependencies that are not going to be used in the application. These dependencies increase the size of the application.

Spring Boot Features:

SPRING-BOOT-MICROSERVICES Annotations:

No beans.xml

Spring MVC and REST Annotations:

Boot-starter-logging:

REST request validation annotations:

@AssertFalse -> The annotated element must be false.

@AssertTrue -> 	The annotated element must be true.

@DecimalMax -> 	The annotated element must be a number whose value must be lower or equal to the specified maximum.

@DecimalMin -> 	The annotated element must be a number whose value must be higher or equal to the specified minimum.

@Future -> 	The annotated element must be an instant, date or time in the future.

@Max -> 	The annotated element must be a number whose value must be lower or equal to the specified maximum.

@Min -> 	The annotated element must be a number whose value must be higher or equal to the specified minimum.

@Negative -> 	The annotated element must be a strictly negative number.

@NotBlank -> 	The annotated element must not be null and must contain at least one non-whitespace character.

@NotEmpty -> 	The annotated element must not be null nor empty.

@NotNull -> 	The annotated element must not be null.

@Null -> 	The annotated element must be null.

@Pattern -> 	The annotated CharSequence must match the specified regular expression.

@Positive -> 	The annotated element must be a strictly positive number.

@Size -> 	The annotated element size must be between the specified boundaries (included).

AOP annotation:

*   @Before: declares the before advice. It is applied before calling the actual method.
    *   @After: declares the after advice. It is applied after calling the actual method and before returning result.
    *   @AfterReturning: declares the after returning advice. It is applied after calling the actual method and before returning result.
    *   @Around: declares the around advice. It is applied before and after calling the actual method.
    *   @AfterThrowing: declares the throws advice. It is applied if actual method throws exception.

Junit Test annotation:

	* @BeforeClass 
	* @Before
	* @Test
	* @After
	* @AfterClass

Various Assest Statement:

Void  equals(boolean exptation, boolean Actual)
Void  Notequals(boolean exptation, boolean Actual)
Void  assertTrue(boolean condition)
Void  assertFalse(boolean condition)
Void  assertNull(Object object)
Void  assertNotNull(Object object)
Void  assertSame(Object object1,Object object 2)
Void  assertNotSame(Object object1,Object object 2)	
void assertArrayEquals(expectedArray, resultArray);

Lombok

*Project Lombok is a Java library tool that generates code for minimizing boilerplate code.

*The library replaces boilerplate code with easy-to-use annotations.

* Will add all getter and setters 

* To string

* Equal and barcode

Spring Boot with Lombok post. In this part I’ll discuss the following Lombok constructs:

```

var and val
@Getter, @Setter
@NoArgsConstructor, @AllArgsConstructor
@Data
@NotNull

```

Web App:

* request
* response
* get
* post
* jsp
* jstl
* bootstrap

Spring mvc:

* dispatcher servlet
* view resolver
* model 
* view
* validation 
* form tags

Spring Boot:

* starterss
* Autoconfiguration
* Initializr
* actuator
 	* embedded servers

External config:

MSA:

Advantage:

Microservices:

12FactorAPP-Microservices

Properties in Spring Boot:

The application.properties file allows us to run an application in a different environment. In short, we can use the application.properties file to:

* Configure the Spring Boot framework
	
* define our application custom configuration properties
	

Spring Boot Property Categories:

There are sixteen categories of Spring Boot Property are as follows:

Core Properties
Cache Properties
Mail Properties
JSON Properties
Data Properties
Transaction Properties
Data Migration Properties
Integration Properties
Web Properties
Templating Properties
Server Properties
Security Properties
RSocket Properties
Actuator Properties
DevTools Properties
Testing Properties
#MYSQL DB Properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbname
spring.datasource.username=root
spring.datasource.password=root

#JPA Properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

#Eureka Properties
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureak.instance.hostnam=localhost
management.endpoints.web.exposure.include=*
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka

spring.cloud.config.uri=http://localhost:8888

spring.rabbitmq.addresses=amqps://ailhzrhf:pnHKsJmZxwUZwUh_P7wEBiiD37BrfCnQ@finch.rmq.cloudamqp.com/ailhzrhf

Server:
    Jetty
    Spring jpa mysql security 
    

security:
  oauth2:
    client:
       clientId: a48f56f893775afb1912
       clientSecret: c91036f105c04d0666655f411f716c43b606d373
       accessTokenUri: https://github.com/login/oauth/access_token
       userAuthorizationUri: https://github.com/login/oauth/authorize
       clientAuthenticationScheme: form
    resource:
      user-info-uri: https://api.github.com/user
      prefer-token-info: false 

Spring Boot DevTools

DevTools stands for Developer Tool. The aim of the module is to try and improve the development time while working with the Spring Boot application.

Spring Boot DevTools pick up the changes and restart the application.

We can implement the DevTools in our project by adding the following dependency in the pom.xml file.

<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-devtools</artifactId>  
<scope>runtime<scope >  
</dependency>
Spring Boot DevTools provides the following features:
* Property Defaults
* Automatic Restart
* LiveReload
* Remote Debug Tunneling
* Remote Update and Restart	
LiveReload works on the following path:
	
/META-INF/maven
/META-INF/resources
/resources
/static
/public
/templates
	

Microservies-MSA

Springboot with Microservies

N-Tier and monolithic applications used to be the de facto standard. In one single binary web artifact, like an EAR or WAR file, there would be a layered architecture with the decomposition of code into more functional components.

There are several disadvantages to the n-tier monolithic application architecture:

MSA Features:

Eureka:

What make a microservice different from a normal RESTful service?

  A microservice must register itself with a discovery service

#### Eureka server to load:

pom.xml:

application.properties:

spring.appliation.name=eurela-server
server.port=8761

#dont register itself as client

eureka.client.regiser-with-eureka = false
eureka.client.fetch-registry= false
logging.level.com.netflix.eureka=ON
logging.level.com.netflix.discovery = ON
Starter:
  @SpringBootApplication
  @EnableEurekaServer
login:
  https://localhost:8761
Eureka dashboard:
Spring Data REST makes it easy to expose microservices.
Spring Data REST builds on top of Spring Data repositories and automatically exports those as REST resources.

So how does Spring Data Rest work?

Item Service

Eureka client

pom.xml:


HTTP defines these standard status codes that can be used to convey the results of a client’s request. The status codes are divided into five categories.

1xx: Informational – Communicates transfer protocol-level information.
2xx: Success – Indicates that the client’s request was accepted successfully.
3xx: Redirection – Indicates that the client must take some additional action in order to complete their request.
4xx: Client Error – This category of error status codes points the finger at clients.
5xx: Server Error – The server takes responsibility for these error status codes.

There are several ways to communicate between two Spring Boot applications:
RESTful web services:

One application can expose its functionality as RESTful web services, which can be consumed by the other application using Spring’s RestTemplate or WebClient.

Messaging:

Applications can use a messaging system such as Apache Kafka or RabbitMQ to send and receive messages. Spring provides support for both of these systems through the Spring Kafka and Spring Rabbit projects.

Feign client:

One application can use the Feign client to make remote calls to the other application. Feign is a declarative web service client and can be used to communicate between applications using HTTP.

gRPC:

Applications can use gRPC to communicate with each other. gRPC is a high-performance, open-source framework for building remote procedure call (RPC) APIs. Spring provides support for gRPC through the Spring gRPC project.

Data sharing:

Applications can share data through a shared database or a data cache such as Redis or memcached. Spring provides support for various data sharing options through its Spring Data project.

Service Discovery:

Spring Cloud provides a way to register and discover microservices, one application can use service discovery to communicate with the other application.

It’s depend on your requirement which one you want to use for communication between two spring boot application.


Package Details:

* ACEFMRSHWU

* Config | Controller | Exception | Filters | Model | Repository | Headers | Resources | Security | Auth | Util | Webservice | Model | WSDL

sb

image

SPring Boot

Follow us on Blog

↥ Back To Top