Search This Blog

Sunday, August 30, 2020

How to Create Eureka Server?

 How to Create Eureka Server?

Note : Eureka default port is 8761.

How to use Eureka in Project for Client side Discovery.

Step 1. Must add eureka dependency in pom.xml

Step 2. Add Eureka application property given flowing

  server.port=8761


eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF

logging.level.com.netflix.discovery=OFF

  Ref Doc: https://spring.io/guides/gs/service-registration-and-discovery/

Note: We must need to add eureka discovery Clint dependency 

Q. How to Use Load balancing?

Q. What is restTemplate and How to use It?



What is Rest template?

 What is Rest template ?

 Rest template is one of the way , using which we can communicate with microservices.

Note:  we have another way communicate with web services is web-client.

M V V I:  Whenever we are using  @Restcontroller  use @PathParam  and Whenever we are using Spring MVC or @Controller  use @PathVariable


Q. When to use JSTL??


Q. Addrases?

Solution:

1. Hard-code the URL.

2. Use Discovery the Service.

There re two types of Discovery Service:

1. Client side service Discovery.

2. Server Side service Discovery.

Pattern: Client-side service discovery

Context

Services typically need to call one another. In a monolithic application, services invoke one another through language-level method or procedure calls. In a traditional distributed system deployment, services run at fixed, well known locations (hosts and ports) and so can easily call one another using HTTP/REST or some RPC mechanism. However, a modern microservice-based application typically runs in a virtualized or containerized environments where the number of instances of a service and their locations changes dynamically.

Consequently, you must implement a mechanism for that enables the clients of service to make requests to a dynamically changing set of ephemeral service instances.

Problem

How does the client of a service - the API gateway or another service - discover the location of a service instance?

Forces

  • Each instance of a service exposes a remote API such as HTTP/REST, or Thrift etc. at a particular location (host and port)
  • The number of services instances and their locations changes dynamically.
  • Virtual machines and containers are usually assigned dynamic IP addresses.
  • The number of services instances might vary dynamically. For example, an EC2 Autoscaling Group adjusts the number of instances based on load.

Solution

When making a request to a service, the client obtains the location of a service instance by querying a Service Registry, which knows the locations of all service instances.

The following diagram shows the structure of this pattern.


Pattern: Server-side service discovery

Context

Services typically need to call one another. In a monolithic application, services invoke one another through language-level method or procedure calls. In a traditional distributed system deployment, services run at fixed, well known locations (hosts and ports) and so can easily call one another using HTTP/REST or some RPC mechanism. However, a modern microservice-based application typically runs in a virtualized or containerized environments where the number of instances of a service and their locations changes dynamically.

Consequently, you must implement a mechanism for that enables the clients of service to make requests to a dynamically changing set of ephemeral service instances.

Problem

How does the client of a service - the API gateway or another service - discover the location of a service instance?

Forces

  • Each instance of a service exposes a remote API such as HTTP/REST, or Thrift etc. at a particular location (host and port)
  • The number of services instances and their locations changes dynamically.
  • Virtual machines and containers are usually assigned dynamic IP addresses.
  • The number of services instances might vary dynamically. For example, an EC2 Autoscaling Group adjusts the number of instances based on load.

Solution

When making a request to a service, the client makes a request via a router (a.k.a load balancer) that runs at a well known location. The router queries a service registry, which might be built into the router, and forwards the request to an available service instance.

The following diagram shows the structure of this pattern.

Examples

An AWS Elastic Load Balancer (ELB) is an example of a server-side discovery router. A client makes HTTP(s) requests (or opens TCP connections) to the ELB, which load balances the traffic amongst a set of EC2 instances. An ELB can load balance either external traffic from the Internet or, when deployed in a VPC, load balance internal traffic. An ELB also functions as a Service Registry. EC2 instances are registered with the ELB either explicitly via an API call or automatically as part of an auto-scaling group.

Some clustering solutions such as Kubernetes and Marathon run a proxy on each host that functions as a server-side discovery router. In order to access a service, a client connects to the local proxy using the port assigned to that service. The proxy then forwards the request to a service instance running somewhere in the cluster.

Resulting context

Server-side service discovery has a number of benefits:

  • Compared to client-side discovery, the client code is simpler since it does not have to deal with discovery. Instead, a client simply makes a request to the router
  • Some cloud environments provide this functionality, e.g. AWS Elastic Load Balancer

It also has the following drawbacks:

  • Unless it’s part of the cloud environment, the router must is another system component that must be installed and configured. It will also need to be replicated for availability and capacity.
  • The router must support the necessary communication protocols (e.g HTTP, gRPC, Thrift, etc) unless it is TCP-based router
  • More network hops are required than when using Client Side Discovery
          ------------------------------ Important Question ----------------------------------------------
               1. What are client side Service Discovery Technology?
Answer-->  Edureka.
                2. What are Server side Service Discovery Technology?
Answer-->  NGIX, AWS.

          

Saturday, August 29, 2020

Friday, August 28, 2020

Web Service?

 What is Web Service?

--> A service on web, for machine consumption.(software level communication).

--> A Web service can have multiple micro-services.

--> Other services uses services.

--> If  Web services have some structural changes , other services may crash.

--> 

What is Entity?

Entity is nothing but a representation in the database.


Q. Why we move from monolithic app to microservices?

1. There will be high level of Inter dependency.

