Creating the example services

Let's create our first service. We will create the service using Spring initializer (http://start.spring.io/).

The following screenshot shows the start.spring.io interface where we are creating the service:

We can import this service to the IDE as a Maven project. We can see a Spring Boot application is created.

We have used Actuator and Devtools dependencies for generating. Here are some sample dependencies you might see in pom.xml:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

The preceding code gives us an autogenerated class, as follows: 

@SpringBootApplication
public class ServiceoneApplication
{
public static void main(String[] args)
{
SpringApplication.run(ServiceoneApplication.class, args);
}
}

Next, we create a simple controller:


@RestController

public class HelloController
{
@GetMapping("/sayhello")
public String sayHello()
{
return("Hello");
}
}

We can run the application and access the service at http://localhost:8080/sayhello.

Now, we will add a couple more services. We will use the same approach as before to create a Spring Boot application. We will create two additional projects to implement the hello2 and hello3 Microservices.

Starting from service 3, we will make a simple service returning a string for the sake of this example, as follows:

@RestController
public class HelloController
{
@GetMapping("/sayhello3")
public String sayHello()
{
return("Hello from service 3.");
}
}

service 2 will call service 3 and add its own input along with that, as follows:

@RestController
public class HelloController
{
private final RestTemplate restTemplate;
public HelloController(RestTemplate restTemplate)
{
this.restTemplate = restTemplate;
}
@GetMapping("/sayhello2")
public String sayHello()
{
String responseFromService = restTemplate.getForObject("http://localhost:8082/sayhello3", String.class);
return("Hello from service 2. " + responseFromService);
}
}

Similarly, our main service will call service 2:

@RestController
public class HelloController
{
private final RestTemplate restTemplate;
public HelloController(RestTemplate restTemplate)
{
this.restTemplate = restTemplate;
}
@GetMapping("/sayhello")
public String sayHello()
{
String responseFromService = restTemplate.getForObject("http://localhost:8081/sayhello2", String.class);
return("Hello from service 1. " + responseFromService);
}
}

Now, let's try calling our service:

curl http://localhost:8080/sayhello

This will return the Hello from service 1. Hello from service 2. Hello from service 3 string.

If one of the services is responding slowly, or has stopped responding, we will get a delayed response or no response at all. For example, let's stop service 3 and try calling the main service.

We receive {"timestamp":"2018-07-22T12:47:54.792+0000","status":500,"error":"Internal Server Error","message":"500 null","path":"/sayhello"} as a response.

Looking at this response, we cannot make any sense out of it. This only says that the service responded with an error, but if the main service or any other service,is failing we cannot make any sense out of the response.

Similarly, if one of the services is responding slowly, we will not know what is causing the delay. Let's modify the service:

@GetMapping("/sayhello3")
public String sayHello()
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return("Hello from service 3.");
}

We can see the main service has been responding with a delay of two seconds, but we do not know why.

We have seen the problems that can occur with Microservices and we will be clueless without proper monitoring implemented. There are multiple ways to implement monitoring and tracing. We can add manual code snippets as logs and monitor logs later, we can use aspect-oriented programming (AOP) to generate logs, or we can use any of the tools available to implement tracing and monitoring.

There are a lot of tools available to implement the monitoring of services; for this chapter, we will focus more on the approach and the reader can use the tool of their choice. We will try to understand how to implement monitoring using certain tools. Let's take a look at Zipkin to implement monitoring and tracing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.91.47