Add spring-boot-microservices-project: config server, Eureka, gateway and three services on Boot 4.1.1 + Spring Cloud 2025.1.3
Product, order and inventory services behind Spring Cloud Gateway (WebMVC), configuration from a Config Server, discovery with Eureka, a Resilience4j circuit breaker on order -> inventory and Brave tracing. Run as six plain jars; output/ holds real transcripts (config, registry, requests, trace propagation, circuit breaker, startup race and the load-balanced RestClient.Builder trap). Dockerfile and docker-compose.yml are included but were not run. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HhW8LD8r3EigoBaCGrQZx5
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.ankurm.shop</groupId>
|
||||
<artifactId>shop</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>inventory-service</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-micrometer-tracing-brave</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-tracing-bridge-brave</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.ankurm.shop.inventory;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@RestController
|
||||
class InventoryController {
|
||||
private static final Logger log = LoggerFactory.getLogger(InventoryController.class);
|
||||
|
||||
record ReserveRequest(int quantity) {
|
||||
}
|
||||
|
||||
private final InventoryProperties properties;
|
||||
private final Map<Long, AtomicInteger> stock = new ConcurrentHashMap<>(Map.of(
|
||||
1L, new AtomicInteger(20), 2L, new AtomicInteger(8), 3L, new AtomicInteger(3)));
|
||||
private final AtomicBoolean down = new AtomicBoolean(false);
|
||||
private final AtomicInteger reserveCalls = new AtomicInteger();
|
||||
|
||||
InventoryController(InventoryProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@GetMapping("/inventory/{productId}")
|
||||
Map<String, Object> level(@PathVariable long productId) {
|
||||
AtomicInteger s = stock.get(productId);
|
||||
if (s == null) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No stock record for product " + productId);
|
||||
}
|
||||
return view(productId, s.get());
|
||||
}
|
||||
|
||||
@PostMapping("/inventory/{productId}/reserve")
|
||||
Map<String, Object> reserve(@PathVariable long productId, @RequestBody ReserveRequest request) {
|
||||
reserveCalls.incrementAndGet();
|
||||
if (down.get()) {
|
||||
log.warn("simulating an outage: refusing to reserve product {}", productId);
|
||||
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "inventory is down");
|
||||
}
|
||||
AtomicInteger s = stock.get(productId);
|
||||
if (s == null) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No stock record for product " + productId);
|
||||
}
|
||||
int remaining = s.addAndGet(-request.quantity());
|
||||
if (remaining < 0) {
|
||||
s.addAndGet(request.quantity());
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "Only " + s.get() + " of product " + productId + " in stock");
|
||||
}
|
||||
log.info("reserved {} of product {}, {} left", request.quantity(), productId, remaining);
|
||||
return view(productId, remaining);
|
||||
}
|
||||
|
||||
/** Test switch: POST /inventory/chaos?down=true makes every reserve call fail with 503. */
|
||||
@PostMapping("/inventory/chaos")
|
||||
Map<String, Object> chaos(@RequestParam boolean down) {
|
||||
this.down.set(down);
|
||||
log.info("outage simulation is now {}", down ? "ON" : "OFF");
|
||||
return Map.of("down", down);
|
||||
}
|
||||
|
||||
/** How many reserve calls actually reached this service (a circuit-breaker test needs this). */
|
||||
@GetMapping("/inventory/stats")
|
||||
Map<String, Object> stats() {
|
||||
return Map.of("reserveCalls", reserveCalls.get());
|
||||
}
|
||||
|
||||
private Map<String, Object> view(long productId, int quantity) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("productId", productId);
|
||||
m.put("quantity", quantity);
|
||||
m.put("lowStock", quantity <= properties.lowStockThreshold());
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ankurm.shop.inventory;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/** Bound from shop.inventory.* , which comes from the config server (inventory-service.yml). */
|
||||
@ConfigurationProperties("shop.inventory")
|
||||
record InventoryProperties(int lowStockThreshold) {
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.ankurm.shop.inventory;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ConfigurationPropertiesScan
|
||||
public class InventoryServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(InventoryServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
spring:
|
||||
application:
|
||||
name: inventory-service
|
||||
config:
|
||||
# Fail fast if the config server is not up: a service with the wrong config is worse than no service.
|
||||
import: "configserver:${CONFIG_URL:http://localhost:8888}"
|
||||
server:
|
||||
port: 8083
|
||||
eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: ${EUREKA_URL:http://localhost:8761/eureka}
|
||||
instance:
|
||||
hostname: ${EUREKA_HOSTNAME:localhost}
|
||||
# Register the service name + port as the instance id so two copies of one service do not collide.
|
||||
instance-id: ${spring.application.name}:${server.port}
|
||||
Reference in New Issue
Block a user