In this tutorial, you will learn how to startup your own Eureka Discovery Server, so that the Microservices you build can register with this server and become discoverable by other Microservices.
To learn how to setup Eureka Cluster, read the Eureka Server Cluster Setup tutorial.
For a step by step series of video lessons, please check this page: Spring Boot Microservices and Spring Cloud.
To learn how to make Microservices register with Eureka Server, read the following tutorial “Register with Eureka Server as Client“.
Create a New Spring Boot App
Eureka Discovery Server is a Spring Boot app. So to create a new Eureka Discovery server for our project we first need to create a new Spring Boot app. Please read the following tutorial on how to create a very simple Spring Boot application:
- Create a Simple Web Service Project with Spring Boot ( Includes Video Tutorial )
Configure Eureka Discovery Server POM.XML File
Now when you have your Spring Boot Application created. Open its POM.xml file and add the following dependency.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <type>jar</type> </dependency>
The above two dependencies go into the <dependencies> section.
Then add the following section to your POM.xml file as well.
<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>
and add the <properties>
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RC2</spring-cloud.version> </properties>
below is a complete POM.xml file from my working project.
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.appsdeveloperblog.photoapp.discovery</groupId> <artifactId>PhotoAppDiscoveryService</artifactId> <version>0.0.1-SNAPSHOT</version> <name>PhotoAppDiscoveryService</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RC2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <type>jar</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
Add @EnableEurekaServer Annotation
Now that we have the needed dependencies added to the POM.xml file, we can start using Eureka Server annotations.
To enable our application to work as Eureka Server we will need to add the @EnableEurekaServer annotation to Application class that contains public static void main(String[] args) function. Below is an example of my Java class with @EnableEurekaServer annotation added above the class name.
package com.appsdeveloperblog.photoapp.discovery.PhotoAppDiscoveryService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class PhotoAppDiscoveryServiceApplication { public static void main(String[] args) { SpringApplication.run(PhotoAppDiscoveryServiceApplication.class, args); } }
Configure Application.properties File
Before we can run our Eureka Server application we need to add a few basic configuration details to our application.properties file.
server.port=8010 spring.application.name=PhotoAppApi-eureka-server eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.instance.hostname=localhost eureka.client.serviceUrl.defaultZone = http://localhost:8010/eureka
- server.port – is a port number of which you need your discovery server to run. The port number must be unique and available.
- spring.application.name – is a name of your discovery server.
- eureka.client.registerWithEureka and eureka.client.fetchRegistry are set to false because we want to prevent this Spring Boot application to register itself with discovery server since this application is itself a discovery server.
Starting Eureka Discovery Server
We are now ready to start up our new Eureka Discovery Server and since it is a Spring Boot application we can start it up with maven command like in the example below:
mvn spring-boot:run
If needed, you can also pass command line arguments and override configuration properties at runtime.
Once your application starts up, open the browser window and navigate to:
http://localhost:8010
I used 8010 for the port number because this is what we have specified as a server port number in our application.properties file above. Once you open http://localhost:8010 url in the browser window, you should see the Eureka Discovery Server page.
At this moment our Eureka Discovery Server does not have any Microservices registered with it but we will do so in the following tutorials. The goal of this tutorial was to learn how to start up your own Discovery Service. To learn how to register existing Microservices with Eureka, have a look at this tutorial: Register Web Service with Eureka.
I hope this tutorial was helpful to you.
Spring Cloud Video Course
To learn more about how to build and run RESTful Microservices with Spring Boot and Spring Cloud have a look at my online video course: Spring Boot Microservices and Spring Cloud.
Happy learning!