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>product-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,6 @@
|
||||
package com.ankurm.shop.product;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public record Product(long id, String name, BigDecimal price) {
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.ankurm.shop.product;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@RestController
|
||||
class ProductController {
|
||||
private static final Logger log = LoggerFactory.getLogger(ProductController.class);
|
||||
|
||||
private final Map<Long, Product> products = Map.of(
|
||||
1L, new Product(1, "Mechanical keyboard", new BigDecimal("89.90")),
|
||||
2L, new Product(2, "USB-C dock", new BigDecimal("129.00")),
|
||||
3L, new Product(3, "27-inch monitor", new BigDecimal("249.50")));
|
||||
|
||||
@GetMapping("/products")
|
||||
List<Product> all() {
|
||||
log.info("listing {} products", products.size());
|
||||
return products.values().stream().sorted((a, b) -> Long.compare(a.id(), b.id())).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/products/{id}")
|
||||
Product one(@PathVariable long id) {
|
||||
log.info("looking up product {}", id);
|
||||
Product p = products.get(id);
|
||||
if (p == null) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Product " + id + " not found");
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.ankurm.shop.product;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ProductServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProductServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
spring:
|
||||
application:
|
||||
name: product-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: 8081
|
||||
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