Implementing Service Discovery – Eureka clients

Project setup

cd ~
spring init -d=cloud-eureka,web -n=eureka-client eureka-client
Using service at https://start.spring.io
Project extracted to '/home/hadoop/eureka-client'

Registering a client with Eureka

cd ~/eureka-client

cat << EOF > src/main/java/com/example/eurekaclient/EurekaClientApplication.java
package com.example.eurekaclient;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

Let's create a REST service to be registered with Eureka server.

cat << EOF > src/main/java/com/example/eurekaclient/HelloController.java
package com.example.eurekaclient;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

   @GetMapping("/hello")
    public String greeting() {
        return "Hello from Admatic from EurekaClient!";
    }
}
EOF

Now let's create an application configuration file for this client application, in the form of an application.yml file, as follows:

cat << EOF > src/main/resources/application.yml
spring:
  application:
    name: spring-cloud-eureka-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true
EOF

Run the Eureka Client

mvn spring-boot:run

Go to http://localhost:8761 again on the browser.

As you can see, now it has one registered instance of a REST service. The registered service name is SPRING-CLOUD-EUREKA-CLIENT as we have given the application name in the configuration file.

results matching ""

    No results matching ""