Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

vmware-archive/getting-started-guides

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

getting-started-guides is no longer actively maintained by VMware, Inc.


tags: [] --- :toc: :icons: font :source-highlighter: prettify :project_id: gs-service-registry This guide walks you through the process of registering and consuming a simple "hello world" REST service with Eureka on Pivotal Cloud Foundry.

What you’ll build

You’ll deploy the simple "hello world" REST service created at Building a RESTful Web Service to Pivotal Cloud Foundry, register it to a Eureka service instance, and then write a client that discovers and consumes the service.

Enable the discovery client

Starting with the "hello world" REST service from Building a RESTful Web Service, you’ll start by enabling the service for service discovery.

Modify the initial code to enable support for external configuration through the use of the Spring Cloud Pivotal Connector. Modify the Maven POM to replace the Spring Boot parent with the Spring Cloud parent.

pom.xml

link:complete/pom.xml[role=include]

The next thing to do is to add the Spring Cloud Eureka starter and the Spring Cloud Pivotal connector dependencies to the build:

pom.xml

link:complete/greeting-service/pom.xml[role=include]

The Spring Cloud Eureka starter adds the Eureka discovery client to the project so that the service can automatically register itself and periodically renew its registration with a Eureka server. The Spring Cloud Pivotal connector enables the application to connect to the Eureka server as a service that is bound to the application in Cloud Foundry.

Next, you’ll need to enable the discovery client by annotating DemoApplication with @EnableDiscoveryClient.

src/main/java/hello/DemoApplication.java

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

The @EnableDiscoveryClient annotation enables a Eureka discovery client that will register the "hello world" REST service with the bound Eureka service so that other applications can discover and consume it.

By default, the service will be registered in Eureka as "bootstrap". But since this name is too generic to be of any practical use, you’ll want to set the spring.application.name property so that the service will be registered with a meaningful name. You can do that by creating an application.yml file with the following entries:

src/main/resources/application.yml

link:complete/greeting-service/src/main/resources/application.yml[role=include]
Note

For now you must also set the eureka.instance.* properties shown here so that the "hello world" service’s URL will be correct in the Eureka registration. This requirement should go away in future versions of the Spring Cloud Pivotal connector.

That’s all of the changes necessary to enable the "hello world" service for service discovery in Eureka. Next you’ll deploy the service to Pivotal Cloud Foundry and bind it to the Eureka service.

Deploy the service to Cloud Foundry

Before you can deploy the service, you’ll need to build it:

mvn package

Once the application has been built, you can deploy it to Cloud Foundry using the Cloud Foundry command line interface (cf CLI):

cf push greetingsvc -p target/gs-rest-service-0.1.0.jar --random-route

Here the application is given the name "greetingsvc" in Cloud Foundry. You are welcome to name it whatever you wish, but remember the name, because you’ll need it when you bind the application to the Eureka service.

By default, the domain name for the application is determined by the application name. By using --random-route, you’re asking Cloud Foundry to assign a random route. This will prevent domain name collisions with others who are working through this guide.

Next you’ll create an instance of the Eureka service. The Eureka service is known as "p-service-registry" in the service marketplace and only offers a single service plan named "standard". Using the cf CLI, create an instance of the service named "registry":

cf create-service p-service-registry standard registry

Now you can bind the Eureka service instance to the application:

cf bind-service greetingsvc registry
cf restage greetingsvc

Note that it’s important to restage the application after binding the service so that the environment variables pertaining to the service binding are reflected in the application.

At this point you have a simple "hello world" REST service deployed to Cloud Foundry and bound to a Eureka service discovery instance. The REST service is enabled with a discovery client so that it will automatically register itself to the Eureka service. It is now eligible for discovery and consumption. Therefore, the next thing you’ll do is create an application that discovers and consumes the "hello world" service.

Discover and consume the service

For the client of the "hello world" REST service, you’ll build another REST service that discovers the "hello world" service via a bound Eureka service and consumes the /greeting endpoint.

The client service has the same Greeting resource representation class as the "hello world" service itself. The only key difference is that the constructor is annotated with @JsonCreator and its arguments with @JsonProperty so that it can be created from the results of invoking the "hello world" service:

src/main/java/hello/Greeting.java

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

The client’s controller handles a GET request for "/hello" by making a GET request for the "hello world" service’s "/greeting" endpoint and binding the result to a Greeting object. It then returns the content property in the response:

src/main/java/hello/GreetingClientController.java

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

GreetingClientController is injected with a RestTemplate via the constructor. It uses the injected RestTemplate to consume the "hello world" service. What’s significant to notice is that the URL given to getForObject() has a host name of "GreetingService"--the name that the "hello world" service is registered as in Eureka. In this way, the client doesn’t need to know the actual URL of the "hello world" service and only references it by the name it is registered as in Eureka.

The client application also includes a bootstrap class named DemoApplication that is identical to the same class in the "hello world" service application:

src/main/java/hello/DemoApplication.java

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

Finally, you’ll need a build file with the required dependencies:

pom.xml

link:complete/greeting-client/pom.xml[role=include]

Deploy the client to Cloud Foundry

First, build the client application:

mvn package

Then push the client application to Cloud Foundry:

cf push greetingclient -p target/gs-rest-client-0.1.0.jar --random-route

Here, the name of the application is "greetingclient". As with the service application, you can choose any application name you want.

Again, the --random-route parameter tells Cloud Foundry to generate a random domain route for your application. For the client application, you’ll want to take note of the route it creates. Look for a line in the cf CLI output that looks like this:

Binding greetingclient-envious-intima.lion.wild.cf-app.com to greetingclient...

In this case, the application is bound to greetingclient-envious-intima.lion.wild.cf-app.com. You’ll use this domain to make a request to the client later in this guide.

Next, bind the Eureka service instance created earlier to the client application and restage the application to pick up the environment variables pertaining to the binding:

cf bind-service greetingclient registry
cf restage greetingclient

Now you should be able to test out the client application by visiting the path "/hello" at the domain that the application is bound to. Given the domain route from above:

curl http://greetingclient-envious-intima.lion.wild.cf-app.com/hello

The response should be "Hello, World!".

Summary

Congratulations! You have just registered a REST service with a Eureka server in Cloud Foundry and written a client application that discovers and consumes that service.

About

The template for new guides and also the place to request them

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published