Skip to content

Distributed Services ​

Tracing ​

Especially in distributed services, tracing is significant to maintain a system. Requests are sent across multiple microservices. Latency, identifying correlating requests and aggregating the date across multiple services is essential for analysis.

  • Spring Cloud Sleuth: Implements distributed tracing in Spring Boot appliactions.
  • Zipkin or Jaeger: Zipkin or Jaeger are distributed systems for visualizing trace data within and between services.

TraceId, spanId gets propagated from one service to another via request headers.

Health check ​

Spring Boot Actuator enables creating custom health checks for monitoring application health. It's rather easy to implement a custom actuator to collect data to show in your monitoring endpoint. Spring Boot has a number of built-in actuator endpoints out of the box. e.g. metrics

java
@Component
public class MyHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        boolean isHealthy = checkSystem();
        if (isHealthy) {
            builder.up().withDetail("Database", "Available");
        } else {
            builder.down().withDetail("Database", "Not Available");
        }
    }
}

Service Discovery ​

With the Eureka framework, it is rather easy to set up a service discovery server and clients.

java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
java
@SpringBootApplication
@EnableEurekaClient
public class AccountsServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(AccountsServiceApplication.class, args);
    }
}
Further information

Link: Service discovery with spring boot

Retryable api calls ​

The @Retryable annotation allows retrying a method call if it fails. If you experience e.g. network timeouts, it can improve the reliability of your service.

java
@Retryable(value = {IOException.class}, maxAttempts = 5, backoff = @Backoff(delay = 2000))
public String fetchData() {
    // API call that might fail
}
  • maxAttemps: Number of attempts before giving up
  • backoff: time between retries

Contact: M_Bergmann AT gmx.at