This is a quick example for setting up Eureka server using Spring Cloud.
You can download the whole project by using following link.
For this tutorial, we will be creating a New Maven Project. To keep thing more simple we will be creating a simple maven project i.e. we will be skipping archetype selection.
This post is a part of the following series.
- Part 1 – Setting Up Eureka Server Using Spring Cloud
- Part 2 – Developing a Eureka client and registering it with Eureka Server.
- Part 3 – Developing Eureka client and consuming service exposed by another Eureka client.
Just fill in the requisite Group Id and Artifact Id on next screen and we are good to go.
You will need to create only two files ‘AsmEurekaApplication.java’ and ‘application.properties’ for this project. You may refer to the following directory structure for their respective locations.
Replace content in pom.xml file with following content
<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>com.example</groupId>
<artifactId>asm-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
Looking for Eureka server built using Spring cloud 1.5.18.RELEASE/ Edgware.SR5 version? You can copy its pom.xml from this post.
Under resources folder create a file with name ‘application.properties‘ and add the following content
spring.application.name=ASMEurekaServer
server.port=11800
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.hostname=localhost
eureka.server.enableSelfPreservation=false
Create file ‘AsmEurekaApplication.java‘ and add the following content
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class AsmEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(AsmEurekaApplication.class, args);
}
}
That’s all. The magic annotation @EnableEurekaServer will do rest everything for you.
Now just run AsmEurekaApplication.java as Java application and Eureka server should start on port 11800. You can open the eureka server dashboard by navigating to http://localhost:11800 from your browser.
Downloads:
Further Reading:
javainuse.com has a four-part series explaining how to develop, register and discover micro-services using Eureka.
- Part 1 Develop an Eureka server using Spring Boot and register an application to it
- Part 2 Develop microservices with REST APIs for producing and consuming.
- Part 3 Develop Eureka Server Microservice and register the employee-producer code to it.
- Part 4 Develop Eureka Server Microservice and use discovery service for employee-consumer.
Another great explanation Spring Cloud Netflix – Eureka at Baeldung and Spring Cloud Service Discovery with Netflix Eureka at HowToDoInJava. You should check both of those.