Commit 1c5443d5 authored by Ali Saeed's avatar Ali Saeed

Using micrometer to trace services and visualize with zipkin

parent 2cf8dd3a
......@@ -46,6 +46,18 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
......
......@@ -24,6 +24,9 @@ spring:
args:
name: usersCircuitBreaker
fallbackUri: forward:/users-fallback
zipkin:
baseurl: http://localhost:9411
eureka:
client:
serviceUrl:
......@@ -46,6 +49,9 @@ management:
endpoint:
health:
show-details: always
tracing:
sampling:
probability: 1.0
resilience4j:
circuitbreaker:
......@@ -53,10 +59,15 @@ resilience4j:
usersCircuitBreaker:
registerHealthIndicator: true
eventConsumerBufferSize: 10
failureRateThreshold: 80
minimumNumberOfCalls: 10
failureRateThreshold: 50
minimumNumberOfCalls: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 10s
permittedNumberOfCallsInHalfOpenState: 3
slidingWindowSize: 10
slidingWindowType: COUNT_BASED
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging:
pattern:
level: ${spring.application.name:},%X{traceId:-},%X{spanId:-}
......@@ -24,6 +24,9 @@ spring:
args:
name: usersCircuitBreaker
fallbackUri: forward:/users-fallback
zipkin:
baseurl: http://localhost:9411
eureka:
client:
serviceUrl:
......@@ -46,6 +49,9 @@ management:
endpoint:
health:
show-details: always
tracing:
sampling:
probability: 1.0
resilience4j:
circuitbreaker:
......@@ -53,10 +59,15 @@ resilience4j:
usersCircuitBreaker:
registerHealthIndicator: true
eventConsumerBufferSize: 10
failureRateThreshold: 80
minimumNumberOfCalls: 10
failureRateThreshold: 50
minimumNumberOfCalls: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 10s
permittedNumberOfCallsInHalfOpenState: 3
slidingWindowSize: 10
slidingWindowType: COUNT_BASED
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging:
pattern:
level: ${spring.application.name:},%X{traceId:-},%X{spanId:-}
version: "3"
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.3.3
container_name: elasticsearch
......
......@@ -6,7 +6,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>invoicing-service</artifactId>
......@@ -40,6 +40,22 @@
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
......
......@@ -13,11 +13,7 @@ public class InvoicingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(InvoicingServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package org.example.configuration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
}
spring.application.name=invoicing-service
server.port=8081
eureka.client.serviceUrl.defaultZone= ${EUREKA_URL:http://localhost:8761/eureka/}
eureka.instance.metadataMap.zone=damascus
\ No newline at end of file
eureka.instance.metadataMap.zone=damascus
#Zipkin
spring.zipkin.baseurl= localhost:9411
# All traces should be sent to latency analysis tool, default sampling probability is 10%
management.tracing.sampling.probability=1.0
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
\ No newline at end of file
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/damascus/invoicing-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/invoicing.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
......
spring.application.name=invoicing-service
server.port=8081
eureka.client.serviceUrl.defaultZone= ${EUREKA_URL:http://localhost:8761/eureka/}
eureka.instance.metadataMap.zone=damascus
\ No newline at end of file
eureka.instance.metadataMap.zone=damascus
#Zipkin
spring.zipkin.baseurl= localhost:9411
# All traces should be sent to latency analysis tool, default sampling probability is 10%
management.tracing.sampling.probability=1.0
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
\ No newline at end of file
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/damascus/invoicing-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/invoicing.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Console appender -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
......@@ -18,8 +21,39 @@
<appender-ref ref="CONSOLE"/> <!-- Add the console appender reference -->
</logger>
<root level="error">
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:5000</destination>
<encoder
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc />
<context />
<logLevel />
<loggerName />
<pattern>
<pattern>
{
"appName": "invoicing-service"
}
</pattern>
</pattern>
<threadName />
<message />
<logstashMarkers />
<stackTrace />
</providers>
</encoder>
</appender>
<logger name="org.example" level="DEBUG" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/> <!-- Add the console appender reference -->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="STASH" />
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="STASH" />
</root>
</configuration>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -42,6 +42,22 @@
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
......
......@@ -3,9 +3,7 @@ package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
......@@ -13,11 +11,6 @@ public class ReplicaInvoicingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ReplicaInvoicingServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package org.example.configuration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
}
\ No newline at end of file
spring.application.name=invoicing-service
server.port=8082
eureka.client.serviceUrl.defaultZone= ${EUREKA_URL:http://localhost:8761/eureka/}
eureka.instance.metadataMap.zone=damascus
\ No newline at end of file
eureka.instance.metadataMap.zone=damascus
#Zipkin
spring.zipkin.baseurl= localhost:9411
# All traces should be sent to latency analysis tool, default sampling probability is 10%
management.tracing.sampling.probability=1.0
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/damascus/replica-invoicing-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/rep-invoicing.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
......
spring.application.name=invoicing-service
server.port=8082
eureka.client.serviceUrl.defaultZone= ${EUREKA_URL:http://localhost:8761/eureka/}
eureka.instance.metadataMap.zone=damascus
\ No newline at end of file
eureka.instance.metadataMap.zone=damascus
#Zipkin
spring.zipkin.baseurl= localhost:9411
# All traces should be sent to latency analysis tool, default sampling probability is 10%
management.tracing.sampling.probability=1.0
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/damascus/replica-invoicing-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/rep-invoicing.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Console appender -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:5000</destination>
<encoder
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc />
<context />
<logLevel />
<loggerName />
<pattern>
<pattern>
{
"appName": "replica-invoicing-service"
}
</pattern>
</pattern>
<threadName />
<message />
<logstashMarkers />
<stackTrace />
</providers>
</encoder>
</appender>
<logger name="org.example" level="DEBUG" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/> <!-- Add the console appender reference -->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="STASH" />
</logger>
<root level="error">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/> <!-- Add the console appender reference -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="STASH" />
</root>
</configuration>
\ No newline at end of file
......@@ -12064,3 +12064,7 @@ java.lang.IllegalStateException: Client instance has been closed.
2024-05-02 18:51:46.940 [main] DEBUG o.e.ServiceDiscoveryApplication - Running with Spring Boot v3.0.1, Spring v6.0.3
2024-05-02 18:51:46.946 [main] INFO o.e.ServiceDiscoveryApplication - No active profile set, falling back to 1 default profile: "default"
2024-05-02 18:52:00.514 [main] INFO o.e.ServiceDiscoveryApplication - Started ServiceDiscoveryApplication in 16.17 seconds (process running for 17.726)
2024-05-03 00:29:21.262 [main] INFO o.e.ServiceDiscoveryApplication - Starting ServiceDiscoveryApplication using Java 17 with PID 14252 (D:\Second Semester\Advanced DS\Practical\New folder\micro-services\service-discovery\target\classes started by dell in D:\Second Semester\Advanced DS\Practical\New folder\micro-services)
2024-05-03 00:29:21.354 [main] DEBUG o.e.ServiceDiscoveryApplication - Running with Spring Boot v3.0.1, Spring v6.0.3
2024-05-03 00:29:21.357 [main] INFO o.e.ServiceDiscoveryApplication - No active profile set, falling back to 1 default profile: "default"
2024-05-03 00:29:51.890 [main] INFO o.e.ServiceDiscoveryApplication - Started ServiceDiscoveryApplication in 34.355 seconds (process running for 37.77)
......@@ -53,6 +53,18 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
......
......@@ -13,9 +13,5 @@ public class UsersServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UsersServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package org.example.configuration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.build();
}
}
\ No newline at end of file
spring.application.name=users-service
server.port=8080
eureka.client.serviceUrl.defaultZone= ${EUREKA_URL:http://localhost:8761/eureka/}
eureka.instance.metadataMap.zone=damascus
\ No newline at end of file
eureka.instance.metadataMap.zone=damascus
#Zipkin
spring.zipkin.baseurl= localhost:9411
# All traces should be sent to latency analysis tool, default sampling probability is 10%
management.tracing.sampling.probability=1.0
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
\ No newline at end of file
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/damascus/users-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/users.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
......
spring.application.name=users-service
server.port=8080
eureka.client.serviceUrl.defaultZone= ${EUREKA_URL:http://localhost:8761/eureka/}
eureka.instance.metadataMap.zone=damascus
\ No newline at end of file
eureka.instance.metadataMap.zone=damascus
#Zipkin
spring.zipkin.baseurl= localhost:9411
# All traces should be sent to latency analysis tool, default sampling probability is 10%
management.tracing.sampling.probability=1.0
## traceID and spanId are predefined MDC keys - we want the logs to include them
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
\ No newline at end of file
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/damascus/users-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/users.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>10</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Console appender -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:5000</destination>
<encoder
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc />
<context />
<logLevel />
<loggerName />
<pattern>
<pattern>
{
"appName": "users-service"
}
</pattern>
</pattern>
<threadName />
<message />
<logstashMarkers />
<stackTrace />
</providers>
</encoder>
</appender>
<logger name="org.example" level="DEBUG" additivity="false">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/> <!-- Add the console appender reference -->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="STASH" />
</logger>
<root level="error">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/> <!-- Add the console appender reference -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="STASH" />
</root>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment