Giter Site home page Giter Site logo

gyj1278 / gs-service-registration-and-discovery Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-guides/gs-service-registration-and-discovery

0.0 0.0 0.0 1.33 MB

Service Registration and Discovery :: Learn how to register and find services with Eureka

Home Page: https://spring.io/guides/gs/service-registration-and-discovery/

License: Apache License 2.0

Shell 8.09% Java 91.91%

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

This guide walks you through the process of starting and using the Netflix Eureka service registry.

What You Will Build

You will set up 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.

Starting with Spring Initializr

For all Spring applications, you should start with the Spring Initializr. The Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the set up for you.

This guide needs two applications. The first application (the service application) needs only the Eureka Server dependency.

The second application (the client application) needs the Eureka Server and Eureka Discovery Client dependencies.

Note
For convenience, we have provided build files (a pom.xml file and a build.gradle file) at the top of the project (one directory above the service and client directories) that you can use to build both projects at once. We also added the Maven and Gradle wrappers there.

You can use this pre-initialized project (for the service application) or this pre-initialized project (for the client application) and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

To manually initialize the project:

  1. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. Click Dependencies and select Eureka Server for the service application and Eureka Server and Eureka Discovery Client for the client application.

  4. Click Generate.

  5. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.
Note
You can also fork the project from Github and open it in your IDE or other editor.

Start a Eureka Service Registry

You first need a Eureka Service registry. You can use Spring Cloud’s @EnableEurekaServer to stand up a registry with which other applications can communicate. This is a regular Spring Boot application with one annotation (@EnableEurekaServer) added to enable the service registry. The following listing (from eureka-service/src/main/java/com.example.serviceregistrationanddiscoveryservice/ServiceRegistrationAndDiscoveryServiceApplication.java) shows the service application:

link:complete/eureka-service/src/main/java/com/example/serviceregistrationanddiscoveryservice/ServiceRegistrationAndDiscoveryServiceApplication.java[role=include]

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

By default, the registry also tries to register itself, so you need to disable that behavior as well.

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

Add some properties to eureka-service/src/main/resources/application.properties to handle all of these requirements, as the following listing shows:

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

Talking to the Registry

Now that you have started a service registry, you can stand up a client that both registers itself with the registry and uses the Spring Cloud DiscoveryClient abstraction to interrogate the registry for its own host and port. The @EnableDiscoveryClient activates the Netflix Eureka DiscoveryClient implementation. (There are other implementations for other service registries, such as Hashicorp’s Consul or Apache Zookeeper). The following listing (from eureka-client/src/main/java/example/serviceregistrationanddiscoveryclient/ServiceRegistrationAndDiscoveryClientApplication.java) shows the client application:

link:complete/eureka-client/src/main/java/com/example/serviceregistrationanddiscoveryclient/ServiceRegistrationAndDiscoveryClientApplication.java[role=include]

Whatever implementation you choose, you should soon see 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 is found before src/main/resources/application.properties. The following listing shows the bootstrap.properties file:

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 registered ServiceInstance instances at http://localhost:8080/service-instances/a-bootiful-client. See 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 that has loaded, starting the eureka-client.

To run the Eureka service with Maven, run the following command in a terminal window (in the /complete directory):

./mvnw spring-boot:run -pl eureka-service

To run the Eureka client with Maven, run the following command in a terminal window (in the /complete directory):

./mvnw spring-boot:run -pl eureka-client

To run the Eureka service with Gradle, run the following command in a terminal window (in the /complete directory):

./gradlew :eureka-service:bootRun

To run the Eureka client with Gradle, run the following command in a terminal window (in the /complete directory):

./gradlew :eureka-client:bootRun

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. Visit the eureka-client in the browser, at http://localhost:8080/service-instances/a-bootiful-client. There, you should see the ServiceInstance for the eureka-client reflected in the response. If you see an empty <List> element, wait a bit and refresh the page.

Summary

Congratulations! You have 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

Contributors

gregturn avatar cbeams avatar joshlong avatar marcingrzejszczak avatar royclarkson avatar btalbott avatar fhanik avatar spring-operator avatar habuma avatar olgamaciaszek avatar dsyer avatar jimtoy avatar izeye avatar shadowmanos avatar odrotbohm avatar robin850 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.