From e3696a82164130cbf2e0934fe4a5402f5e093bc9 Mon Sep 17 00:00:00 2001 From: DongZeLiang <2746733890@qq.com> Date: Thu, 11 Jan 2024 20:53:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/muyu/auth/AuthApplication.java | 7 ++++ .../auth/controller/AuthRegController.java | 32 +++++++++++++++ .../com/muyu/auth/service/AuthRegService.java | 16 ++++++++ .../auth/service/impl/AuthRegServiceImpl.java | 40 +++++++++++++++++++ .../common/constant/ServerNameConstants.java | 15 +++++++ .../domain/request/auth/RegUserReq.java | 29 ++++++++++++++ .../handler/GlobalExceptionHandler.java | 9 +++++ .../remote/system/RemoteUserInfoService.java | 30 ++++++++++++++ .../system/factory/RemoteUserInfoFactory.java | 34 ++++++++++++++++ .../java/com/muyu/common/result/Result.java | 16 ++++++++ .../system/service/UserInfoServiceImpl.java | 17 -------- .../service/impl/UserInfoServiceImpl.java | 29 ++++++++++++++ 12 files changed, 257 insertions(+), 17 deletions(-) create mode 100644 muyu-auth/src/main/java/com/muyu/auth/controller/AuthRegController.java create mode 100644 muyu-auth/src/main/java/com/muyu/auth/service/AuthRegService.java create mode 100644 muyu-auth/src/main/java/com/muyu/auth/service/impl/AuthRegServiceImpl.java create mode 100644 muyu-common/src/main/java/com/muyu/common/constant/ServerNameConstants.java create mode 100644 muyu-common/src/main/java/com/muyu/common/domain/request/auth/RegUserReq.java create mode 100644 muyu-common/src/main/java/com/muyu/common/remote/system/RemoteUserInfoService.java create mode 100644 muyu-common/src/main/java/com/muyu/common/remote/system/factory/RemoteUserInfoFactory.java delete mode 100644 muyu-modules/muyu-system/src/main/java/com/muyu/system/service/UserInfoServiceImpl.java create mode 100644 muyu-modules/muyu-system/src/main/java/com/muyu/system/service/impl/UserInfoServiceImpl.java diff --git a/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java b/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java index 7ea2b84..fd42377 100644 --- a/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java +++ b/muyu-auth/src/main/java/com/muyu/auth/AuthApplication.java @@ -1,9 +1,14 @@ package com.muyu.auth; +import com.muyu.common.handler.GlobalExceptionHandler; +import com.muyu.common.remote.system.factory.RemoteUserInfoFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; /** * @author DongZl @@ -11,6 +16,8 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; */ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableDiscoveryClient +@EnableFeignClients(basePackages = "com.muyu.**") +@Import(value = {RemoteUserInfoFactory.class, GlobalExceptionHandler.class}) public class AuthApplication { public static void main(String[] args) { SpringApplication.run(AuthApplication.class); diff --git a/muyu-auth/src/main/java/com/muyu/auth/controller/AuthRegController.java b/muyu-auth/src/main/java/com/muyu/auth/controller/AuthRegController.java new file mode 100644 index 0000000..a14c7f3 --- /dev/null +++ b/muyu-auth/src/main/java/com/muyu/auth/controller/AuthRegController.java @@ -0,0 +1,32 @@ +package com.muyu.auth.controller; + +import com.muyu.auth.service.AuthRegService; +import com.muyu.common.domain.request.auth.RegUserReq; +import com.muyu.common.result.Result; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author DongZl + * @description: 注册 + * @Date 2024-1-11 下午 08:04 + */ +@RestController +@RequestMapping("/reg") +public class AuthRegController { + + private final AuthRegService authRegService; + + public AuthRegController (AuthRegService authRegService) { + this.authRegService = authRegService; + } + + @PostMapping() + public Result reg(@RequestBody @Validated RegUserReq regUserReq){ + authRegService.reg(regUserReq.getUserName(), regUserReq.getPassword()); + return Result.success(); + } +} diff --git a/muyu-auth/src/main/java/com/muyu/auth/service/AuthRegService.java b/muyu-auth/src/main/java/com/muyu/auth/service/AuthRegService.java new file mode 100644 index 0000000..8fa235a --- /dev/null +++ b/muyu-auth/src/main/java/com/muyu/auth/service/AuthRegService.java @@ -0,0 +1,16 @@ +package com.muyu.auth.service; + +/** + * @author DongZl + * @description: 注册业务 + * @Date 2024-1-11 下午 08:07 + */ +public interface AuthRegService { + + /** + * 注册用户 + * @param userName 用户名称 + * @param password 用户密码 + */ + void reg (String userName, String password); +} diff --git a/muyu-auth/src/main/java/com/muyu/auth/service/impl/AuthRegServiceImpl.java b/muyu-auth/src/main/java/com/muyu/auth/service/impl/AuthRegServiceImpl.java new file mode 100644 index 0000000..9ffdfb6 --- /dev/null +++ b/muyu-auth/src/main/java/com/muyu/auth/service/impl/AuthRegServiceImpl.java @@ -0,0 +1,40 @@ +package com.muyu.auth.service.impl; + +import com.muyu.auth.service.AuthRegService; +import com.muyu.common.domain.request.user.UserAddReq; +import com.muyu.common.remote.system.RemoteUserInfoService; +import com.muyu.common.result.Result; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +/** + * @author DongZl + * @description: 注册用户实现类 + * @Date 2024-1-11 下午 08:07 + */ +@Service +public class AuthRegServiceImpl implements AuthRegService { + + private final RemoteUserInfoService remoteUserInfoService; + + public AuthRegServiceImpl (RemoteUserInfoService remoteUserInfoService) { + this.remoteUserInfoService = remoteUserInfoService; + } + + /** + * 注册用户 + * + * @param userName 用户名称 + * @param password 用户密码 + */ + @Override + public void reg (String userName, String password) { + Result addResult = remoteUserInfoService.add( + UserAddReq.builder() + .userName(userName) + .password(password) + .build() + ); + Assert.isTrue(addResult.isSuccess(), "用户注册失败"); + } +} diff --git a/muyu-common/src/main/java/com/muyu/common/constant/ServerNameConstants.java b/muyu-common/src/main/java/com/muyu/common/constant/ServerNameConstants.java new file mode 100644 index 0000000..73d01ee --- /dev/null +++ b/muyu-common/src/main/java/com/muyu/common/constant/ServerNameConstants.java @@ -0,0 +1,15 @@ +package com.muyu.common.constant; + +/** + * @author DongZl + * @description: 服务名称常量 + * @Date 2024-1-11 下午 07:43 + */ +public class ServerNameConstants { + + /** + * 系统服务名称 + */ + public final static String SYSTEM_NAME = "muyu-system"; + +} diff --git a/muyu-common/src/main/java/com/muyu/common/domain/request/auth/RegUserReq.java b/muyu-common/src/main/java/com/muyu/common/domain/request/auth/RegUserReq.java new file mode 100644 index 0000000..9afec61 --- /dev/null +++ b/muyu-common/src/main/java/com/muyu/common/domain/request/auth/RegUserReq.java @@ -0,0 +1,29 @@ +package com.muyu.common.domain.request.auth; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.Pattern; + +/** + * @author DongZl + * @description: 注册用户 + * @Date 2024-1-11 下午 08:05 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class RegUserReq { + + @NotEmpty(message = "用户名称不可为空") + @Pattern(regexp = "(?=.*[A-Za-z]\\d).{6,16}$", message = "用户名称不合法,因为6-16位的大小写英文和数字组成") + private String userName; + + @NotEmpty(message = "用户密码不可为空") + @Pattern(regexp = "(?=.*[A-Za-z]\\d).{8,16}$", message = "用户密码不合法,因为8-16位的大小写英文和数字组成") + private String password; +} diff --git a/muyu-common/src/main/java/com/muyu/common/handler/GlobalExceptionHandler.java b/muyu-common/src/main/java/com/muyu/common/handler/GlobalExceptionHandler.java index 0205af9..107093f 100644 --- a/muyu-common/src/main/java/com/muyu/common/handler/GlobalExceptionHandler.java +++ b/muyu-common/src/main/java/com/muyu/common/handler/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ package com.muyu.common.handler; import com.alibaba.fastjson.JSONObject; import com.muyu.common.result.Result; +import lombok.extern.log4j.Log4j2; import org.springframework.context.annotation.Configuration; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -13,12 +14,14 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; * @description: 全局异常处理 * @Date 2024-1-10 下午 08:49 */ +@Log4j2 @RestControllerAdvice @Configuration public class GlobalExceptionHandler { @ExceptionHandler(value = MethodArgumentNotValidException.class) public Result runtimeException (MethodArgumentNotValidException exception) { + log.error("请求异常:[{}]", exception.getMessage(), exception); return Result.error( JSONObject.toJSONString( exception.getBindingResult().getAllErrors() @@ -28,4 +31,10 @@ public class GlobalExceptionHandler { ) ); } + + @ExceptionHandler(value = IllegalArgumentException.class) + public Result illegalArgumentExceptionHandler(IllegalArgumentException exception){ + log.error("请求异常:[{}]", exception.getMessage(), exception); + return Result.error(exception.getMessage()); + } } diff --git a/muyu-common/src/main/java/com/muyu/common/remote/system/RemoteUserInfoService.java b/muyu-common/src/main/java/com/muyu/common/remote/system/RemoteUserInfoService.java new file mode 100644 index 0000000..d859b6f --- /dev/null +++ b/muyu-common/src/main/java/com/muyu/common/remote/system/RemoteUserInfoService.java @@ -0,0 +1,30 @@ +package com.muyu.common.remote.system; + +import com.muyu.common.constant.ServerNameConstants; +import com.muyu.common.domain.request.user.UserAddReq; +import com.muyu.common.remote.system.factory.RemoteUserInfoFactory; +import com.muyu.common.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * @author DongZl + * @description: 用户远程调用 + * @Date 2024-1-11 下午 07:42 + */ +@FeignClient( + name = ServerNameConstants.SYSTEM_NAME, + fallbackFactory = RemoteUserInfoFactory.class, + path = "/user" +) +public interface RemoteUserInfoService { + + /** + * 添加用户 + * @return 方法结果集 + */ + @PostMapping() + public Result add(@RequestBody @Validated UserAddReq userAddReq); +} diff --git a/muyu-common/src/main/java/com/muyu/common/remote/system/factory/RemoteUserInfoFactory.java b/muyu-common/src/main/java/com/muyu/common/remote/system/factory/RemoteUserInfoFactory.java new file mode 100644 index 0000000..fb1540f --- /dev/null +++ b/muyu-common/src/main/java/com/muyu/common/remote/system/factory/RemoteUserInfoFactory.java @@ -0,0 +1,34 @@ +package com.muyu.common.remote.system.factory; + +import com.muyu.common.domain.request.user.UserAddReq; +import com.muyu.common.remote.system.RemoteUserInfoService; +import com.muyu.common.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +/** + * @author DongZl + * @description: 远程用户调用熔断 + * @Date 2024-1-11 下午 07:46 + */ +@Log4j2 +@Component +public class RemoteUserInfoFactory implements FallbackFactory { + @Override + public RemoteUserInfoService create (Throwable cause) { + return new RemoteUserInfoService(){ + + /** + * 添加用户 + * @param userAddReq 请求对象 + * @return 方法结果集 + */ + @Override + public Result add (UserAddReq userAddReq) { + log.error("添加用户远程调用失败:[{}-{}]",userAddReq, cause.getMessage(), cause); + return Result.error(); + } + }; + } +} diff --git a/muyu-common/src/main/java/com/muyu/common/result/Result.java b/muyu-common/src/main/java/com/muyu/common/result/Result.java index 1076493..0818c45 100644 --- a/muyu-common/src/main/java/com/muyu/common/result/Result.java +++ b/muyu-common/src/main/java/com/muyu/common/result/Result.java @@ -50,4 +50,20 @@ public class Result implements Serializable { apiResult.setMsg(msg); return apiResult; } + + /** + * 成功 + * @return 成功 true + */ + public boolean isSuccess(){ + return this.code == SUCCESS; + } + + /** + * 失败 + * @return 失败 false + */ + public boolean isError(){ + return !isSuccess(); + } } diff --git a/muyu-modules/muyu-system/src/main/java/com/muyu/system/service/UserInfoServiceImpl.java b/muyu-modules/muyu-system/src/main/java/com/muyu/system/service/UserInfoServiceImpl.java deleted file mode 100644 index 68b7d8b..0000000 --- a/muyu-modules/muyu-system/src/main/java/com/muyu/system/service/UserInfoServiceImpl.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.muyu.system.service; - -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.muyu.common.domain.UserInfo; -import com.muyu.system.mapper.UserInfoMapper; -import org.springframework.stereotype.Service; - -/** - * @author DongZl - * @description: 用户业务实现类 - * @Date 2024-1-10 下午 07:42 - */ -@Service -public class UserInfoServiceImpl extends ServiceImpl - implements UserInfoService { - -} diff --git a/muyu-modules/muyu-system/src/main/java/com/muyu/system/service/impl/UserInfoServiceImpl.java b/muyu-modules/muyu-system/src/main/java/com/muyu/system/service/impl/UserInfoServiceImpl.java new file mode 100644 index 0000000..d98f824 --- /dev/null +++ b/muyu-modules/muyu-system/src/main/java/com/muyu/system/service/impl/UserInfoServiceImpl.java @@ -0,0 +1,29 @@ +package com.muyu.system.service.impl; + +import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.muyu.common.domain.UserInfo; +import com.muyu.system.mapper.UserInfoMapper; +import com.muyu.system.service.UserInfoService; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +/** + * @author DongZl + * @description: 用户业务实现类 + * @Date 2024-1-10 下午 07:42 + */ +@Service +public class UserInfoServiceImpl extends ServiceImpl + implements UserInfoService { + + @Override + public boolean save (UserInfo entity) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(UserInfo::getUserName, entity.getUserName()); + UserInfo userInfo = getOne(queryWrapper); + Assert.isNull(userInfo, "用户已经存在"); + return super.save(entity); + } +}