0% found this document useful (0 votes)
58 views

t06 Service Discovery

The document provides instructions for setting up service registration and discovery using Netflix's Eureka and Spring. It describes creating a Eureka server using Spring Initializr, and updating an existing REST service to register with the Eureka server and be discoverable. Key steps include generating a Spring Boot project with Eureka dependencies, configuring the Eureka server properties and Application class, adding Eureka client configuration and dependencies to the REST service, and annotating the REST classes with @Component for discovery.

Uploaded by

Brent Liang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

t06 Service Discovery

The document provides instructions for setting up service registration and discovery using Netflix's Eureka and Spring. It describes creating a Eureka server using Spring Initializr, and updating an existing REST service to register with the Eureka server and be discoverable. Key steps include generating a Spring Boot project with Eureka dependencies, configuring the Eureka server properties and Application class, adding Eureka client configuration and dependencies to the REST service, and annotating the REST classes with @Component for discovery.

Uploaded by

Brent Liang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Tutorial 6 – Service

Registration and
Discovery

SOEN 487 - Web Services and Applications


By: Konstantinos Psimoulis
Introduction

For this tutorial, we will do the following two things:

● Create a service discovery server using Netflix’s Eureka and Spring Initializr

● Adjust our existing REST service to be discoverable by the server.

Winter 2021
What is Service Discovery

Service Discovery is the process by which


microservices discover each other over a
network.

Typically, there is a Central Server which takes


the role of the service registry and multiple
microservices, which use the registry as a sort
of relay to communicate with other services.

Winter 2021
Restaurant Service from T03

We will be using the CustomerRest implementations from T03 for this Tutorial, and
updating them to work with Eureka.

Winter 2021
Eureka

Eureka is a REST (Representational State Transfer) based service that is primarily used in the
AWS cloud for locating services for the purpose of load balancing and failover of middle-tier
servers.

Service registries like Eureka enable client-side load balancing and help a lot when deploying
multiple microservices.

Winter 2021
Spring Initializr

To create the project for the Eureka server, we will be using Spring Initializr. This service helps
us generate a Spring Boot maven project for IntelliJ very easily, adjusted to our working
environment.

Here is the link to the service: Link

We are using the Spring framework because Eureka Server is part of the Spring Cloud bundle
and Spring Boot helps make the initial configuration much faster.

Winter 2021
Creating the server

Visit the site for Spring Initializr, select Maven Project, Java as the language and use Spring Boot
2.4.2. Add the project metadata similar to the image on the next page. You will also need to add
the following two dependencies.

● Spring Web

● Eureka Server

Note: For the Java version, use whichever you have installed on your system.

Winter 2021
Creating the server

Winter 2021
Creating the server

After clicking on Generate, a zip folder will be downloaded. Extract the zip and open the
contained folder with IntelliJ. IntelliJ should open it as a maven project so you shouldn’t need to
manually do that.

Edit src/main/resources/application.properties and the Application.java file as follows:

application.properties: server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

Winter 2021
Application Java file
package com.example.discoveryserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DiscoveryServerApplication.class);
}

public static void main(String[] args) throws IOException {


ConfigurableApplicationContext ctx = SpringApplication.run(DiscoveryServerApplication.class, args);
System.out.println("Eureka server running.\nHit enter to stop it...");
System.in.read();
System.exit(0);
}
}
Winter 2021
Running the Server

You can now run the server either through IntelliJ or using maven.

mvn install
mvn spring-boot:run

Winter 2021
Updating the REST Service

We will have to make some changes for the existing REST service to be discoverable by the
eureka server.

First, we will add two new Java files in the rest directory:

● RestaurantApiApplication.java, which will run the client that connects to the server.

● DemoConfig.java, which configures Jersey to work properly with Spring Boot

Winter 2021
RestaurantApiApplication.java
package com.example.rest;

import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.HashMap;
import java.util.Map;

// The @EnableDiscoveryClient tag helps register the service on the Eureka registry
@SpringBootApplication
@EnableDiscoveryClient
public class RestaurantApiApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(RestaurantApiApplication.class);
}
Winter 2021
RestaurantApiApplication.java Part 2
@Bean
public ServletContextInitializer servletInitializer() {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
final ServletRegistration.Dynamic appServlet = servletContext.addServlet("jersey-servlet", new
SpringServlet());
Map<String, String> filterParameters = new HashMap<>();
// Make sure the filter parameters fit with your implementation
filterParameters.put("javax.ws.rs.RestaurantApiApplication", "com.example.rest.DemoConfig");
appServlet.setInitParameters(filterParameters);
appServlet.setLoadOnStartup(2);
appServlet.addMapping("/restaurant/*");
}
};
}

public static void main(String[] args) {


SpringApplication.run(RestaurantApiApplication.class, args);
}

Winter 2021
DemoConfig.java
package com.example.rest;

import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.json.JSONConfiguration;

import javax.ws.rs.ApplicationPath;
import java.util.HashMap;
import java.util.Map;

@ApplicationPath("/")
public class DemoConfig extends PackagesResourceConfig {
private static final Map<String, Object> properties() {
Map<String, Object> result = new HashMap<>();
result.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.sun.jersey;com.example.rest");
// To forward non-Jersey paths to servlet container for Spring Boot actuator endpoints to work.
result.put("com.sun.jersey.config.feature.FilterForwardOn404", "true");
result.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
return result;
}
public DemoConfig() {
super(properties());
}
}

Winter 2021
Add Missing dependencies
You can simply copy the pom file from Tutorial 06 on github and add it to your
project

https://github.com/SOEN487/T06/blob/main/pom.xml

Winter 2021
Application Properties
We need to add an application.properties file to the project, to store some
configuration values for spring boot.

To do this, go to File -> Project Structure, then go to Modules and click on


the Sources tab. Create a new folder in src/main called resources and mark it
as a Resources folder from the top bar.

The image in the following slide will help.

Winter 2021
Application Properties

Winter 2021
Application Properties
From the project navigator, find the folder we just created and create a New ->
Resource Bundle and enter application as the base name.

You can then open the application.properties file and add the following lines:

server.port=8080
spring.application.name=restaurant
management.endpoints.web.exposure.include=*

Winter 2021
Component Annotation
For the Eureka client to detect our existing REST services, we need to
add the @Component annotation from the Spring framework above our
class definitions. You will also need to add an import statement.

import org.springframework.stereotype.Component;

@Component
@Path("/customer")
public class CustomerRest {

Do the same for CustomerJson and CustomerForm

You can then run the RestaurantApiApplication the same way we


ran the server.

Winter 2021
Result
If you visit localhost:8761, you should see the service detected by the eureka
server and you can still use the API as before.

Winter 2021
Eureka Client
Finally, we will create a EurekaClient java class, which will discover the service
we just published, and call a couple of methods, similar to the existing
CustomerClient from T03.

In the com.example.client package of the restaurant-api project, create a class


called ExampleEurekaClient and copy the code from the finished branch of T06.

Winter 2021
Eureka Client
Also, create a file in the resources folder named eureka-client.properties, and
copy the contents from the same file on the finished branch. This file contains the
configuration necessary for a basic eureka client. It should contain the following:

You can now right click on ExampleEurekaClient and run the main() method.

Winter 2021
Eureka Client
Before (Hard coded url):

After: (URL retrieved from eureka server)

Winter 2021
Final Result
The ExampleEurekaClient driver should run and create three customers on our
existing service. If you visit http://localhost:8080/restaurant/customerform, you
should get the following result:

Winter 2021
References

- Eureka Info
- Spring Initializr
- Spring Cloud Logo
- Service Discovery Info

Winter 2021

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy