1. Overview

In this example we are going to cover Spring boot cloud eureka client example or configuration. We are assuming that Spring boot cloud eureka server us running on

8761 port. If Eureka server configuration is not configured then here is an article for Eureka server configuration.

Generally, Eureka client application is rest service which exposes REST services. Which will be accessible from direct UI or another Spring boot services.

  • We can also register multiple instances of the same service to the server. In case of multiple instances of the same server requires load balancing.
  • @EnableEurekaClient annotation is used to consider service as a eureka client
  • spring-cloud-starter-eureka must be available in CLASSPATH
  • eureka.client.serviceUrl.defaultZone configuration requires in application.properties or application.yml. Which indicate URL of eureka server in which client application will be registered.
  • spring.application.name in application.properties  will be considered as a name of service which will access services.

2. Example

Spring boot cloud eureka client example

Spring boot cloud eureka client example

2.1 pom.xml

spring-cloud-starter-eureka dependency requires which contains class related to Eureka.

<?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-eureka-client-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Spring boot cloud eureka client 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</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.serviceUrl.defaultZone contains default contains URL of eureka server.

spring.application.name will be considered as application service name.

server.port=8383
spring.application.name=account-service
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/
server.context-path=/account

2.3 EurekaClientConfig

@EnableEurekaClient annotation indicates consider this application as eureka client.

package com.javadeveloperzone;

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

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

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
@EnableEurekaClient         // To enable eureka client
public class EurekaClientConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(EurekaClientConfig.class, args);            // it wil start application
    }
}

2.4 AccountController

package com.javadeveloperzone.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@RestController
public class AccountController {

    @Autowired
    private Environment environment;

    @RequestMapping("/getAccountDetails")
    public String callClient() {
        return "This response from : " + environment.getProperty("server.port");
    }
}

2.5 Demo

If all above configurations are done let build and run Eureka client application which will be automatically registered to Eureka server then we saw as below:

java -jar spring-boot-example-1.0-SNAPSHOT.jar --server.port=8888

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

2018-01-06 20:53:56.653  INFO 95440 --- [           main] c.javadeveloperzone.EurekaClientConfig   : No active profile set, falling back to default profiles: default
2018-01-06 20:54:04.146  INFO 95440 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1515252244146, current=UP, previous=STARTING]
2018-01-06 20:54:04.162  INFO 95440 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_ACCOUNT-SERVICE/localhost:account-service:8383: registering service...
2018-01-06 20:54:04.433  INFO 95440 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_ACCOUNT-SERVICE/localhost:account-service:8383 - registration status: 204
2018-01-06 20:54:04.448  INFO 95440 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8383 (http)
2018-01-06 20:54:04.448  INFO 95440 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8383
2018-01-06 20:54:04.448  INFO 95440 --- [           main] c.javadeveloperzone.EurekaClientConfig   : Started EurekaClientConfig in 15.902 seconds (JVM running for 16.849)
2018-01-06 20:59:03.890  INFO 95440 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

 

Let’s check eureka serverhttp://localhost:8761/

Spring boot cloud eureka client example - client register

Spring boot cloud eureka client example – client register

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 *