2. More complexity.

3. High level of  dependency.

4. There will not be categorization.

5 Difficult for Load balance.

6. Difficult to manage.

7. Lack of scalability.

8. Redeploy the entire application on each update.

9. Not Reliability.

10. Barrier to adopt new technology.


To overcome monolithic approch me can use Modular  approach.


Microservices:-

1. Web based Application.

2. Distributed  Environment.

Advantages.

1. Complexity get reduced by decomposing.

2. Microservices can be developed Independently using any language.

3. It reduces the barrier of adapting new technology.

4. Each microservices needs to  be deployed independently.


How to create Java WAR file with Maven ?

 First you must define your project with as packaging of war type:

<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>war</packaging>

Then you will need to use the maven plugin to generate the war when compiling:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <attachClasses>true</attachClasses>
        <webXml>target/web.xml</webXml>
        <webResources>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>true</filtering>
            </resource>
        </webResources>
    </configuration>
</plugin>
-----------------------------------------------------------------------------------

Create war file from Spring:boot project in Eclipse



You need to extend ****SpringBootServletInitializer**** in your @SpringBootApplication

You don't need to add the ****sprinng-boot-starter-tomcat**** dependency for war file generation

enter image description here

Use below configuration in pom.xml

<packaging>war</packaging>

Configure Build Path for your project and select JDK

Right click on project > Run As > Maven Install

It will generate the war file inside target folder.

enter image description here

Copy this war and deploy to any web/application server (I have renamed it to demo.war).

You can access the app by using your host name followed by the port and app name.

enter image description here

How to create war file from maven Project

step 1. Right click on project -> Run as -> Maven Clean.( this will clean up file inside target folder)

step 2.  Right click on project -> Run as -> Maven Install( it will try to create war based on package that you have chosen)


application.properties in Spring boot

 -------------- application.properties ------------------

Properties files are used to keep ‘N’ number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application.properties file under the classpath.

Note:1. application.properties  is very important file to override default values of spring boot.

2. static folder is used for static content file like   css and  js file.
3. application.properties  is case sensitive and must be accurate.

Ref doc: 1. https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/html/common-application-properties.html

2.  https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

Properties files are used to keep ‘N’ number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application.properties file under the classpath.

YAML File

Spring Boot supports YAML based properties configurations to run the application. Instead of application.properties, we can use application.yml file. This YAML file also should be kept inside the classpath. The sample application.yml file is given below −

spring:
   application:
      name: demoservice
   server:
port: 9090

Externalized Properties

Instead of keeping the properties file under classpath, we can keep the properties in different location or path. While running the JAR file, we can specify the properties file path. You can use the following command to specify the location of properties file while running the JAR −

   What is Class SpringApplicationBuilder

In our application we have used SpringApplicationBuilder in the starter application. starter is an simple application which will start the actual application instances programatically.

The number of processes to be started and type of process web/standalone will be passed as an argument to starter application, based on the arguments the application instances will be started. we have used -w to start as web application for state management.

boolean isWeb = // options parser, parse -w
new SpringApplicationBuilder(SpringBootAngularApp.class).web(isWeb).run(args);

There is another way of doing the same

SpringApplication sp = new SpringApplication(SpringApplicationBuilder.class);       
sp.setWebEnvironment(false);
sp.run(args);

We can also customise banner, loggers with SpringApplicationBuilder.



Ref doc: https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/builder/SpringApplicationBuilder.html

Saturday, August 22, 2020

What is SpringBoot ???

 What is Springboot?

Spring boot is nothing but opinionated version of Spring framework.

Note: Spring boot uses Java technology in the Background as a result Java or JDK must be in your System.


Spring boot starter project?????

What is Spring boot parent starter Project??

Q. what is spring boot application??

A Spring boot application is standalone application and we need not to deploy it anywhere, because it already comes with apache tomacat server.

Steps to create Spring boot Application??

1. use Annotation @SpringBootApplication  and this is the starting point of application

2. To run application we need to use SpringApplication.run(Application.class);

Ref: https://medium.com/@cancerian0684/run-method-on-application-startup-in-spring-boot-37aa5e82c948

Another Way

2

You can use an ApplicationListener

    public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

      public void onApplicationEvent(final ContextRefreshedEvent event) {
        ApplicationContext ctx = event.getApplicationContext();
        (new Thread() {
           public void run() {
             // do stuff
           }
         }).start();
      }

    }

Just register that as a Spring bean. As suggested in the comments, you can execute the code on another thread.

Noe: After run this application following things happens:

1. loads the default page. 

2. Starts the boo app.

3. Perform a class path scan.

4. Starts local tomcat.

For controller class use @Controller anotation

use @Requestmapping for url pattern.

Note:  Spring boot uses embedded tomcat server , it happens due to tomcat embedded dependency jar.

Note: By default our Spring Application doesn't support rendering of JSP page.Hence  we need to add dependency for tomcat embedded jasper 

In application.properties can be used for following ways if jsp page is not in default folder package :

The Solution

I found the answer via trial-and-error, which turned out rather annoying. I hope someone can correct me if this conclusion is wrong, but it appears that Spring Boot does not like the string WEB-INF. I renamed the WEB-INF directory to view and changed the application.properties to the following and the view loaded successfully.

spring.mvc.view.prefix=/view/
spring.mvc.view.suffix=.jsp