Friday, September 06, 2019

Spring Eureka for Microservice

Spring Eureka Server is an application that maintains information about all client-service applications i.e. In SOA architecture where we are trying to decompose our application in many Microservice with a need where in one microservice need to contact another microservice we need to Manuel maintain all the required information like IP address, PORT etc but Eureka server does this for us. Eureka server knows all the client applications running on each port and IP address. All Microservice need to register themself to Eureka Server so that they are available to the client. Eureka service is also known as Discovery Server. So in short the Eureka server is nothing but a service discovery pattern implementation, where every microservice is registered and a client microservice looks up the Eureka server to get a dependent microservice to get the job done.

Let's create Spring Eureka Server as shown below. We are using STS IDE.
Image1
Image2

Image3
Change Property files
#Below line tell this is server and not the client
eureka.client.registerWithEureka = false
#Indicate no need to fetch client registry
eureka.client.fetchRegistry = false
#Operate on Port 8761.
server.port = 8761
Image4


Add anotation to main Spring boot class to inform that this is Eureka Server.
Image5
Once you successfully run below maven command you will get *.jar files.
mvn clean install
This is our Server jar and now run your Eureka Server jar

Image6
Note :- if you java.lang. TypeNotPresentException: Type javax.xml.bind.JAXBContext Exception check your JDK version.
Java 7 version: Included and Working
Java 8 version: Included and Working
Java 9 version: Deprecated
Java 10 version: Deprecated
Java 11 version: Removed
so either used JDK8 or lower version.
Now lets create a another Microservice as a client and register it with our above created Eureka Server.
Image7

Image7
Image8

Change Property files
#Give the address of server where he need to register itself.
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka
#Communication can be done with the help of IP address.
eureka.client.instance.preferIpAddress = true
#Itendified by below given name by Eureka Server and other client.
spring.application.name = eurekaclient

Image9

Add annotation to main Spring boot class to inform that this is Eureka Server.

Image10
Once you successfully run below maven command you will get *.jar files.
You also need add following items in the pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Image11
mvn clean install
Now runt the created jar file using below command
Note: As I am using different port 8888 i need to use below comments to start the spring boot eureka client.
java -Dserver.port=8888 -jar siddhu-eureka-example-client-0.0.1-SNAPSHOT.jar
Run following command on url http://localhost:8888/
Image12Image13

No comments: