Registering a client with Eureka

Registering a client with Eureka means that a client provides it's own meta-information, such as hostname with port, health indicator URL, and homepage. Each instance of a service sends heartbeat messages to the Eureka server; if Eureka doesn't receive the heartbeat over a configurable timetable, the instance is normally removed from the registry.

Let's create the main application class annotated with @SpringBootApplication for this client application. By default, Spring Discovery Client doesn't enable, so we have to use either @EnableDiscoveryClient or @EnableEurekaClient to enable it. Here is an example Eureka client:

package com.dineshonjava.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); 
   } 
} 
 
Let's create a REST service to be registered with Eureka server. 
 
package com.dineshonjava.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 to the Dineshonjava from EurekaClient!"; 
    } 
} 

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

spring: 
  application: 
    name: spring-cloud-eureka-client 
 
server: 
  port: 80 
 
eureka: 
  client: 
    serviceUrl: 
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} 
  instance: 
    preferIpAddress: true 

This configuration file has a Spring application name to uniquely identify our client in the list of registered applications, it also has server port 80. But we can let Spring Boot choose a random port for us, because later we are accessing this service with its name, and finally, we have to tell our client where to locate the registry.

Let's run this client application and go to http://localhost:8761 again on the browser. Now you can see the client registration status on the Eureka Dashboard. Let's look at the following screenshot:

 

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. You can set up home-page-url, health-check-url, and status-page-url-path as follows:

spring: 
  application: 
    name: spring-cloud-eureka-client 
 
server: 
  port: 80 
 
eureka: 
  client: 
    service-url: 
      default-zone: ${EUREKA_URI:http://localhost:8761/eureka} 
  instance: 
    prefer-ip-address: true 
    status-page-url-path: https://${eureka.instance.hostName}/info 
    health-check-url: https://${eureka.instance.hostName}/health 
    home-page-url: https://${eureka.instance.hostName}/ 
 

Eureka internally registers these properties for status and homepage and publishes a non-secure URL for status and homepage. In the preceding configuration file, I have overridden those properties explicitly to secure HTTP protocol. The ${eureka.instance.hostName} property will be resolved from a defined hostname under the eureka.instance property. And you can set your hostname at runtime using environment variables, for example, eureka.instance.hostname=${HOST_NAME}.

Let's consume the microservices registered to the Eureka server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.241.250