# Java Microservices

> Build microservices - Spring Cloud, service mesh, event-driven, resilience patterns

- **Type:** Skill
- **Install:** `agentstack add skill-linlannet-agent-skills-java-microservices`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [linlannet](https://agentstack.voostack.com/s/linlannet)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** Apache-2.0
- **Upstream author:** [linlannet](https://github.com/linlannet)
- **Source:** https://github.com/linlannet/agent-skills/tree/main/skills/java-microservices

## Install

```sh
agentstack add skill-linlannet-agent-skills-java-microservices
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Java 微服务技能

使用 Spring Cloud 和分布式系统模式构建生产级微服务。

## 概述

本技能涵盖使用 Spring Cloud 的微服务架构，包括服务发现、API 网关、断路器、事件驱动通信和分布式追踪。

## 何时使用此技能

当您需要以下操作时使用：
- 设计微服务架构
- 实现服务间通信
- 配置弹性模式
- 设置事件驱动消息传递
- 添加分布式追踪

## 涵盖主题

### Spring Cloud 组件
- Config Server（集中配置）
- Service Discovery（服务发现，Eureka、Consul）
- API Gateway（API 网关，Spring Cloud Gateway）
- Load Balancing（负载均衡，Spring Cloud LoadBalancer）

### 弹性模式
- Circuit Breaker（断路器，Resilience4j）
- 带退避的重试
- 舱壁隔离
- 速率限制

### 事件驱动架构
- Apache Kafka 集成
- Spring Cloud Stream
- Saga 模式
- 事件溯源基础

### 可观测性
- 分布式追踪（Micrometer）
- 指标（Prometheus）
- 日志关联

## 快速参考

```java
// Saga with Choreography
@Component
public class OrderSagaListener {

    @KafkaListener(topics = "order.created")
    public void handleOrderCreated(OrderCreatedEvent event) {
        inventoryService.reserve(event.getItems());
    }

    @KafkaListener(topics = "payment.failed")
    public void handlePaymentFailed(PaymentFailedEvent event) {
        // Compensating transaction
        inventoryService.release(event.getOrderId());
        orderService.cancel(event.getOrderId());
    }
}

// Circuit Breaker Configuration
@Configuration
public class ResilienceConfig {

    @Bean
    public Customizer cbCustomizer() {
        return factory -> factory.configureDefault(id ->
            new Resilience4JConfigBuilder(id)
                .circuitBreakerConfig(CircuitBreakerConfig.custom()
                    .failureRateThreshold(50)
                    .waitDurationInOpenState(Duration.ofSeconds(30))
                    .slidingWindowSize(10)
                    .build())
                .build());
    }
}

// API Gateway Routes
@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes()
            .route("orders", r -> r
                .path("/api/orders/**")
                .filters(f -> f
                    .stripPrefix(1)
                    .circuitBreaker(c -> c.setName("order-cb"))
                    .retry(retry -> retry.setRetries(3)))
                .uri("lb://order-service"))
            .build();
    }
}
```

## 可观测性配置

```yaml
management:
  tracing:
    sampling:
      probability: 1.0
  endpoints:
    web:
      exposure:
        include: health,info,metrics,prometheus

logging:
  pattern:
    level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
```

## 常见模式

### Saga 模式
```
Order → Inventory → Payment → (Success | Compensate)
```

### 断路器状态
```
CLOSED → (failures exceed threshold) → OPEN
OPEN → (wait duration) → HALF_OPEN
HALF_OPEN → (success) → CLOSED
HALF_OPEN → (failure) → OPEN
```

## 故障排除

### 常见问题

| 问题 | 原因 | 解决方案 |
|------|------|----------|
| 级联失败 | 无断路器 | 添加 Resilience4j |
| 消息丢失 | 无确认 | 启用手动确认 |
| 数据不一致 | 无补偿 | 实现 saga |
| 服务未找到 | 发现延迟 | 调整心跳 |

### 调试清单
```
□ 追踪请求 (traceId)
□ 检查断路器状态
□ 验证 Kafka 消费者滞后
□ 审查网关路由
□ 监控重试次数
```

## 使用方法

```
Skill("java-microservices")
```

## 相关技能
- `java-spring-boot` - Spring Cloud
- `java-docker` - 容器化

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [linlannet](https://github.com/linlannet)
- **Source:** [linlannet/agent-skills](https://github.com/linlannet/agent-skills)
- **License:** Apache-2.0

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-linlannet-agent-skills-java-microservices
- Seller: https://agentstack.voostack.com/s/linlannet
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
