这个基于SpringBoot的社交平台项目是我在2023年主导开发的一个全栈式实践案例,完整包含了从技术选型到部署上线的全流程。系统采用经典的三层架构设计,前端使用Thymeleaf模板引擎配合Bootstrap5实现响应式布局,后端基于SpringBoot 2.7整合了Spring Security、WebSocket等核心组件,数据库选用MySQL 8.0并进行了系统的性能优化。特别值得一提的是,项目中实现了社交平台的三大核心功能模块:即时通讯、动态feed流和好友关系网络,每个模块都经过精心设计和压力测试。
选择SpringBoot作为基础框架主要基于以下几个技术考量:
数据库访问层采用JPA+Hibernate的组合方案,相比MyBatis的优势在于:
java复制// 用户实体类示例
@Entity
@Table(name = "user")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String username;
@OneToMany(mappedBy = "author")
private List<Post> posts = new ArrayList<>();
@ManyToMany
@JoinTable(name = "user_friends",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "friend_id"))
private Set<User> friends = new HashSet<>();
}
虽然现在主流趋势是前后端分离,但本项目选择服务端渲染主要基于:
Thymeleaf的三大实用技巧:
html复制<!-- 动态列表模板示例 -->
<div th:each="post : ${posts}" class="card mb-3">
<div class="card-header">
<span th:text="${post.author.nickname}">用户名</span>
<small class="text-muted" th:text="${#dates.format(post.createTime, 'yyyy-MM-dd HH:mm')}"></small>
</div>
<div class="card-body" th:text="${post.content}">动态内容...</div>
<div class="card-footer">
<button th:if="${#authentication.principal.id != post.author.id}"
class="btn btn-sm btn-outline-primary">关注</button>
</div>
</div>
采用WebSocket协议实现实时对话,关键设计点包括:
java复制@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue", "/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat")
.setAllowedOrigins("*")
.withSockJS();
}
}
采用推拉结合的模式优化性能:
sql复制-- 动态表结构设计
CREATE TABLE `post` (
`id` bigint NOT NULL AUTO_INCREMENT,
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`author_id` bigint NOT NULL,
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`visibility` tinyint NOT NULL DEFAULT '0' COMMENT '0公开 1好友可见 2私密',
PRIMARY KEY (`id`),
KEY `idx_author` (`author_id`),
KEY `idx_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
java复制@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/auth/**").permitAll()
.antMatchers("/websocket/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
采用Docker Compose编排方案,包含以下服务:
yaml复制version: '3.8'
services:
app:
build: .
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=prod
depends_on:
- db
- redis
db:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=rootpass
- MYSQL_DATABASE=social
volumes:
- db_data:/var/lib/mysql
redis:
image: redis:alpine
ports:
- "6379:6379"
核心监控指标包括:
事务管理陷阱:
循环引用问题:
时区处理:
性能测试发现:
这个项目从技术选型到最终上线历时3个月,期间经历了多次架构调整和性能优化。最大的收获是认识到社交类系统在数据一致性、实时性和扩展性方面的特殊挑战。比如在消息已读状态同步时,最初采用的全量同步方案在用户量增长后出现了明显的性能瓶颈,后来改为差异同步才解决问题。