feat(user): Implement user module foundation
This commit is contained in:
@@ -113,4 +113,13 @@ public class AuthController {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取用户名
|
||||
*/
|
||||
@GetMapping("/username/{userId}")
|
||||
public String getUsernameById(@PathVariable Long userId) {
|
||||
AuthUser user = authUserService.getById(userId);
|
||||
return user != null ? user.getUsername() : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.nuaa.video.auth.feign;
|
||||
|
||||
import com.nuaa.video.auth.feign.fallback.UserClientFallback;
|
||||
import com.nuaa.video.common.model.vo.Result;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient(name = "video-user", fallback = UserClientFallback.class)
|
||||
public interface UserClient {
|
||||
|
||||
@PostMapping("/user/profile/init")
|
||||
Result<Void> createUserInfo(@RequestParam("userId") Long userId,
|
||||
@RequestParam("username") String username);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.nuaa.video.auth.feign.fallback;
|
||||
|
||||
import com.nuaa.video.auth.feign.UserClient;
|
||||
import com.nuaa.video.common.model.vo.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UserClientFallback implements UserClient {
|
||||
|
||||
@Override
|
||||
public Result<Void> createUserInfo(Long userId, String username) {
|
||||
log.error("创建用户资料失败(回退): userId={}, username={}", userId, username);
|
||||
return Result.error("用户服务不可用,资料将在首次访问时创建");
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.nuaa.video.auth.dto.RegisterDTO;
|
||||
import com.nuaa.video.auth.dto.TokenInfoDTO;
|
||||
import com.nuaa.video.auth.entity.AuthUser;
|
||||
import com.nuaa.video.auth.entity.AuthUserRole;
|
||||
import com.nuaa.video.auth.feign.UserClient;
|
||||
import com.nuaa.video.auth.mapper.AuthUserMapper;
|
||||
import com.nuaa.video.auth.mapper.AuthUserRoleMapper;
|
||||
import com.nuaa.video.auth.service.AuthUserService;
|
||||
@@ -21,6 +22,8 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -31,7 +34,7 @@ import java.util.List;
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@RequiredArgsConstructor //Lombok注解,会自动为final字段创建构造函数
|
||||
public class AuthUserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> implements AuthUserService {
|
||||
|
||||
private final AuthUserMapper authUserMapper;
|
||||
@@ -39,6 +42,7 @@ public class AuthUserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> i
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtUtils jwtUtils;
|
||||
private final RedisUtils redisUtils;
|
||||
private final UserClient userClient;
|
||||
|
||||
@Value("${jwt.expiration:86400}")
|
||||
private long expiration;
|
||||
@@ -171,6 +175,22 @@ public class AuthUserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> i
|
||||
userRole.setCreateTime(LocalDateTime.now());
|
||||
authUserRoleMapper.insert(userRole);
|
||||
|
||||
// 使用事务同步器确保在事务提交后再创建用户资料
|
||||
final Long userId = user.getId();
|
||||
final String username = user.getUsername();
|
||||
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
try {
|
||||
userClient.createUserInfo(userId, username);
|
||||
log.info("用户资料创建成功:userId={}", userId);
|
||||
} catch (Exception e) {
|
||||
log.error("创建用户资料失败,用户ID: " + userId, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Result.success("注册成功");
|
||||
}
|
||||
|
||||
|
||||
@@ -32,12 +32,22 @@ spring:
|
||||
username: ${MYSQL_USERNAME:root}
|
||||
password: ${MYSQL_PASSWORD:sty@G458221}
|
||||
hikari:
|
||||
minimum-idle: 5
|
||||
maximum-pool-size: 20
|
||||
idle-timeout: 30000
|
||||
max-lifetime: 1200000
|
||||
connection-timeout: 30000
|
||||
# 减少最大生命周期,避免超过数据库的wait_timeout
|
||||
max-lifetime: 58000 # 设置为MySQL wait_timeout的值减少一些,默认通常为8小时(28800000ms)
|
||||
# 空闲连接超时时间
|
||||
idle-timeout: 300000 # 5分钟,超过该时间的空闲连接将被移除
|
||||
# 连接超时时间
|
||||
connection-timeout: 20000 # 20秒
|
||||
# 测试连接的有效性
|
||||
connection-test-query: SELECT 1
|
||||
# 保持连接活跃的时间间隔
|
||||
keepalive-time: 60000 # 1分钟,定期发送ping保持连接
|
||||
# 验证超时时间
|
||||
validation-timeout: 3000 # 3秒
|
||||
# 最小空闲连接数
|
||||
minimum-idle: 5
|
||||
# 最大连接池大小
|
||||
maximum-pool-size: 20
|
||||
|
||||
data:
|
||||
redis:
|
||||
@@ -87,4 +97,13 @@ logging:
|
||||
com.nuaa.video: info
|
||||
org.springframework.security: info
|
||||
org.springframework.beans: debug
|
||||
org.springframework.context: debug
|
||||
org.springframework.context: debug
|
||||
|
||||
feign:
|
||||
circuitbreaker:
|
||||
enabled: true # 启用断路器
|
||||
client:
|
||||
config:
|
||||
default:
|
||||
connectTimeout: 5000
|
||||
readTimeout: 5000
|
||||
Reference in New Issue
Block a user