t06 Service Discovery
t06 Service Discovery
Registration and
Discovery
● Create a service discovery server using Netflix’s Eureka and Spring Initializr
Winter 2021
What is Service Discovery
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.
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.
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);
}
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.
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/*");
}
};
}
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.
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 {
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.
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):
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