@llryiop/avatar-boot-cli 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/exam-question-generate-api.md +163 -0
- package/package.json +1 -1
- package/src/prompts.js +3 -3
- package/src/transform.js +1 -1
- package/templates/.claude/skills/avatar-boot-starter-feign/README.md +243 -0
- package/templates/.claude/skills/avatar-boot-starter-feign/SKILL.md +47 -219
- package/templates/.claude/skills/avatar-boot-starter-feign/references//345/212/237/350/203/275/350/257/246/350/247/243.md +65 -0
- package/templates/.claude/skills/avatar-boot-starter-feign/references//345/277/253/351/200/237/346/216/245/345/205/245/346/214/207/345/215/227.md +75 -0
- package/templates/.claude/skills/avatar-boot-starter-feign/references//351/205/215/347/275/256/345/217/202/350/200/203.md +70 -0
- package/templates/.claude/skills/avatar-boot-starter-job/README.md +437 -0
- package/templates/.claude/skills/avatar-boot-starter-job/SKILL.md +35 -414
- package/templates/.claude/skills/avatar-boot-starter-job/references//345/270/270/350/247/201/351/227/256/351/242/230.md +55 -0
- package/templates/.claude/skills/avatar-boot-starter-job/references//345/277/253/351/200/237/346/216/245/345/205/245/344/270/216/351/205/215/347/275/256.md +124 -0
- package/templates/.claude/skills/avatar-boot-starter-job/references//347/233/221/346/216/247/346/214/207/346/240/207.md +72 -0
- package/templates/.claude/skills/avatar-boot-starter-kafka/README.md +580 -0
- package/templates/.claude/skills/avatar-boot-starter-kafka/SKILL.md +36 -560
- package/templates/.claude/skills/avatar-boot-starter-kafka/references//346/234/200/344/275/263/345/256/236/350/267/265.md +43 -0
- package/templates/.claude/skills/avatar-boot-starter-kafka/references//346/240/270/345/277/203/345/212/237/350/203/275.md +117 -0
- package/templates/.claude/skills/avatar-boot-starter-kafka/references//351/205/215/347/275/256/345/217/202/350/200/203.md +54 -0
- package/templates/.claude/skills/avatar-boot-starter-mysql/README.md +572 -0
- package/templates/.claude/skills/avatar-boot-starter-mysql/SKILL.md +40 -550
- package/templates/.claude/skills/avatar-boot-starter-mysql/references//345/256/236/344/275/223/344/270/216/345/212/237/350/203/275.md +96 -0
- package/templates/.claude/skills/avatar-boot-starter-mysql/references//345/277/253/351/200/237/346/216/245/345/205/245/344/270/216/346/225/260/346/215/256/346/272/220.md +91 -0
- package/templates/.claude/skills/avatar-boot-starter-mysql/references//351/253/230/347/272/247/347/211/271/346/200/247/344/270/216/351/205/215/347/275/256.md +59 -0
- package/templates/.claude/skills/avatar-boot-starter-nacos/README.md +901 -0
- package/templates/.claude/skills/avatar-boot-starter-nacos/SKILL.md +40 -879
- package/templates/.claude/skills/avatar-boot-starter-nacos/references//345/212/237/350/203/275/344/275/277/347/224/250.md +134 -0
- package/templates/.claude/skills/avatar-boot-starter-nacos/references//345/277/253/351/200/237/346/216/245/345/205/245/344/270/216/351/205/215/347/275/256.md +96 -0
- package/templates/.claude/skills/avatar-boot-starter-nacos/references//346/225/205/351/232/234/346/216/222/346/237/245.md +64 -0
- package/templates/.claude/skills/avatar-boot-starter-oss/README.md +594 -0
- package/templates/.claude/skills/avatar-boot-starter-oss/SKILL.md +52 -570
- package/templates/.claude/skills/avatar-boot-starter-oss/references//345/277/253/351/200/237/346/216/245/345/205/245/344/270/216/351/205/215/347/275/256.md +77 -0
- package/templates/.claude/skills/avatar-boot-starter-oss/references//346/240/270/345/277/203/345/212/237/350/203/275.md +94 -0
- package/templates/.claude/skills/avatar-boot-starter-oss/references//350/247/204/350/214/203/344/270/216/346/263/250/346/204/217/344/272/213/351/241/271.md +61 -0
- package/templates/.claude/skills/avatar-boot-starter-redis/README.md +586 -0
- package/templates/.claude/skills/avatar-boot-starter-redis/SKILL.md +42 -566
- package/templates/.claude/skills/avatar-boot-starter-redis/references//345/277/253/351/200/237/346/216/245/345/205/245/344/270/216/351/205/215/347/275/256.md +78 -0
- package/templates/.claude/skills/avatar-boot-starter-redis/references//346/225/260/346/215/256/346/223/215/344/275/234.md +111 -0
- package/templates/.claude/skills/avatar-boot-starter-redis/references//351/253/230/347/272/247/345/212/237/350/203/275.md +90 -0
- package/templates/.claude/skills/avatar-boot-starter-rocketmq/README.md +662 -0
- package/templates/.claude/skills/avatar-boot-starter-rocketmq/SKILL.md +48 -640
- package/templates/.claude/skills/avatar-boot-starter-rocketmq/references//346/240/270/345/277/203/345/212/237/350/203/275.md +101 -0
- package/templates/.claude/skills/avatar-boot-starter-rocketmq/references//351/205/215/347/275/256/344/270/216/346/263/250/346/204/217/344/272/213/351/241/271.md +44 -0
- package/templates/.claude/skills/avatar-boot-starter-rocketmq/references//351/253/230/347/272/247/347/211/271/346/200/247.md +71 -0
- package/templates/.claude/skills/avatar-boot-starter-web/README.md +1007 -0
- package/templates/.claude/skills/avatar-boot-starter-web/SKILL.md +150 -1003
- package/templates/.claude/skills/avatar-boot-starter-web/references//345/212/237/350/203/275-LogInfo/346/263/250/350/247/243.md +75 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//345/212/237/350/203/275-/345/205/250/345/261/200/345/274/202/345/270/270/345/244/204/347/220/206.md +90 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//345/212/237/350/203/275-/346/214/207/346/240/207/347/233/221/346/216/247.md +74 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//345/212/237/350/203/275-/346/227/245/345/277/227/344/275/223/347/263/273.md +73 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//345/212/237/350/203/275-/350/257/267/346/261/202/344/270/212/344/270/213/346/226/207.md +77 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//345/277/253/351/200/237/346/216/245/345/205/245/346/214/207/345/215/227.md +52 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//346/263/250/346/204/217/344/272/213/351/241/271.md +68 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//350/207/252/345/256/232/344/271/211/346/211/251/345/261/225/346/214/207/345/215/227.md +107 -0
- package/templates/.claude/skills/avatar-boot-starter-web/references//351/205/215/347/275/256/345/217/202/350/200/203.md +107 -0
- package/templates/.claude/skills/crud-generator/SKILL.md +133 -64
- package/templates/.claude/skills/database-design/README.md +207 -0
- package/templates/.claude/skills/database-design/SKILL.md +469 -82
- package/templates/.claude/skills/database-design/references//345/221/275/345/220/215/350/247/204/350/214/203.md +232 -0
- package/templates/.claude/skills/database-design/references//345/255/227/346/256/265/347/261/273/345/236/213/350/247/204/350/214/203.md +400 -0
- package/templates/.claude/skills/database-design/references//347/264/242/345/274/225/350/247/204/350/214/203.md +506 -0
- package/templates/README.md +65 -100
- package/templates/avatar-scaffold-api/pom.xml +0 -5
- package/templates/avatar-scaffold-api/src/main/java/com/iflytek/avatar/login/api/LoginFeignClient.java +2 -0
- package/templates/avatar-scaffold-api/src/main/java/com/iflytek/avatar/login/exception/LoginErrorCode.java +25 -0
- package/templates/avatar-scaffold-service/pom.xml +25 -87
- package/templates/avatar-scaffold-service/src/main/java/com/iflytek/avatar/login/feign/DemoFeign.java +4 -1
- package/templates/avatar-scaffold-service/src/main/java/com/iflytek/avatar/login/repository/UserLoginRepository.java +10 -0
- package/templates/avatar-scaffold-service/src/main/java/com/iflytek/avatar/login/repository/mapper/UserLoginMapper.java +4 -1
- package/templates/avatar-scaffold-service/src/main/resources/application-dev.yaml +3 -5
- package/templates/avatar-scaffold-service/src/main/resources/application-local.yaml +21 -21
- package/templates/pom.xml +9 -18
|
@@ -1,243 +1,71 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: avatar-boot-starter-feign
|
|
3
|
-
description:
|
|
3
|
+
description: Avatar Boot Feign 模块使用指南。当用户询问 OpenFeign 客户端配置、TraceId 传播、请求日志、性能指标监控、慢请求检测,或需要接入 avatar-boot-starter-feign 时触发。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Avatar Boot
|
|
6
|
+
# Avatar Boot Starter Feign 使用指南
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
你是 Avatar Boot Feign 模块的使用顾问与开发助手。
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
- ✅ **请求日志** - 自动记录 Feign 客户端的请求参数和响应数据
|
|
12
|
-
- ✅ **TraceId 传播** - 自动传播分布式追踪 ID 到下游服务
|
|
13
|
-
- ✅ **性能指标** - 集成 Micrometer,记录调用次数、执行时间、成功/失败率
|
|
14
|
-
- ✅ **慢请求检测** - 自动检测并警告超过阈值的慢请求
|
|
15
|
-
- ✅ **灵活配置** - 支持细粒度的功能开关和参数配置
|
|
10
|
+
## 交互流程(必须遵守)
|
|
16
11
|
|
|
17
|
-
|
|
12
|
+
**每次被触发时,先通过 AskUserQuestion 工具询问用户意图:**
|
|
18
13
|
|
|
19
|
-
|
|
14
|
+
问题:"您好!我是 Avatar Boot Feign 模块助手,请问您需要哪方面的帮助?"
|
|
15
|
+
选项:
|
|
16
|
+
1. **快速接入** - 添加依赖、定义 FeignClient、启用日志
|
|
17
|
+
2. **功能使用** - TraceId 传播、详细日志、性能指标、慢请求检测
|
|
18
|
+
3. **配置调整** - 日志开关、慢请求阈值、请求体长度限制
|
|
19
|
+
4. **使用场景** - 只要 TraceId / 需要详细日志 / 自定义配置
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
**根据用户选择,用 Read 工具按下方「文档读取路由」加载对应文档,然后给出具体指导。**
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
<dependency>
|
|
25
|
-
<groupId>com.iflytek.avatar.boot</groupId>
|
|
26
|
-
<artifactId>avatar-boot-starter-feign</artifactId>
|
|
27
|
-
</dependency>
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### 2. 定义 Feign 客户端
|
|
31
|
-
|
|
32
|
-
在 Spring Boot 应用主类上添加 `@EnableFeignClients` 注解:
|
|
33
|
-
|
|
34
|
-
```java
|
|
35
|
-
@SpringBootApplication
|
|
36
|
-
@EnableFeignClients
|
|
37
|
-
public class Application {
|
|
38
|
-
public static void main(String[] args) {
|
|
39
|
-
SpringApplication.run(Application.class, args);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
然后定义 Feign 客户端接口:
|
|
45
|
-
|
|
46
|
-
```java
|
|
47
|
-
import com.iflytek.avatar.boot.config.FeignLoggingConfiguration;
|
|
48
|
-
|
|
49
|
-
// 使用 FeignLoggingConfiguration 启用详细日志记录
|
|
50
|
-
@FeignClient(name = "user-service", url = "http://localhost:8080",
|
|
51
|
-
configuration = FeignLoggingConfiguration.class)
|
|
52
|
-
public interface UserServiceClient {
|
|
53
|
-
|
|
54
|
-
@GetMapping("/api/users/{id}")
|
|
55
|
-
User getUserById(@PathVariable("id") Long id);
|
|
56
|
-
|
|
57
|
-
@PostMapping("/api/users")
|
|
58
|
-
User createUser(@RequestBody User user);
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
**重要说明:**
|
|
63
|
-
- `FeignLoggingConfiguration` 不是默认启用的,需要在 `@FeignClient` 的 `configuration` 属性中显式指定
|
|
64
|
-
- 如果不指定 `configuration`,则只会启用 TraceId 传播功能,不会打印详细日志
|
|
65
|
-
- 这样设计是为了让你可以灵活控制哪些 Feign 客户端需要详细日志
|
|
66
|
-
|
|
67
|
-
### 3. 使用 Feign 客户端
|
|
68
|
-
|
|
69
|
-
```java
|
|
70
|
-
@Service
|
|
71
|
-
@RequiredArgsConstructor
|
|
72
|
-
public class UserService {
|
|
73
|
-
|
|
74
|
-
private final UserServiceClient userServiceClient;
|
|
75
|
-
|
|
76
|
-
public User getUser(Long id) {
|
|
77
|
-
return userServiceClient.getUserById(id);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
## 配置说明
|
|
83
|
-
|
|
84
|
-
### 基础配置
|
|
85
|
-
|
|
86
|
-
```yaml
|
|
87
|
-
avatar:
|
|
88
|
-
feign:
|
|
89
|
-
logging:
|
|
90
|
-
enabled: true # 启用日志拦截器(TraceId 传播)
|
|
91
|
-
request-enabled: true # 记录请求参数
|
|
92
|
-
response-enabled: true # 记录响应数据
|
|
93
|
-
print-request-details: true # 打印请求详情(URI、Method、Params)
|
|
94
|
-
print-response-details: true # 打印响应详情(包含响应体)
|
|
95
|
-
print-stack-trace: true # 打印异常堆栈信息
|
|
96
|
-
slow-request-threshold: 3000 # 慢请求阈值(毫秒)
|
|
97
|
-
max-body-length: 1000 # 最大请求体长度
|
|
98
|
-
trace-id-header: X-Trace-Id # TraceId 请求头名称
|
|
99
|
-
metrics:
|
|
100
|
-
enabled: true # 启用指标记录
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### 完整配置示例
|
|
104
|
-
|
|
105
|
-
参考 `src/main/resources/application-example.yml` 查看完整配置示例。
|
|
106
|
-
|
|
107
|
-
## 功能详解
|
|
108
|
-
|
|
109
|
-
### 1. 详细日志记录
|
|
110
|
-
|
|
111
|
-
当在 FeignClient 中配置 `FeignLoggingConfiguration` 后,会自动记录详细的请求和响应信息:
|
|
112
|
-
|
|
113
|
-
**成功请求日志:**
|
|
114
|
-
```
|
|
115
|
-
Feign Request => URI: http://localhost:8080/api/users/1, Method: GET, Params: N/A
|
|
116
|
-
Feign Response => Method: getUserById, URI: http://localhost:8080/api/users/1, Params: N/A, Response: {"id":1,"name":"John"}, Status: 200, Time: 125ms
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
**失败请求日志:**
|
|
120
|
-
```
|
|
121
|
-
Feign Error => Method: getUserById, URI: http://localhost:8080/api/users/1, Params: N/A, Error: Connection refused, Time: 50ms, StackTrace:
|
|
122
|
-
java.net.ConnectException: Connection refused
|
|
123
|
-
at ...
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
**配置控制:**
|
|
127
|
-
- `print-request-details: false` - 不打印请求详情
|
|
128
|
-
- `print-response-details: false` - 不打印响应体(只打印状态和耗时)
|
|
129
|
-
- `print-stack-trace: false` - 不打印异常堆栈信息
|
|
130
|
-
- `max-body-length: 500` - 限制请求体/响应体的最大长度
|
|
131
|
-
|
|
132
|
-
### 2. TraceId 传播
|
|
133
|
-
|
|
134
|
-
自动从 `ContextManager` 获取 TraceId 并添加到请求头,实现分布式追踪:
|
|
135
|
-
|
|
136
|
-
```java
|
|
137
|
-
// 在上游服务中设置 TraceId
|
|
138
|
-
ContextManager.get().setTraceId("trace-123");
|
|
139
|
-
|
|
140
|
-
// Feign 客户端会自动将 TraceId 添加到请求头
|
|
141
|
-
// X-Trace-Id: trace-123
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
**注意:** TraceId 传播是全局启用的,不需要在 FeignClient 中配置 `FeignLoggingConfiguration`。
|
|
145
|
-
|
|
146
|
-
### 3. 性能指标
|
|
147
|
-
|
|
148
|
-
集成 Micrometer,自动记录以下指标:
|
|
149
|
-
|
|
150
|
-
- `feign.client.invocation.count` - 调用次数(标签:client, method, status)
|
|
151
|
-
- `feign.client.execution.time` - 执行时间(标签:client, method, status)
|
|
152
|
-
|
|
153
|
-
查看指标:
|
|
154
|
-
|
|
155
|
-
```bash
|
|
156
|
-
# 访问 Actuator 端
|
|
157
|
-
curl http://localhost:8080/actuator/metrics/feign.client.invocation.count
|
|
158
|
-
curl http://localhost:8080/actuator/metrics/feign.client.execution.time
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
### 4. 慢请求检测
|
|
162
|
-
|
|
163
|
-
当请求执行时间超过配置的阈值时,自动记录警告日志:
|
|
164
|
-
|
|
165
|
-
```
|
|
166
|
-
WARN: Slow Feign Request Detected: getUserById took 3500ms (threshold: 3000ms)
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
## 使用场景
|
|
170
|
-
|
|
171
|
-
### 场景 1:只需要 TraceId 传播,不需要详细日志
|
|
172
|
-
|
|
173
|
-
```java
|
|
174
|
-
// 不指定 configuration,只会启用 TraceId 传播
|
|
175
|
-
@FeignClient(name = "simple-service", url = "http://localhost:8080")
|
|
176
|
-
public interface SimpleServiceClient {
|
|
177
|
-
@GetMapping("/api/data")
|
|
178
|
-
String getData();
|
|
179
|
-
}
|
|
180
|
-
```
|
|
23
|
+
---
|
|
181
24
|
|
|
182
|
-
|
|
25
|
+
## 行为准则
|
|
183
26
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
public interface ImportantServiceClient {
|
|
189
|
-
@PostMapping("/api/process")
|
|
190
|
-
Result processData(@RequestBody Data data);
|
|
191
|
-
}
|
|
192
|
-
```
|
|
27
|
+
1. **回答要具体**:引用具体的注解、配置项、类名
|
|
28
|
+
2. **主动提醒**:`FeignLoggingConfiguration` 需要显式指定,不是默认启用
|
|
29
|
+
3. **使用中文回答**
|
|
30
|
+
4. **版本说明**:Spring Boot 3.5.3 + Spring Cloud 2025.0.0 + Java 21
|
|
193
31
|
|
|
194
|
-
|
|
32
|
+
---
|
|
195
33
|
|
|
196
|
-
|
|
197
|
-
avatar:
|
|
198
|
-
feign:
|
|
199
|
-
logging:
|
|
200
|
-
# 只打印错误日志,不打印成功请求
|
|
201
|
-
print-request-details: false
|
|
202
|
-
print-response-details: false
|
|
203
|
-
# 但保留异常堆栈信息
|
|
204
|
-
print-stack-trace: true
|
|
205
|
-
# 降低慢请求阈值
|
|
206
|
-
slow-request-threshold: 1000
|
|
207
|
-
```
|
|
34
|
+
## 文档读取路由
|
|
208
35
|
|
|
209
|
-
|
|
36
|
+
> 所有路径相对于 skill 目录 `docs/skills/avatar-boot-starter-feign-skill/`
|
|
210
37
|
|
|
211
|
-
|
|
38
|
+
| 用户需求 | 需读取的文件 |
|
|
39
|
+
|:--|:--|
|
|
40
|
+
| 快速接入 | `references/快速接入指南.md` |
|
|
41
|
+
| 功能使用 / 使用场景 | `references/功能详解.md` |
|
|
42
|
+
| 配置调整 | `references/配置参考.md` |
|
|
212
43
|
|
|
213
|
-
|
|
214
|
-
feign:
|
|
215
|
-
client:
|
|
216
|
-
config:
|
|
217
|
-
user-service:
|
|
218
|
-
connectTimeout: 3000
|
|
219
|
-
readTimeout: 5000
|
|
220
|
-
loggerLevel: FULL
|
|
221
|
-
```
|
|
44
|
+
---
|
|
222
45
|
|
|
223
|
-
|
|
46
|
+
## 通用参考信息
|
|
224
47
|
|
|
225
|
-
|
|
226
|
-
avatar:
|
|
227
|
-
feign:
|
|
228
|
-
logging:
|
|
229
|
-
enabled: false # 禁用日志
|
|
230
|
-
metrics:
|
|
231
|
-
enabled: false # 禁用指标
|
|
232
|
-
```
|
|
48
|
+
### 核心类速查
|
|
233
49
|
|
|
234
|
-
|
|
50
|
+
| 类/注解 | 用途 |
|
|
51
|
+
|:--|:--|
|
|
52
|
+
| `@EnableFeignClients` | 启动类上启用 Feign |
|
|
53
|
+
| `@FeignClient` | 定义 Feign 客户端接口 |
|
|
54
|
+
| `FeignLoggingConfiguration` | 启用详细请求/响应日志(需显式指定) |
|
|
235
55
|
|
|
236
|
-
|
|
237
|
-
- Spring Cloud: 2025.0.0
|
|
238
|
-
- Java: 21
|
|
56
|
+
### 默认配置值
|
|
239
57
|
|
|
240
|
-
|
|
58
|
+
| 配置项 | 默认值 |
|
|
59
|
+
|:--|:--|
|
|
60
|
+
| `avatar.feign.logging.enabled` | `true` |
|
|
61
|
+
| `avatar.feign.logging.slow-request-threshold` | `3000`(ms) |
|
|
62
|
+
| `avatar.feign.logging.max-body-length` | `1000` |
|
|
63
|
+
| `avatar.feign.logging.trace-id-header` | `X-Trace-Id` |
|
|
64
|
+
| `avatar.feign.metrics.enabled` | `true` |
|
|
241
65
|
|
|
242
|
-
|
|
66
|
+
### 可用指标
|
|
243
67
|
|
|
68
|
+
| 指标名 | 标签 |
|
|
69
|
+
|:--|:--|
|
|
70
|
+
| `feign.client.invocation.count` | `client`, `method`, `status` |
|
|
71
|
+
| `feign.client.execution.time` | `client`, `method`, `status` |
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Feign 功能详解
|
|
2
|
+
|
|
3
|
+
## 1. TraceId 传播
|
|
4
|
+
|
|
5
|
+
自动从 `ContextManager` 获取当前请求的 TraceId,添加到 Feign 请求头,实现跨服务链路追踪。
|
|
6
|
+
|
|
7
|
+
- **全局生效**:无需配置 `FeignLoggingConfiguration`,所有 FeignClient 自动传播
|
|
8
|
+
- **请求头名称**:默认 `X-Trace-Id`,可通过 `avatar.feign.logging.trace-id-header` 修改
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
上游服务 [TraceId: abc123] → Feign 自动添加 X-Trace-Id: abc123 → 下游服务 [TraceId: abc123]
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 2. 详细日志记录
|
|
15
|
+
|
|
16
|
+
需要在 `@FeignClient` 中显式指定 `FeignLoggingConfiguration`:
|
|
17
|
+
|
|
18
|
+
```java
|
|
19
|
+
@FeignClient(name = "service", url = "http://...",
|
|
20
|
+
configuration = FeignLoggingConfiguration.class)
|
|
21
|
+
public interface MyClient { ... }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**成功请求日志**:
|
|
25
|
+
```
|
|
26
|
+
Feign Request => URI: http://localhost:8080/api/users/1, Method: GET, Params: N/A
|
|
27
|
+
Feign Response => Method: getUserById, Status: 200, Time: 125ms, Response: {"id":1,"name":"张三"}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**失败请求日志**:
|
|
31
|
+
```
|
|
32
|
+
Feign Error => Method: getUserById, URI: ..., Error: Connection refused, Time: 50ms
|
|
33
|
+
java.net.ConnectException: Connection refused ...
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 3. 性能指标(Micrometer)
|
|
37
|
+
|
|
38
|
+
自动记录以下指标,通过 Actuator 查询:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# 调用次数
|
|
42
|
+
curl http://localhost:8080/actuator/metrics/feign.client.invocation.count
|
|
43
|
+
|
|
44
|
+
# 执行时间
|
|
45
|
+
curl http://localhost:8080/actuator/metrics/feign.client.execution.time
|
|
46
|
+
|
|
47
|
+
# 按标签过滤(某个方法的成功调用)
|
|
48
|
+
curl "http://localhost:8080/actuator/metrics/feign.client.invocation.count?tag=status:success"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 4. 慢请求检测
|
|
52
|
+
|
|
53
|
+
请求超过阈值(默认 3000ms)时自动输出 WARN 日志:
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
WARN: Slow Feign Request Detected: getUserById took 3500ms (threshold: 3000ms)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 5. 使用场景对比
|
|
60
|
+
|
|
61
|
+
| 场景 | 配置方式 |
|
|
62
|
+
|:--|:--|
|
|
63
|
+
| 只需 TraceId 传播 | 不指定 `configuration`(默认) |
|
|
64
|
+
| 需要详细日志 + 指标 | 指定 `configuration = FeignLoggingConfiguration.class` |
|
|
65
|
+
| 关闭某个客户端的日志 | 不指定 `configuration` |
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Feign 快速接入指南
|
|
2
|
+
|
|
3
|
+
## 1. 添加依赖
|
|
4
|
+
|
|
5
|
+
```xml
|
|
6
|
+
<dependency>
|
|
7
|
+
<groupId>com.iflytek.avatar.boot</groupId>
|
|
8
|
+
<artifactId>avatar-boot-starter-feign</artifactId>
|
|
9
|
+
</dependency>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 2. 启用 Feign
|
|
13
|
+
|
|
14
|
+
在启动类上添加 `@EnableFeignClients`:
|
|
15
|
+
|
|
16
|
+
```java
|
|
17
|
+
@SpringBootApplication
|
|
18
|
+
@EnableFeignClients
|
|
19
|
+
public class Application {
|
|
20
|
+
public static void main(String[] args) {
|
|
21
|
+
SpringApplication.run(Application.class, args);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 3. 定义 Feign 客户端
|
|
27
|
+
|
|
28
|
+
### 只需要 TraceId 传播(不打印详细日志)
|
|
29
|
+
|
|
30
|
+
```java
|
|
31
|
+
@FeignClient(name = "user-service", url = "http://localhost:8080")
|
|
32
|
+
public interface UserServiceClient {
|
|
33
|
+
|
|
34
|
+
@GetMapping("/api/users/{id}")
|
|
35
|
+
User getUserById(@PathVariable("id") Long id);
|
|
36
|
+
|
|
37
|
+
@PostMapping("/api/users")
|
|
38
|
+
User createUser(@RequestBody User user);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 需要详细请求/响应日志
|
|
43
|
+
|
|
44
|
+
```java
|
|
45
|
+
import com.iflytek.avatar.boot.config.FeignLoggingConfiguration;
|
|
46
|
+
|
|
47
|
+
@FeignClient(name = "order-service", url = "http://localhost:8081",
|
|
48
|
+
configuration = FeignLoggingConfiguration.class)
|
|
49
|
+
public interface OrderServiceClient {
|
|
50
|
+
|
|
51
|
+
@PostMapping("/api/orders")
|
|
52
|
+
Result<Order> createOrder(@RequestBody OrderRequest request);
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> `FeignLoggingConfiguration` 需要显式指定,不是默认启用的。
|
|
57
|
+
|
|
58
|
+
## 4. 注入使用
|
|
59
|
+
|
|
60
|
+
```java
|
|
61
|
+
@Service
|
|
62
|
+
@RequiredArgsConstructor
|
|
63
|
+
public class UserService {
|
|
64
|
+
|
|
65
|
+
private final UserServiceClient userServiceClient;
|
|
66
|
+
|
|
67
|
+
public User getUser(Long id) {
|
|
68
|
+
return userServiceClient.getUserById(id);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 5. 验证 TraceId 传播
|
|
74
|
+
|
|
75
|
+
启动后调用接口,查看下游服务日志中是否包含相同的 TraceId,确认链路追踪生效。
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Feign 配置参考
|
|
2
|
+
|
|
3
|
+
## 完整配置项
|
|
4
|
+
|
|
5
|
+
```yaml
|
|
6
|
+
avatar:
|
|
7
|
+
feign:
|
|
8
|
+
logging:
|
|
9
|
+
enabled: true # 启用日志拦截器(含 TraceId 传播,默认 true)
|
|
10
|
+
request-enabled: true # 记录请求参数
|
|
11
|
+
response-enabled: true # 记录响应数据
|
|
12
|
+
print-request-details: true # 打印请求详情(URI、Method、Params)
|
|
13
|
+
print-response-details: true # 打印响应详情(含响应体)
|
|
14
|
+
print-stack-trace: true # 打印异常堆栈
|
|
15
|
+
slow-request-threshold: 3000 # 慢请求阈值(毫秒)
|
|
16
|
+
max-body-length: 1000 # 请求体/响应体最大长度(超出截断)
|
|
17
|
+
trace-id-header: X-Trace-Id # TraceId 请求头名称
|
|
18
|
+
metrics:
|
|
19
|
+
enabled: true # 启用 Micrometer 指标
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 常用场景配置
|
|
23
|
+
|
|
24
|
+
### 生产环境(只记录错误,不打印响应体)
|
|
25
|
+
|
|
26
|
+
```yaml
|
|
27
|
+
avatar:
|
|
28
|
+
feign:
|
|
29
|
+
logging:
|
|
30
|
+
print-request-details: false
|
|
31
|
+
print-response-details: false
|
|
32
|
+
print-stack-trace: true
|
|
33
|
+
slow-request-threshold: 2000
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 调试模式(打印所有详情)
|
|
37
|
+
|
|
38
|
+
```yaml
|
|
39
|
+
avatar:
|
|
40
|
+
feign:
|
|
41
|
+
logging:
|
|
42
|
+
request-enabled: true
|
|
43
|
+
response-enabled: true
|
|
44
|
+
print-request-details: true
|
|
45
|
+
print-response-details: true
|
|
46
|
+
max-body-length: 5000
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 关闭日志和指标
|
|
50
|
+
|
|
51
|
+
```yaml
|
|
52
|
+
avatar:
|
|
53
|
+
feign:
|
|
54
|
+
logging:
|
|
55
|
+
enabled: false
|
|
56
|
+
metrics:
|
|
57
|
+
enabled: false
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 针对特定客户端配置超时
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
feign:
|
|
64
|
+
client:
|
|
65
|
+
config:
|
|
66
|
+
user-service: # FeignClient 的 name
|
|
67
|
+
connectTimeout: 3000
|
|
68
|
+
readTimeout: 10000
|
|
69
|
+
loggerLevel: FULL
|
|
70
|
+
```
|