/ JAVASPRINGBOOTMICROSERVICES

Microservice Registration With Spring Cloud Using Netflix Eureka - Part 3

In this series we’ll learn how to use Netflix Spring Cloud Components. In these posts we make use of Netflix component Eureka for service registry and discovery.

Part 1 - Overview of Netflix components.

Part 2 - Develop an Student service to produce and consume REST API using Spring Boot

THIS - Use Eureka for Service Registration

Part 4 - Use Eureka for Service Discovery

Part 5 - Load balancing using Netflix Eureka + Ribbon

We will register student-producer service to Eureka service. First we will develop the Eureka server service as follows-

eureka6

Add the dependancies in the pom.xml as follows:

<?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>org.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.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>
    </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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</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>

Define the Spring Boot class with annotations @SpringBootApplication and @EnableEurekaServer.

package com.stackdev;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

Finally specify the port on which the eureka application will get started -

server.port=8090

Run this as java application. Go to URL- http://localhost:8090/ We can see the Eureka Server page as follows-

eureka7

Next we will modify the student-producer module we defined previously to register to the Eureka server. Include these two dependanncies in your student-producer pom.xml file.



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

   <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
	

Add @EnableDiscoveryClient annotation in your Spring boot class.

In the application.properties specify the url on which the Eureka server is up and running.

eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka eureka.instance.instanceId=:1

To change the registered application name, create in resources a file named bootstrap.properties Add this in the bootstrap.properties file- spring.application.name=student-producer

Now run the student-producer and eureka application. We can see here that the registered application name is coming as STUDENT-PRODUCER.

eureka8

In the next post we will do service discovery using the student-consumer module.

Read More