> ## Documentation Index
> Fetch the complete documentation index at: https://blog.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SpringWolf：Spring Boot异步API文档生成利器

## 背景介绍

在微服务架构中，异步通信是一个不可或缺的组成部分。无论是使用 Apache Kafka、RabbitMQ 还是 AMQP 协议，异步通信都能让系统组件之间实现松耦合和高效交互。然而，随着异步通信的广泛应用，如何维护清晰且实时的文档成为了一个挑战。

就像 REST API 有 OpenAPI(Swagger)一样，异步 API 同样需要一个标准化的文档解决方案。这就是 SpringWolf 诞生的背景。

## AsyncAPI 规范简介

[AsyncAPI](https://www.asyncapi.com/) 是一个开源项目，为异步 API 提供了统一的文档规范。它的设计理念类似于 OpenAPI，但专注于异步通信场景。

主要特点：

* 支持多种消息协议(Kafka、AMQP、MQTT等)
* 提供标准化的消息格式描述
* 支持自动化工具生成

## SpringWolf 核心功能

[SpringWolf](https://github.com/springwolf/springwolf-core) 是一个基于 Spring Boot 的 AsyncAPI 文档自动生成工具，它能够：

* 自动扫描并识别应用中的消息监听器和发布者
* 提供类似 Swagger UI 的可视化界面
* 支持 Kafka、RabbitMQ 等主流消息中间件
* 通过注解实现文档定制

## 快速入门指南

### 1. 添加依赖

```xml theme={null}
<dependency>
    <groupId>io.github.springwolf</groupId>
    <artifactId>springwolf-core</artifactId>
    <version>0.6.0</version>
</dependency>

<dependency>
    <groupId>io.github.springwolf</groupId>
    <artifactId>springwolf-kafka</artifactId>
    <version>0.6.0</version>
</dependency>
```

### 2. 基础配置

```yaml theme={null}
springwolf:
  enabled: true
  docket:
    base-package: com.example.demo
    info:
      title: 示例服务
      version: 1.0.0
      description: SpringWolf 示例服务
  plugin:
    kafka:
      enabled: true
```

### 3. 消息生产者示例

```java theme={null}
@Service
@RequiredArgsConstructor
public class UserEventProducer {
    private final KafkaTemplate<String, UserEvent> kafkaTemplate;
    
    @AsyncPublisher(
        channelName = "user-events",
        description = "用户事件发布"
    )
    public void sendUserEvent(UserEvent event) {
        kafkaTemplate.send("user-events", event.getUserId(), event);
    }
}
```

### 4. 消息消费者示例

```java theme={null}
@Service
@Slf4j
public class UserEventConsumer {
    @AsyncListener(
        channelName = "user-events",
        description = "用户事件处理"
    )
    @KafkaListener(topics = "user-events")
    public void consume(UserEvent event) {
        log.info("接收到事件: {}", event);
    }
}
```

## 进阶特性

### 安全配置

集成 Spring Security 保护文档访问：

```java theme={null}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/springwolf/**").hasRole("ADMIN")
            .and()
            .httpBasic();
    }
}
```

### 协议支持

SpringWolf 支持多种消息协议：

* Kafka：springwolf-kafka
* RabbitMQ：springwolf-amqp
* JMS：springwolf-jms
* Cloud Stream：springwolf-cloud-stream

## 文档访问

启动应用后，可通过以下地址访问文档：

* UI 界面：`http://localhost:8080/springwolf/asyncapi-ui.html`
* JSON 格式：`http://localhost:8080/springwolf/docs`

## 总结

SpringWolf 为 Spring Boot 应用提供了一个强大的异步 API 文档解决方案：

* 自动生成文档，减少维护成本
* 确保文档与代码同步
* 提升团队协作效率
* 简化系统集成流程

## 参考资源

* [SpringWolf GitHub](https://github.com/springwolf/springwolf-core)
* [AsyncAPI 官网](https://www.asyncapi.com/)
* [Spring Kafka 文档](https://spring.io/projects/spring-kafka)
