Sunday, 28 July 2019

Service Registry in MicroServices

The Service registry is a database populated with the information in how to dispatch requests to microservices instances.

In this tutorial we are using the Eureka Server is used for service discovery and registration.

For Setting up Follow the Below Steps:

Eureka Server.

For Configuring the server side follow the below steps.

1.Create a project named Discovery Server.
2.Add the Gradle dependencies.

Below is my File. /DiscoveryServer/build.gradle

plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.searchendeca.ecommerce'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}

ext {
set('springCloudVersion', 'Greenwich.SR1')
}

dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

3. Configurations related to the Eureka server.

Below is the file.

/DiscoveryServer/src/main/resources/application.properties

server.port=8761

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

//When the registry starts up it will complain that there are no replica nodes for the registry to //connect to disable the relevant logging.
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

4.Main Class.

You’ll first need a Eureka Service registry. You can use Spring Cloud’s @EnableEurekaServer to stand up a registry that other applications can talk to. This is a regular Spring Boot application with one annotation added to enable the service registry.

package com.searchendeca.ecommerce.DiscoveryServer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {

public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}

}


Now Server side configuration are done and now its time to configure the client service. Its not necessary to be a separate project, instead your custom project where you have hosted your services.

Client Side.

For Configuring the client side follow the below steps.

1. My Project Named ProfileService
2.Add Gradle Dependencies.

Below is my File /ProfileService/build.gradle

plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.searchendeca.ecommerce'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
mavenCentral()
}

ext {
set('springCloudVersion', 'Greenwich.SR1')
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

3.Below is my configuration.

/ProfileService/src/main/resources/application.properties

spring.application.name=profileService

4.Below is the Main class where the client identifies.

/ProfileService/src/main/java/com/searchendeca/user/ProfileServiceApplication.java

package com.searchendeca.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient

public class ProfileServiceApplication {

public static void main(String[] args) {
SpringApplication.run(ProfileServiceApplication.class, args);
}

}
Once this is setup your client service is identified in the eureka server.

Testing the application.



Access the Eureka server on http://localhost:8761/ you will see the service registered there.

Now Congratulations you have setup the discovery server and registered in it.



Happy Learning !!!

No comments:
Write comments