Discovering Services with Eureka (2020) | TechGeekNext >>






Discovering Services with Eureka Example


What is Eureka?

Eureka, it's a registry that acts like a phone-book for your microservices. Eureka itself is a rest-based service, primarily used by Netflix in their AWS Cloud for finding services, for load balancing fail over, and mainly for middle-tier services. It's a really powerful tool for helping you kind of register these things dynamically. There's no database behind the scenes. It's really actually made up of a live look at the health of individual systems. With Spring Cloud, it's made Eureka even easier to use and so you have these sort of abstractions for easily discovering services from the registry. Very, very easy to use.

History of Eureka

So let's talk a little bit about the history. This was first released by the Netflix open source team in 2012, so this is mature technology. Mainly used for middle-tier load balancing and they use this for a number of things in Netflix, switching between versions of services, actually even, they use it for databases, not just web apps.

  • First released by Netflix OSS team in 2012
  • Used for load balancing
  • Integrated into many other Netflix project

Components of Eureka

Eureka Components

Eureka Server - Project Structure

Let's begin with creating Eureka Server and then will create services called Ticket Pass Service and it's Rate Service.
Eureka Project Structure

Create Spring Boot Eureka Server Project

Create Spring Boot Project from Spring Initializer site https://start.spring.io/. Create Eureka Server

pom.xml

If you see, we added spring-cloud-starter-eureka-server dependencies in pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>techgeeknext.eureka.server</groupId>
	<artifactId>techgeeknext-eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>techgeeknext-eureka-server</name>
	<description>Demo project for Spring Cloud Eureka</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Dalston.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-actuator</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

Properties

We don't want this service to register with Eureka, because this a Eureka Server, so make register-with-eureka flag false.
# set port
server.port=8761
# no need to register the server with the server
eureka.client.register-with-eureka=false
# don't need a local copy of the registry
eureka.client.fetch-registry=false

# value used for AWS, here can be anything
eureka.datacenter=techgeeknext-datacenter
eureka.environment=dev

Main Class

Add @EnableEurekaServer annotation in the main class to denote this service as Eureka Server.
package techgeeknext.eureka.server;

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

@EnableEurekaServer
@SpringBootApplication
public class TechgeeknextEurekaServerApplication {

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

Testing Eureka in Dashboard

Run this application, and goto http://localhost:8761/, and you can see Eureka Server up and running without any services register. Testing Eureka Server

Register Services with Eureka Server

In this tutorial, will create two services called ticket-service and rate-service, which will register with Eureka Server.
Services Project Structure

Services

Now let's create techgeeknext-eureka-ticket-service and techgeeknext-eureka-rate-service services to register in Eureka Server. In both services add below dependencies in application.properties
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false

eureka.instance.hostname=localhost

eureka.instance.instance-id=${spring.application.name}:${random.int}

server.port=0

techgeeknext-eureka-ticket-service

Define your service name in bootstrap.properties
spring.application.name=techgeeknext-ticket-service

pom.xml
Add spring-cloud-starter-netflix-eureka-client dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>techgeeknext.ticket</groupId>
	<artifactId>techgeeknext-eureka-ticket-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>techgeeknext-eureka-ticket-service</name>
	<description>Demo project for Spring Cloud Eureka</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Main class
Add @EnableEurekaClient annotation to declare this service as Eureka Client.
package techgeeknext.ticket;

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

@EnableEurekaClient
@SpringBootApplication
public class TechgeeknextEurekaTicketServiceApplication {

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

techgeeknext-eureka-rate-service

Define your service name in bootstrap.properties
spring.application.name=techgeeknext-rate-service

pom.xml
Add spring-cloud-starter-netflix-eureka-client dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>techgeeknext.rate</groupId>
	<artifactId>techgeeknext-eureka-rate-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>techgeeknext-eureka-rate-service</name>
	<description>Demo project for Spring Cloud Eureka</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Main class
Add @EnableEurekaClient annotation to declare this service as Eureka Client.
package techgeeknext.rate;

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

@EnableEurekaClient
@SpringBootApplication
public class TechgeeknextEurekaRateServiceApplication {

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

Testing Services in Eureka Dashboard

Run this application, and goto http://localhost:8761/, and you can see both services got register with Eureka Server. Testing Services in Eureka Server

Download Source Code

The full source code for this article can be found on below.
Download it here - Register Services With Eureka














































































Recommendation for Top Popular Post :