mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-08 20:20:09 +00:00
feat: Distributed tracing (#3006)
* added microservices distributed tracing pattern * feat: Implement Microservice pattern: Distributed tracing #2693
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.payment.microservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* With the Microservices pattern, a request often travels through multiple different microservices.
|
||||
* Tracking the entire request flow across these services can be challenging, especially when trying
|
||||
* to diagnose performance issues or failures. Distributed tracing addresses this challenge by
|
||||
* providing end-to-end visibility into the lifecycle of a request as it passes through various
|
||||
* microservices.
|
||||
*
|
||||
* <p>The intent of the Distributed Tracing pattern is to trace a request across different
|
||||
* microservices, collecting detailed timing data and logs that help in understanding the flow,
|
||||
* performance bottlenecks, and failure points in a distributed system. Each microservice involved
|
||||
* in the request contributes to the tracing data, creating a comprehensive view of the request's
|
||||
* journey.
|
||||
*
|
||||
* <p>This implementation demonstrates distributed tracing in a microservices architecture for an
|
||||
* e-commerce platform. When a customer places an order, the OrderService interacts with
|
||||
* both the PaymentService to process the payment and the ProductService to check the
|
||||
* product inventory. Tracing logs are generated for each interaction, and these logs can be
|
||||
* visualized using Zipkin.
|
||||
*
|
||||
* <p>To run Zipkin and view the tracing logs, you can use the following Docker command:
|
||||
*
|
||||
* <pre>
|
||||
* {@code docker run -d -p 9411:9411 --name zipkin openzipkin/zipkin }
|
||||
* </pre>
|
||||
*
|
||||
* <p>Start Zipkin with the command above. Once Zipkin is running, you can
|
||||
* access the Zipkin UI at <a href="http://localhost:9411">http://localhost:9411</a>
|
||||
* to view the tracing logs and analyze the request flows across your microservices.
|
||||
*
|
||||
* <p>To place an order and generate tracing data, you can use the following curl command:
|
||||
*
|
||||
* <pre>
|
||||
* {@code curl -X POST http://localhost:30300/order -H "Content-Type: application/json" -d '{"orderId": "123"}' }
|
||||
* </pre>
|
||||
*
|
||||
* <p>This command sends a POST request to create an order, which will trigger interactions with the
|
||||
* payment and product microservices, generating tracing logs that can be viewed in Zipkin.
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class, args);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.payment.microservice;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Controller for handling payment processing requests.
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class PaymentController {
|
||||
|
||||
/**
|
||||
* Processes the payment based on the request.
|
||||
*
|
||||
* @param request the request body containing payment information (can be null)
|
||||
* @return ResponseEntity containing the payment processing result (true)
|
||||
*/
|
||||
@PostMapping("/payment/process")
|
||||
public ResponseEntity<Boolean> payment(@RequestBody(required = false) String request) {
|
||||
LOGGER.info("Received payment request: {}", request);
|
||||
boolean result = true;
|
||||
LOGGER.info("Payment result: {}", result);
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
#
|
||||
# The MIT License
|
||||
# Copyright © 2014-2021 Ilkka Seppälä
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
#
|
||||
|
||||
spring.application.name=payment-microservice
|
||||
server.port=30301
|
||||
|
||||
management.tracing.sampling.probability=1
|
||||
logging.pattern.level=%5p [${spring.zipkin.service.name:${spring.application.name:}},%X{traceId:-},%X{spanId:-}]
|
||||
management.tracing.propagation.type=w3c
|
||||
management.tracing.baggage.enabled=true
|
||||
management.tracing.enabled=true
|
||||
management.zipkin.tracing.endpoint=http://localhost:9411/api/v2/spans
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.payment.microservice;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Context loads test
|
||||
*/
|
||||
class MainTest {
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> Main.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.payment.microservice;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Payment controller test.
|
||||
*/
|
||||
class ProductControllerTest {
|
||||
|
||||
private PaymentController paymentController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
paymentController = new PaymentController();
|
||||
}
|
||||
/**
|
||||
* Test to process the payment.
|
||||
*/
|
||||
@Test
|
||||
void testValidateProduct() {
|
||||
// Arrange
|
||||
String request = "Sample payment process request";
|
||||
// Act
|
||||
ResponseEntity<Boolean> response = paymentController.payment(request);
|
||||
// Assert
|
||||
assertEquals(ResponseEntity.ok(true), response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to process the payment with null request.
|
||||
*/
|
||||
@Test
|
||||
void testValidateProductWithNullRequest() {
|
||||
// Arrange
|
||||
// Act
|
||||
ResponseEntity<Boolean> response = paymentController.payment(null);
|
||||
// Assert
|
||||
assertEquals(ResponseEntity.ok(true), response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user