3. Logistics Service Mongodb Edition‐ Step‐by‐Step Guide - Wiz-DevTech/prettygirllz GitHub Wiki

Step-by-Step Guide: Logistics Service Implementation & Testing

1. Environment Setup 🛠️

1.1 Install Core Dependencies

# Verify Java & Maven
java -version  # Requires 17+
mvn -v        # Requires 3.9+

# Install Docker
sudo apt-get update && sudo apt-get install docker.io docker-compose  # Linux
brew install docker docker-compose  # Mac

1.2 Clone Repository

git clone https://github.com/your-org/logistics-service.git
cd logistics-service

2. Infrastructure Deployment 🐳

2.1 Start Containers

docker-compose up -d mongodb kafka redis

2.2 Verify Services

# Check MongoDB
docker exec -it mongodb mongosh --eval "db.runCommand({ping:1})"

# Check Kafka
docker exec -it kafka kafka-topics --list --bootstrap-server localhost:9092

# Check Redis
docker exec -it redis redis-cli PING

3. Service Configuration ⚙️

3.1 Create Environment File

cat <<EOF > .env
SPRING_DATA_MONGODB_URI=mongodb://localhost:27017/logistics
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
FRAUD_THRESHOLD=0.85
EOF

3.2 Initialize MongoDB

docker exec -it mongodb mongosh --eval "
  use logistics;
  db.createCollection('deliveries');
  db.createCollection('fraud_blacklist');
"

4. Build & Run ▶️

4.1 Build with Maven

mvn clean install -DskipTests

4.2 Run Service

mvn spring-boot:run -Dspring-boot.run.profiles=dev

Verify Startup:

2024-02-20T10:00:00 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port 8080

5. Testing Procedures 🧪

5.1 Unit Tests

mvn test

Key Test Classes:

  • DeliveryOrchestratorServiceUnitTest
  • FraudDetectionServiceTest

5.2 Integration Tests

mvn verify -Pintegration-test

Test Coverage:

  • MongoDB repository operations
  • Kafka event publishing/consumption
  • Redis cache interactions

5.3 Manual API Testing

Create Test Delivery:

curl -X POST http://localhost:8080/api/v1/deliveries \
  -H "Content-Type: application/json" \
  -d '{
    "orderId": "test_ord_1",
    "destinationType": "HOME",
    "recipient": {
      "phone": "+15551234567"
    }
  }'

Force Error Scenario:

curl -X POST http://localhost:8080/api/v1/deliveries \
  -H "X-TEST-MODE: FAIL_HDS" \
  -d '{"orderId": "fail_ord_1", "destinationType": "HOME"}'

6. Verification Steps ✔️

6.1 Check MongoDB Data

docker exec -it mongodb mongosh logistics --eval "
  db.deliveries.find().pretty();
  db.fraud_blacklist.find().pretty();
"

6.2 Monitor Kafka Events

docker exec -it kafka kafka-console-consumer \
  --bootstrap-server localhost:9092 \
  --topic delivery-events \
  --from-beginning

6.3 Validate Redis Cache

docker exec -it redis redis-cli KEYS "*delivery*"

7. Troubleshooting 🚨

Common Issues Table:

Symptom Diagnostic Command Solution
MongoDB connection refused docker logs mongodb Check .env URI
Kafka consumer lag docker exec kafka kafka-consumer-groups --describe Increase partitions
High API latency curl -o /dev/null -s -w '%{time_total}' http://localhost:8080/actuator/health Check Redis connection pool

8. Cleanup 🧹

# Stop containers
docker-compose down

# Remove test data
docker exec -it mongodb mongosh logistics --eval "db.dropDatabase()"

Next Steps ➡️

  • Set up CI/CD pipeline
  • Configure Prometheus monitoring
  • Implement chaos engineering tests

Pro Tip: Use the included test-data-generator.sh for load testing:

./scripts/test-data-generator.sh --orders 100 --threads 5

This guide ensures your Logistics Service is properly implemented and ready for integration with other microservices. For advanced scenarios, refer to the Integration Handbook.

⚠️ **GitHub.com Fallback** ⚠️