1. Overview

This article contains Spring boot cloud eureka server example with step by step. Spring cloud provide netflix project for spring boot cloud implantation, Netflix provides eureka server feature to manage micro service spring boot application.

Steps to configure Eureka server

  1. add spring-cloud-starter-eureka-server dependency in pom.xml
  2. Enable eureka server using annotation in main class :@EnableEurekaServer
  3.  Add server port and default url in application.properties or application.yml
  4. Build application and open dashboard

2. Eureka Server Configuration Example

Spring boot cloud eureka server example

Spring boot cloud eureka server example

2.1 pom.xml

spring-cloud-starter-eureka-server requires for eureka server management. Need to add in maven dependency or must be available in CLASSPATH

<?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>spring-boot-example</groupId>
    <artifactId>spring-boot-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Spring boot cloud eureka server example</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 application.properties

eureka.client.register-with-eureka indicate that do not resister self as client

eureka.client.serviceUrl.defaultZone Self Server URL

eureka.server.eviction-interval-timer-in-ms indicate heart bit to check register services(client) availability.

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.server.eviction-interval-timer-in-ms=1000

2.3 application.yml

eureka server application.yml properties as bellow, application.yml or application.properties requires not both.

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone : http://${eureka.instance.hostname}:${server.port}/eureka/

  server:
    eviction-interval-timer-in-ms: 1000

2.4 EurekaServerApplication

@EnableEurekaServer indicate that consider this application or service as ureka server. This annotation is org.springframework.cloud.netflix.eureka dependency which must be available in CLASSPATH

package com.javadeveloperzone;

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

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan 
@EnableEurekaServer // Indicate Eureka Server Application     
public class EurekaServerApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(EurekaServerApplication.class, args);            // it wil start application

    }
}

2.5 Run Spring boot application:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2018-01-01 11:49:37.294  INFO 80240 --- [           main] c.j.EurekaServerApplication              : No active profile set, falling back to default profiles: default
2018-01-01 11:49:37.323  INFO 80240 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]4d411036: startup date [Mon Jan 01 11:49:37 IST 2018]; parent: org.spring[email protected]563f38c4
2018-01-01 11:49:39.051  INFO 80240 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=f1969cf7-dd87-3d17-b668-c40ed1395bf1
2018-01-01 11:49:39.082  INFO 80240 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-01-01 11:49:39.207  INFO 80240 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration' of type [org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration$$EnhancerBySpringCGLIB$$28ea30a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-01-01 11:49:39.223  INFO 80240 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$ec7bffc6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-01-01 11:49:39.676  INFO 80240 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8761 (http)
2018-01-01 11:49:39.692  INFO 80240 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-01-01 11:49:44.484  INFO 80240 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/service-registry/instance-status],methods=[GET]}" onto public org.springframework.http.ResponseEntity org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint.getStatus()
2018-01-01 11:49:44.484  INFO 80240 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/service-registry/instance-status],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint.setStatus(java.lang.String)
2018-01-01 11:49:44.485  INFO 80240 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/features || /features.json],methods=[GET],produces=
2018-01-01 11:49:45.794  INFO 80240 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2018-01-01 11:49:45.794  INFO 80240 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application unknown with eureka with status UP
2018-01-01 11:49:45.856  INFO 80240 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Setting the eureka configuration..
2018-01-01 11:49:45.856  INFO 80240 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka data center value eureka.datacenter is not set, defaulting to default
2018-01-01 11:49:45.856  INFO 80240 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka environment value eureka.environment is not set, defaulting to test
2018-01-01 11:49:45.884  INFO 80240 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : isAws returned false
2018-01-01 11:49:45.885  INFO 80240 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Initialized server context
2018-01-01 11:49:45.886  INFO 80240 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
2018-01-01 11:49:45.886  INFO 80240 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2018-01-01 11:49:45.886  INFO 80240 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2018-01-01 11:49:45.889  INFO 80240 --- [      Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2018-01-01 11:49:45.942  INFO 80240 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8761 (http)
2018-01-01 11:49:45.942  INFO 80240 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761

2.6 Open Eureka Server Dashboard

http://localhost:8761/

Spring boot cloud eureka server dashboard

Spring boot cloud eureka server dashboard

3. References

4. Source Code

Spring boot cloud eureka server example (114 KB)

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *