跳到主要内容

后端开发

"精心设计的后端是任何可扩展应用的基石。"

掌握 Java 生态系统,构建健壮、高性能的后端服务。


Spring Boot

核心概念

应用启动流程

常用注解

注解用途
@SpringBootApplication入口(组合了 3 个注解)
@RestControllerREST API 端点
@Service业务逻辑 Bean
@Repository数据访问 Bean
@Configuration基于 Java 的配置
@Bean定义 Spring 管理的 Bean
@Autowired依赖注入

自动配置

// Spring Boot 基于 classpath 自动配置
// 添加 starter → 获得配置
@ConditionalOnClass(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.url")
@AutoConfiguration
public class DataSourceAutoConfiguration {
// 自动创建 DataSource Bean
}

请求处理

@RestController
@RequestMapping("/api/v1/users")
public class UserController {

private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return userService.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User createUser(@Valid @RequestBody CreateUserRequest request) {
return userService.create(request);
}

@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFound(UserNotFoundException ex) {
return new ErrorResponse(ex.getMessage());
}
}

并发编程(JUC)

线程池

// 推荐:使用线程池,不要直接创建线程
ExecutorService executor = Executors.newFixedThreadPool(10);

// 更好的方式:自定义 ThreadPoolExecutor,调整参数
ThreadPoolExecutor executor = new ThreadPoolExecutor(
5, // corePoolSize
10, // maxPoolSize
60L, TimeUnit.SECONDS, // keepAliveTime
new LinkedBlockingQueue<>(100), // workQueue
new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
);

CompletableFuture

// 异步组合
public CompletableFuture<OrderSummary> processOrder(Order order) {
return CompletableFuture
.supplyAsync(() -> validateOrder(order))
.thenCompose(this::checkInventory)
.thenCombine(calculateShipping(order), this::createSummary)
.exceptionally(ex -> handleError(ex));
}

// 并行运行多个任务
CompletableFuture.allOf(task1, task2, task3)
.thenAccept(v -> {
// 所有任务完成
});

锁和同步

机制使用场景
synchronized简单互斥
ReentrantLock更多控制(tryLock、公平锁)
ReadWriteLock多读单写
StampedLock乐观读锁
Semaphore限制并发访问
// ReentrantLock 带超时
private final ReentrantLock lock = new ReentrantLock();

public void updateResource() {
try {
if (lock.tryLock(1, TimeUnit.SECONDS)) {
try {
// 临界区
} finally {
lock.unlock();
}
} else {
throw new TimeoutException("无法获取锁");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

JVM 内部原理

内存模型

垃圾收集

GC 类型特点适用场景
G1低延迟、均衡通用(默认)
ZGC超低暂停(10ms 以下)大堆内存、延迟敏感
Parallel高吞吐批处理
Shenandoah低暂停、并发RedHat/OpenJDK

JVM 调优参数

# 常见生产环境设置
java -Xms4g -Xmx4g \ # 堆大小(最小 = 最大)
-XX:+UseG1GC \ # 使用 G1 垃圾收集器
-XX:MaxGCPauseMillis=200 \ # 目标暂停时间
-XX:+HeapDumpOnOutOfMemoryError \
-Xlog:gc*:file=gc.log \ # GC 日志
-jar app.jar

性能分析和监控

# JFR(Java Flight Recorder)
jcmd <pid> JFR.start duration=60s filename=recording.jfr

# jstat - GC 统计
jstat -gcutil <pid> 1000

# jmap - 堆转储
jmap -dump:live,format=b,file=heap.hprof <pid>

API 设计

REST 最佳实践

HTTP 方法用途幂等
GET获取资源
POST创建资源
PUT替换资源
PATCH部分更新
DELETE删除资源

响应结构

// 统一 API 响应
public record ApiResponse<T>(
boolean success,
T data,
String message,
Map<String, Object> metadata
) {
public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>(true, data, null, null);
}

public static <T> ApiResponse<T> error(String message) {
return new ApiResponse<>(false, null, message, null);
}
}

详细主题


性能优化建议
  1. 连接池 - 使用 HikariCP(Spring Boot 默认)
  2. 懒加载 - 只在需要时获取数据
  3. 批量操作 - 减少数据库往返
  4. 尽量异步 - 不要阻塞 I/O
  5. 先分析再优化 - 测量优先