Code Monkey home page Code Monkey logo

gs-service-registration-and-discovery's Introduction

tags projects
spring-cloud
eureka
spring-cloud
spring-cloud-netflix

This guide walks you through the process of standing up and consuming the Netflix Eureka service registry.om

What you’ll build

You’ll setup a Netflix Eureka service registry and then build a client that both registers itself with the registry and uses it to resolve its own host. A service registry is useful because it enables client-side load-balancing and decouples service providers from consumers without the need for DNS.

Build with Gradle

Build with Gradle

eureka-service/build.gradle

link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-service/build.gradle[role=include]

eureka-client/build.gradle

link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-client/build.gradle[role=include]

Build with Maven

Build with Maven

eureka-service/pom.xml

link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-service/pom.xml[role=include]

eureka-client/pom.xml

link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-client/pom.xml[role=include]

Stand up a Eureka Service Registry

You’ll first need a Eureka Service registry. You can use Spring Cloud’s @EnableEurekaServer to standup a registry that other applications can talk to. This is a regular Spring Boot application with one annotation added to enable the service registry.

eureka-service/src/main/java/hello/EurekaServiceApplication.java

link:complete/eureka-service/src/main/java/hello/EurekaServiceApplication.java[role=include]

When the registry starts up it will complain, with a stacktrace, that there are no replica nodes for the registry to connect to. In a production environment, you will want more than one instance of the registry. For our simple purposes, however, it sufficies to disable the relevant logging.

By default, the registry will also attempt to register itself, so you’ll need to disable that, as well.

It’s a good convention to put this registry on a separate port when using it locally.

Add some properties to your eureka-service/src/main/resources/application.properties to handle all of these requirements.

eureka-service/src/main/resources/application.properties

link:complete/eureka-service/src/main/resources/application.properties[role=include]

Talking to the Registry

Now that we’ve stood up a service registry, let’s stand up a client that both registers itself with the registry and uses the Spring Cloud DiscoveryClient abstraction to interrogate the registry for it’s own host and port. The @EnableDiscoveryClient activates the Netflix Eureka DiscoveryClient implementation. There are other implementations for other service registries like Hashicorp’s Consul or Apache Zookeeper.

eureka-client/src/main/java/hello/EurekaClientApplication.java

link:complete/eureka-client/src/main/java/hello/EurekaClientApplication.java[role=include]

Whatever implementation you choose, you’ll soon see the eureka-client registered under whatever name you specify in the spring.application.name property. This property is used a lot in Spring Cloud, often in the earliest phases of a service’s configuration. This property is used in service bootstrap and so by convention lives in eureka-client/src/main/resources/bootstrap.properties where it’s found before src/main/resources/application.properties.

eureka-client/src/main/resources/bootstrap.properties

link:complete/eureka-client/src/main/resources/bootstrap.properties[role=include]

The eureka-client defines a Spring MVC REST endpoint, ServiceInstanceRestController, that returns an enumeration of all the ServiceInstance instances registered in the registry at http://localhost:8080/service-instances/a-bootiful-client. Consult the Building a RESTful Web Service guide to learn more about building REST services with Spring MVC and Spring Boot.

Test the application

Test the end-to-end result by starting the eureka-service first and then, once loaded, starting the eureka-client. The eureka-client will take about a minute to register itself in the registry and to refresh its own list of registered instances from the registry. All of these thresholds are configurable. Visit the eureka-client in the browser, http://localhost:8080/service-instances/a-bootiful-client. There, you should see the ServiceInstance for the eureka-client reflected in the response.

Summary

Congratulations! You’ve just used Spring to stand up a Netflix Eureka service registry and to use that registry in a client application.

gs-service-registration-and-discovery's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.