ARTICLE · 1357274
速查手册实战:常用依赖与注解、配置文件模板、排查命令与故障判断与线上故障排查
本附录汇总常用配置、注解、命令和排查路径。Spring Boot 3.x 与 Spring Cloud 2023.x/2024.x 为主线,具体配置项随版本变化,使用前以当前版本文档为准。
01
PART
常用依赖
PART 01
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway-server-webflux</artifactId>
</dependency>
02
PART
常用注解
PART 02
03
PART
配置文件模板
PART 03
spring:
application:
name: order-service
profiles:
active: ${SPRING_PROFILES_ACTIVE:dev}
datasource:
url: jdbc:mysql://${DB_HOST:localhost}:3306/order
username: ${DB_USER:order}
password: ${DB_PASSWORD}
hikari:
pool-name: order-hikari
maximum-pool-size: 20
connection-timeout: 2000
jpa:
open-in-view: false
data:
redis:
host: ${REDIS_HOST:localhost}
timeout: 500ms
应用配置:
app:
downstream:
inventory:
timeout: 2s
retry: 2
feature:
new-order-flow: false
04
PART
Actuator
PART 04
management:
endpoints:
web:
exposure:
include: health,info,prometheus,metrics
endpoint:
health:
probes:
enabled: true
show-details: never
端点:
生产谨慎暴露 env、beans、threaddump。
05
PART
Web 接口模板
PART 05
@RestController
@RequestMapping("/api/orders")
publicclassOrderController {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OrderView create(@RequestHeader("Idempotency-Key") String key,
@Valid@RequestBody CreateOrderRequest request) {
return orderService.create(request.toCommand(key));
}
@GetMapping("/{id}")
public OrderView get(@PathVariable Long id) {
return orderService.get(id);
}
}
全局异常:
@RestControllerAdvice
publicclassApiExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleValidation(MethodArgumentNotValidException ex) {
returnnew ErrorResponse("VALIDATION_ERROR", "invalid request");
}
}
06
PART
事务速查
PART 06
传播行为:
失效检查:
自调用?
public 方法?
异常被吞?
rollbackFor?
Bean 被 Spring 管理?
多线程执行?
代理和事务管理器配置正确?
07
PART
Feign 配置
PART 07
spring:
cloud:
openfeign:
client:
config:
inventory-service:
connect-timeout: 500
read-timeout: 2000
retryer: never
接口:
@FeignClient(name ="inventory-service", path ="/api/inventories")
publicinterfaceInventoryClient {
@GetMapping("/{id}")
InventoryView get(@PathVariable Long id);
}
08
PART
Resilience4j
PART 08
resilience4j:
circuitbreaker:
instances:
inventory:
sliding-window-size: 100
minimum-number-of-calls: 20
failure-rate-threshold: 50
wait-duration-in-open-state: 10s
ratelimiter:
instances:
createOrder:
limit-for-period: 100
limit-refresh-period: 1s
状态:
CLOSED -> OPEN -> HALF_OPEN
09
PART
网关路由
PART 09
spring:
cloud:
gateway:
server:
webflux:
routes:
- id: order
uri: lb://order-service
predicates:
- Path=/api/orders/**
常用谓词:
Path
Method
Header
Query
Host
Weight
10
PART
Kafka 配置
PART 10
生产者:
spring:
kafka:
producer:
bootstrap-servers: kafka:9092
acks: all
properties:
enable.idempotence: true
消费者:
spring:
kafka:
consumer:
group-id: order-service
enable-auto-commit: false
治理:
Outbox
幂等表
分区 key
重试 topic
死信 topic
lag 告警
11
PART
测试注解
PART 11
Maven 命令:
mvn test
mvn verify
12
PART
容器启动参数
PART 12
exec java \
-XX:+UseContainerSupport \
-XX:InitialRAMPercentage=60.0 \
-XX:MaxRAMPercentage=60.0 \
-XX:ActiveProcessorCount=4\
-XX:MaxMetaspaceSize=512m \
-XX:MaxDirectMemorySize=1g \
-Xlog:gc*,safepoint:file=/logs/gc.log:time,uptime,level,tags \
-jar /app/app.jar
探针:
/actuator/health/liveness
/actuator/health/readiness
13
PART
优雅停机
PART 13
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s
K8s:
terminationGracePeriodSeconds > 应用等待时间
14
PART
排查命令
PART 14
# JVM
jcmd <pid> VM.command_line
jcmd <pid> VM.flags
jcmd <pid> Thread.print
jcmd <pid> GC.heap_info
# Linux
top -H -p <pid>
ss -lntp
vmstat 1
日志级别:
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.transaction=DEBUG
15
PART
常见故障判断
PART 15















































































