From 769dac6930f9f6a9286376d19cfca966eadc3b58 Mon Sep 17 00:00:00 2001 From: An Yong Shuai <1539893812@qq.com> Date: Mon, 18 Mar 2024 16:47:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=93=E9=AB=98=E5=85=AD=E6=9C=88=E8=80=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 38 +++++ .idea/.gitignore | 8 + .idea/encodings.xml | 18 +++ .idea/misc.xml | 14 ++ .idea/uiDesigner.xml | 124 +++++++++++++++ .idea/vcs.xml | 6 + bwie-auth/pom.xml | 32 ++++ .../bwie/auth/AuthSpringBootApplication.java | 19 +++ .../bwie/auth/controller/AuthController.java | 68 +++++++++ .../com/bwie/auth/service/AuthService.java | 11 ++ .../auth/service/impl/AuthServiceImpl.java | 70 +++++++++ .../java/com/bwie/auth/vo/AuthRequestVo.java | 21 +++ .../java/com/bwie/auth/vo/AuthResultVo.java | 24 +++ bwie-auth/src/main/resources/bootstrap.yml | 29 ++++ bwie-common/pom.xml | 142 ++++++++++++++++++ .../com/bwie/config/MybitsPlusConfig.java | 26 ++++ .../java/com/bwie/constant/Constants.java | 18 +++ .../java/com/bwie/constant/JwtConstants.java | 27 ++++ .../bwie/constant/ServerNameConstants.java | 15 ++ .../com/bwie/constant/TokenConstants.java | 24 +++ .../src/main/java/com/bwie/domain/Borrow.java | 65 ++++++++ .../src/main/java/com/bwie/domain/User.java | 49 ++++++ .../com/bwie/handler/BizExceptionHandler.java | 61 ++++++++ .../bwie/handler/GlobalExceptionHandler.java | 40 +++++ .../java/com/bwie/remote/user/UserFeign.java | 15 ++ .../bwie/remote/user/errory/ErroryFeign.java | 24 +++ .../main/java/com/bwie/result/PageResult.java | 34 +++++ .../src/main/java/com/bwie/result/Result.java | 68 +++++++++ .../src/main/java/com/bwie/utils/IdUtils.java | 19 +++ .../main/java/com/bwie/utils/JwtUtils.java | 104 +++++++++++++ .../java/com/bwie/utils/SecurityUtils.java | 54 +++++++ .../main/java/com/bwie/utils/StringUtils.java | 68 +++++++++ bwie-elastic/pom.xml | 45 ++++++ .../bwie/es/ElasticSpringBootApplication.java | 55 +++++++ .../java/com/bwie/es/config/EsConfigInit.java | 28 ++++ .../bwie/es/controller/ElasticController.java | 70 +++++++++ .../java/com/bwie/es/feign/SystemFeign.java | 14 ++ .../java/com/bwie/es/lister/RabbitLister.java | 51 +++++++ .../bwie/es/service/ElasticSearchService.java | 12 ++ .../es/service/impl/ElasticServiceImpl.java | 77 ++++++++++ .../com/bwie/es/sync/ElasticSearchSync.java | 62 ++++++++ .../java/com/bwie/es/vo/ElasticSearchVo.java | 16 ++ bwie-elastic/src/main/resources/bootstrap.yml | 41 +++++ bwie-gateway/pom.xml | 44 ++++++ .../gateway/GateWaySpringBootApplication.java | 12 ++ .../gateway/config/GatewaySentinelConfig.java | 71 +++++++++ .../gateway/config/IgnoreWhiteConfig.java | 32 ++++ .../com/bwie/gateway/filters/LoginFilter.java | 81 ++++++++++ .../com/bwie/gateway/utils/GatewayUtils.java | 98 ++++++++++++ bwie-gateway/src/main/resources/bootstrap.yml | 29 ++++ bwie-module/bwie-expressage/pom.xml | 32 ++++ .../sysytem/SystemSpringBootApplication.java | 20 +++ .../sysytem/controller/BorrowController.java | 77 ++++++++++ .../com/bwie/sysytem/feign/ElasticFeign.java | 14 ++ .../com/bwie/sysytem/mapper/BorrowMapper.java | 9 ++ .../bwie/sysytem/service/BorrowService.java | 12 ++ .../service/impl/BorrowServiceImpl.java | 117 +++++++++++++++ .../java/com/bwie/sysytem/vo/BorrowPrice.java | 28 ++++ .../java/com/bwie/sysytem/vo/BorrowVo.java | 47 ++++++ .../src/main/resources/bootstrap.yml | 29 ++++ bwie-module/bwie-user/pom.xml | 32 ++++ .../bwie/user/UserSpringBootApplication.java | 15 ++ .../bwie/user/controller/UserController.java | 30 ++++ .../java/com/bwie/user/mapper/UserMapper.java | 10 ++ .../com/bwie/user/service/UserService.java | 8 + .../user/service/impl/UserServiceImpl.java | 18 +++ .../src/main/resources/bootstrap.yml | 29 ++++ bwie-module/pom.xml | 25 +++ bwie-rabbit/pom.xml | 46 ++++++ .../RabbitMqSpringBootApplication.java | 17 +++ .../bwie/rabbitmq/config/RabbitMQConfig.java | 134 +++++++++++++++++ bwie-rabbit/src/main/resources/bootstrap.yml | 33 ++++ pom.xml | 68 +++++++++ 73 files changed, 3023 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 bwie-auth/pom.xml create mode 100644 bwie-auth/src/main/java/com/bwie/auth/AuthSpringBootApplication.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/vo/AuthRequestVo.java create mode 100644 bwie-auth/src/main/java/com/bwie/auth/vo/AuthResultVo.java create mode 100644 bwie-auth/src/main/resources/bootstrap.yml create mode 100644 bwie-common/pom.xml create mode 100644 bwie-common/src/main/java/com/bwie/config/MybitsPlusConfig.java create mode 100644 bwie-common/src/main/java/com/bwie/constant/Constants.java create mode 100644 bwie-common/src/main/java/com/bwie/constant/JwtConstants.java create mode 100644 bwie-common/src/main/java/com/bwie/constant/ServerNameConstants.java create mode 100644 bwie-common/src/main/java/com/bwie/constant/TokenConstants.java create mode 100644 bwie-common/src/main/java/com/bwie/domain/Borrow.java create mode 100644 bwie-common/src/main/java/com/bwie/domain/User.java create mode 100644 bwie-common/src/main/java/com/bwie/handler/BizExceptionHandler.java create mode 100644 bwie-common/src/main/java/com/bwie/handler/GlobalExceptionHandler.java create mode 100644 bwie-common/src/main/java/com/bwie/remote/user/UserFeign.java create mode 100644 bwie-common/src/main/java/com/bwie/remote/user/errory/ErroryFeign.java create mode 100644 bwie-common/src/main/java/com/bwie/result/PageResult.java create mode 100644 bwie-common/src/main/java/com/bwie/result/Result.java create mode 100644 bwie-common/src/main/java/com/bwie/utils/IdUtils.java create mode 100644 bwie-common/src/main/java/com/bwie/utils/JwtUtils.java create mode 100644 bwie-common/src/main/java/com/bwie/utils/SecurityUtils.java create mode 100644 bwie-common/src/main/java/com/bwie/utils/StringUtils.java create mode 100644 bwie-elastic/pom.xml create mode 100644 bwie-elastic/src/main/java/com/bwie/es/ElasticSpringBootApplication.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/config/EsConfigInit.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/controller/ElasticController.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/feign/SystemFeign.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/lister/RabbitLister.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/service/ElasticSearchService.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/service/impl/ElasticServiceImpl.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/sync/ElasticSearchSync.java create mode 100644 bwie-elastic/src/main/java/com/bwie/es/vo/ElasticSearchVo.java create mode 100644 bwie-elastic/src/main/resources/bootstrap.yml create mode 100644 bwie-gateway/pom.xml create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/GateWaySpringBootApplication.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/filters/LoginFilter.java create mode 100644 bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java create mode 100644 bwie-gateway/src/main/resources/bootstrap.yml create mode 100644 bwie-module/bwie-expressage/pom.xml create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/SystemSpringBootApplication.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/controller/BorrowController.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/feign/ElasticFeign.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/mapper/BorrowMapper.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/BorrowService.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/impl/BorrowServiceImpl.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowPrice.java create mode 100644 bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowVo.java create mode 100644 bwie-module/bwie-expressage/src/main/resources/bootstrap.yml create mode 100644 bwie-module/bwie-user/pom.xml create mode 100644 bwie-module/bwie-user/src/main/java/com/bwie/user/UserSpringBootApplication.java create mode 100644 bwie-module/bwie-user/src/main/java/com/bwie/user/controller/UserController.java create mode 100644 bwie-module/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java create mode 100644 bwie-module/bwie-user/src/main/java/com/bwie/user/service/UserService.java create mode 100644 bwie-module/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java create mode 100644 bwie-module/bwie-user/src/main/resources/bootstrap.yml create mode 100644 bwie-module/pom.xml create mode 100644 bwie-rabbit/pom.xml create mode 100644 bwie-rabbit/src/main/java/com/bwie/rabbitmq/RabbitMqSpringBootApplication.java create mode 100644 bwie-rabbit/src/main/java/com/bwie/rabbitmq/config/RabbitMQConfig.java create mode 100644 bwie-rabbit/src/main/resources/bootstrap.yml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..57fbf7f --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bwie-auth/pom.xml b/bwie-auth/pom.xml new file mode 100644 index 0000000..eed50b8 --- /dev/null +++ b/bwie-auth/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + + + bwie-auth + + + 17 + 17 + UTF-8 + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/bwie-auth/src/main/java/com/bwie/auth/AuthSpringBootApplication.java b/bwie-auth/src/main/java/com/bwie/auth/AuthSpringBootApplication.java new file mode 100644 index 0000000..814880c --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/AuthSpringBootApplication.java @@ -0,0 +1,19 @@ +package com.bwie.auth; + +import com.bwie.config.MybitsPlusConfig; +import com.bwie.handler.GlobalExceptionHandler; +import com.bwie.remote.user.errory.ErroryFeign; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Import; + +@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) +@EnableFeignClients(basePackages = {"com.bwie"}) +@Import(value = {MybitsPlusConfig.class, GlobalExceptionHandler.class, ErroryFeign.class}) +public class AuthSpringBootApplication { + public static void main(String[] args) { + SpringApplication.run(AuthSpringBootApplication.class); + } +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java b/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java new file mode 100644 index 0000000..ee435ec --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/controller/AuthController.java @@ -0,0 +1,68 @@ +package com.bwie.auth.controller; + +import com.bwie.auth.service.AuthService; +import com.bwie.auth.vo.AuthRequestVo; +import com.bwie.auth.vo.AuthResultVo; +import com.bwie.constant.TokenConstants; +import com.bwie.domain.User; +import com.bwie.handler.BizExceptionHandler; +import com.bwie.result.Result; +import com.bwie.utils.JwtUtils; +import com.bwie.utils.StringUtils; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@RequestMapping("/login") +public class AuthController { + private final AuthService authService; + private final RedisTemplate redisTemplate; + private final HttpServletRequest request; + + public AuthController(AuthService authService, RedisTemplate redisTemplate, HttpServletRequest request) { + this.authService = authService; + this.redisTemplate = redisTemplate; + this.request = request; + } + + /** + * 登录接口 + * @param authRequestVo + * @return + */ + @PostMapping("/login") + public Result login(@RequestBody AuthRequestVo authRequestVo){ + if(StringUtils.isNull(authRequestVo.getUserName())){ + throw new BizExceptionHandler(408,"用户名不能为空"); + } + if(StringUtils.isNull(authRequestVo.getUserPwd())){ + throw new BizExceptionHandler(408,"密码不能为空"); + } + AuthResultVo authResultVo = authService.loginName(authRequestVo); + return Result.success(authResultVo); + } + + /** + * 查询登录人用户信息 + * @return + */ + @GetMapping("/info") + public Result info(){ + User info = authService.info(); + return Result.success(info); + } + + /** + * 登出 + * @return + */ + @PostMapping("/logout") + public Result logOut(){ + String token = request.getHeader("token"); + String userKey = JwtUtils.getUserKey(token); + redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY+userKey); + return Result.success(); + } +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java b/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java new file mode 100644 index 0000000..28d0baa --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/service/AuthService.java @@ -0,0 +1,11 @@ +package com.bwie.auth.service; + +import com.bwie.auth.vo.AuthRequestVo; +import com.bwie.auth.vo.AuthResultVo; +import com.bwie.domain.User; +import com.bwie.remote.user.UserFeign; + +public interface AuthService { + AuthResultVo loginName(AuthRequestVo authRequestVo); + User info(); +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java b/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java new file mode 100644 index 0000000..8a59b51 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/service/impl/AuthServiceImpl.java @@ -0,0 +1,70 @@ +package com.bwie.auth.service.impl; + +import com.alibaba.fastjson.JSON; +import com.bwie.auth.service.AuthService; +import com.bwie.auth.vo.AuthRequestVo; +import com.bwie.auth.vo.AuthResultVo; +import com.bwie.constant.JwtConstants; +import com.bwie.constant.TokenConstants; +import com.bwie.domain.User; +import com.bwie.handler.BizExceptionHandler; +import com.bwie.remote.user.UserFeign; +import com.bwie.result.Result; +import com.bwie.utils.IdUtils; +import com.bwie.utils.JwtUtils; +import com.bwie.utils.StringUtils; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.concurrent.TimeUnit; + +@Service +public class AuthServiceImpl implements AuthService { + private final RedisTemplate redisTemplate; + private final UserFeign userFeign; + private final HttpServletRequest request; + + public AuthServiceImpl(RedisTemplate redisTemplate, UserFeign userFeign, HttpServletRequest request) { + this.redisTemplate = redisTemplate; + this.userFeign = userFeign; + this.request = request; + } + + @Override + public AuthResultVo loginName(AuthRequestVo authRequestVo) { + Result byName = userFeign.getByName(authRequestVo.getUserName()); + if(!byName.isSuccess()){ + throw new BizExceptionHandler(408,"网络不佳,请稍后再试"); + } + User user = byName.getData(); + if(StringUtils.isNull(user)){ + throw new BizExceptionHandler(408,"未检测到该用户"); + } + if(!user.getUserPwd().equals(authRequestVo.getUserPwd())){ + throw new BizExceptionHandler(408,"账户明明或密码输入错误"); + } + HashMap map = new HashMap<>(); + String userKey = IdUtils.genId(); + map.put(JwtConstants.USER_KEY,userKey); + map.put(JwtConstants.DETAILS_USER_ID,user.getUserId()); + map.put(JwtConstants.DETAILS_USERNAME,user.getUserName()); + String token = JwtUtils.createToken(map); + redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSON.toJSONString(user),5, TimeUnit.MINUTES); + return AuthResultVo.builder() + .token(token) + .time(TokenConstants.EXPIRATION) + .build(); + + } + + @Override + public User info() { + String token = request.getHeader("token"); + String userKey = JwtUtils.getUserKey(token); + Object o = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + User user = JSON.parseObject(o.toString(), User.class); + return user; + } +} diff --git a/bwie-auth/src/main/java/com/bwie/auth/vo/AuthRequestVo.java b/bwie-auth/src/main/java/com/bwie/auth/vo/AuthRequestVo.java new file mode 100644 index 0000000..ab5daf9 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/vo/AuthRequestVo.java @@ -0,0 +1,21 @@ +package com.bwie.auth.vo; + +import lombok.Data; + +/** + * 登录信息 + */ +@Data +public class AuthRequestVo { + /** + * 登录名 + */ + private String userName; + /** + * 登录密码 + */ + private String userPwd; +} + + + diff --git a/bwie-auth/src/main/java/com/bwie/auth/vo/AuthResultVo.java b/bwie-auth/src/main/java/com/bwie/auth/vo/AuthResultVo.java new file mode 100644 index 0000000..b74ec24 --- /dev/null +++ b/bwie-auth/src/main/java/com/bwie/auth/vo/AuthResultVo.java @@ -0,0 +1,24 @@ +package com.bwie.auth.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 登录返会信息 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class AuthResultVo { + /** + * 返回token + */ + private String token; + /** + * 持续时间 + */ + private Long time; +} diff --git a/bwie-auth/src/main/resources/bootstrap.yml b/bwie-auth/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..cec4caa --- /dev/null +++ b/bwie-auth/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# Tomcat +server: + port: 9005 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-auth + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.223.156.14:8848 + config: + # 配置中心地址 + server-addr: 124.223.156.14:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-common/pom.xml b/bwie-common/pom.xml new file mode 100644 index 0000000..bc5777d --- /dev/null +++ b/bwie-common/pom.xml @@ -0,0 +1,142 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + + + bwie-common + + + 17 + 17 + UTF-8 + + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + com.alibaba + fastjson + 1.2.80 + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + mysql + mysql-connector-java + 8.0.33 + + + + com.baomidou + mybatis-plus-boot-starter + 3.5.4.1 + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + org.springframework.boot + spring-boot-starter-validation + + + + org.apache.commons + commons-lang3 + + + + org.projectlombok + lombok + + + + cn.hutool + hutool-all + 5.8.3 + + + + + com.aliyun.oss + aliyun-sdk-oss + 3.12.0 + + + + + + com.github.tobato + fastdfs-client + 1.26.5 + + + org.springframework.boot + spring-boot-starter-amqp + + + + org.springframework.boot + spring-boot-starter-test + + + + diff --git a/bwie-common/src/main/java/com/bwie/config/MybitsPlusConfig.java b/bwie-common/src/main/java/com/bwie/config/MybitsPlusConfig.java new file mode 100644 index 0000000..8faa5d8 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/config/MybitsPlusConfig.java @@ -0,0 +1,26 @@ +package com.bwie.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MybitsPlusConfig { + public MybitsPlusConfig(){ + System.out.println("初始化>>>>>"); + } + + /** + * 添加分页插件 + */ + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor(){ + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); + return interceptor; + } + + +} diff --git a/bwie-common/src/main/java/com/bwie/constant/Constants.java b/bwie-common/src/main/java/com/bwie/constant/Constants.java new file mode 100644 index 0000000..98b84a8 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/constant/Constants.java @@ -0,0 +1,18 @@ +package com.bwie.constant; + +/** + * @description: 系统常量 + * @author DongZl + */ +public class Constants { + /** + * 成功标记 + */ + public static final Integer SUCCESS = 200; + public static final String SUCCESS_MSG = "操作成功"; + /** + * 失败标记 + */ + public static final Integer ERROR = 500; + public static final String ERROR_MSG = "操作异常"; +} diff --git a/bwie-common/src/main/java/com/bwie/constant/JwtConstants.java b/bwie-common/src/main/java/com/bwie/constant/JwtConstants.java new file mode 100644 index 0000000..735eecc --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/constant/JwtConstants.java @@ -0,0 +1,27 @@ +package com.bwie.constant; + +/** + * @author DongZl + * @description: Jwt常量 + */ +public class JwtConstants { + /** + * 用户ID字段 + */ + public static final String DETAILS_USER_ID = "user_id"; + + /** + * 用户名字段 + */ + public static final String DETAILS_USERNAME = "username"; + + /** + * 用户标识 + */ + public static final String USER_KEY = "user_key"; + + /** + * 令牌秘钥 + */ + public final static String SECRET = "abcdefghijklmnopqrstuvwxyz"; +} diff --git a/bwie-common/src/main/java/com/bwie/constant/ServerNameConstants.java b/bwie-common/src/main/java/com/bwie/constant/ServerNameConstants.java new file mode 100644 index 0000000..0cdaef3 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/constant/ServerNameConstants.java @@ -0,0 +1,15 @@ +package com.bwie.constant; + +/** + * @author DongZl + * @description: 服务名称常量 + * @Date 2024-1-11 下午 07:43 + */ +public class ServerNameConstants { + + /** + * 系统服务名称 + */ + public final static String SYSTEM_NAME = "bwie-user"; + +} diff --git a/bwie-common/src/main/java/com/bwie/constant/TokenConstants.java b/bwie-common/src/main/java/com/bwie/constant/TokenConstants.java new file mode 100644 index 0000000..5121146 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/constant/TokenConstants.java @@ -0,0 +1,24 @@ +package com.bwie.constant; + +/** + * @author DongZl + * @description: 令牌常量 + */ +public class TokenConstants { + /** + * 缓存有效期,默认720(分钟) + */ + public final static long EXPIRATION = 720; + /** + * 缓存刷新时间,默认120(分钟) + */ + public final static long REFRESH_TIME = 120; + /** + * 权限缓存前缀 + */ + public final static String LOGIN_TOKEN_KEY = "login_tokens:"; + /** + * token标识 + */ + public static final String TOKEN = "token"; +} diff --git a/bwie-common/src/main/java/com/bwie/domain/Borrow.java b/bwie-common/src/main/java/com/bwie/domain/Borrow.java new file mode 100644 index 0000000..adfb5ad --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/domain/Borrow.java @@ -0,0 +1,65 @@ +package com.bwie.domain; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * 借款记录 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@TableName(value = "borrow") +public class Borrow { + /** + * 借款编号 + */ + private Long borrowId; + /** + * 借款时间 + */ + private Date borrowDate; + /** + * 价款金额 + */ + private BigDecimal borrowPrice; + /** + * 期数 + */ + private Long borrowNum; + /** + * 最近还款时间 + */ + private Date borrowTime; + /** + * 已还本金 + */ + private BigDecimal borrowBackPrice; + /** + * 未还本金 + */ + private BigDecimal borrowAuthPrice; + /** + * 利息 + */ + private String borrowInsert; + /** + * 手续费 + */ + private BigDecimal borrowMoney; + /** + * 放款机构 + */ + private String borrowPlace; + /** + * 状态 + */ + private Long borrowFlag; +} diff --git a/bwie-common/src/main/java/com/bwie/domain/User.java b/bwie-common/src/main/java/com/bwie/domain/User.java new file mode 100644 index 0000000..0b0b3f4 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/domain/User.java @@ -0,0 +1,49 @@ +package com.bwie.domain; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@TableName(value = "user") +public class User { + /** + * 用户编号 + */ + private Long userId; + /** + * 用户名 + */ + private String userName; + /** + * 密码 + */ + private String userPwd; + /** + * 盐值 + */ + private String userSalt; + /** + * 手机号 + */ + private String userPhone; + /** + * 身份 + */ + private Long userRoal; + /** + * 可借款金额 + */ + private BigDecimal userPrice; + /** + * 用户余额 + */ + private BigDecimal userMoney; +} diff --git a/bwie-common/src/main/java/com/bwie/handler/BizExceptionHandler.java b/bwie-common/src/main/java/com/bwie/handler/BizExceptionHandler.java new file mode 100644 index 0000000..6753e1b --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/handler/BizExceptionHandler.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2016-2019 人人开源 All rights reserved. + * + * https://www.renren.io + * + * 版权所有,侵权必究! + */ + +package com.bwie.handler; + +/** + * 自定义异常 + * + * @author Mark sunlightcs@gmail.com + */ +public class BizExceptionHandler extends RuntimeException { + private static final long serialVersionUID = 1L; + + private String msg; + private int code = 500; + + public BizExceptionHandler(String msg) { + super(msg); + this.msg = msg; + } + + public BizExceptionHandler(String msg, Throwable e) { + super(msg, e); + this.msg = msg; + } + + public BizExceptionHandler(int code, String msg) { + super(msg); + this.msg = msg; + this.code = code; + } + + public BizExceptionHandler(String msg, int code, Throwable e) { + super(msg, e); + this.msg = msg; + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + +} diff --git a/bwie-common/src/main/java/com/bwie/handler/GlobalExceptionHandler.java b/bwie-common/src/main/java/com/bwie/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..4c91a5a --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/handler/GlobalExceptionHandler.java @@ -0,0 +1,40 @@ +package com.bwie.handler; + +import com.alibaba.fastjson.JSONObject; +import com.bwie.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +@Configuration +@Log4j2 +public class GlobalExceptionHandler { + + /** + * 全局的异常处理 + * @param exception + * @return + */ + @ExceptionHandler(value = MethodArgumentNotValidException.class) + public Result runtimeException(MethodArgumentNotValidException exception){ + return Result.error( + JSONObject.toJSONString( + exception.getBindingResult().getAllErrors() + .stream() + .map(ObjectError::getDefaultMessage) + .toList() + ) + ); + } + +@ExceptionHandler(value = IllegalArgumentException.class) + public Result illegalArgumentExceptionHandler(IllegalArgumentException exception){ + log.error("请求异常:[{}]", exception.getMessage(), exception); + return Result.error(exception.getMessage()); + } + +} diff --git a/bwie-common/src/main/java/com/bwie/remote/user/UserFeign.java b/bwie-common/src/main/java/com/bwie/remote/user/UserFeign.java new file mode 100644 index 0000000..98b9578 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/remote/user/UserFeign.java @@ -0,0 +1,15 @@ +package com.bwie.remote.user; + +import com.bwie.constant.ServerNameConstants; +import com.bwie.domain.User; +import com.bwie.remote.user.errory.ErroryFeign; +import com.bwie.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +@FeignClient(name = ServerNameConstants.SYSTEM_NAME,path = "/user",fallbackFactory = ErroryFeign.class) +public interface UserFeign { + @PostMapping("/getByName/{userName}") + public Result getByName(@PathVariable String userName); +} diff --git a/bwie-common/src/main/java/com/bwie/remote/user/errory/ErroryFeign.java b/bwie-common/src/main/java/com/bwie/remote/user/errory/ErroryFeign.java new file mode 100644 index 0000000..8996ae8 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/remote/user/errory/ErroryFeign.java @@ -0,0 +1,24 @@ +package com.bwie.remote.user.errory; + +import com.bwie.domain.User; +import com.bwie.remote.user.UserFeign; +import com.bwie.result.Result; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +@Component +@Log4j2 +public class ErroryFeign implements FallbackFactory { + + @Override + public UserFeign create(Throwable cause) { + return new UserFeign() { + @Override + public Result getByName(String userName) { + log.error("登录查询用户名feign调用失败"); + return Result.error(); + } + }; + } +} diff --git a/bwie-common/src/main/java/com/bwie/result/PageResult.java b/bwie-common/src/main/java/com/bwie/result/PageResult.java new file mode 100644 index 0000000..4f367f4 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/result/PageResult.java @@ -0,0 +1,34 @@ +package com.bwie.result; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * @author DongZl + * @description: 列表返回结果集 + */ +@Data +public class PageResult implements Serializable { + /** + * 总条数 + */ + private long total; + /** + * 结果集合 + */ + private List list; + public PageResult() { + } + public PageResult(long total, List list) { + this.total = total; + this.list = list; + } + public static PageResult toPageResult(long total, List list){ + return new PageResult(total , list); + } + public static Result> toResult(long total, List list){ + return Result.success(PageResult.toPageResult(total,list)); + } +} diff --git a/bwie-common/src/main/java/com/bwie/result/Result.java b/bwie-common/src/main/java/com/bwie/result/Result.java new file mode 100644 index 0000000..2365f8b --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/result/Result.java @@ -0,0 +1,68 @@ +package com.bwie.result; + +import com.bwie.constant.Constants; +import lombok.Data; + +import java.io.Serializable; + +/** + * @description: 响应信息主体 + * @author DongZl + */ +@Data +public class Result implements Serializable { + private static final long serialVersionUID = 1L; + /** 成功 */ + public static final int SUCCESS = Constants.SUCCESS; + /** 失败 */ + public static final int FAIL = Constants.ERROR; + private int code; + private String msg; + private T data; + public static Result success() { + return restResult(null, SUCCESS, Constants.SUCCESS_MSG); + } + public static Result success(T data) { + return restResult(data, SUCCESS, Constants.SUCCESS_MSG); + } + public static Result success(T data, String msg) { + return restResult(data, SUCCESS, msg); + } + public static Result error() { + return restResult(null, FAIL, Constants.ERROR_MSG); + } + public static Result error(String msg) { + return restResult(null, FAIL, msg); + } + public static Result error(T data) { + return restResult(data, FAIL, Constants.ERROR_MSG); + } + public static Result error(T data, String msg) { + return restResult(data, FAIL, msg); + } + public static Result error(int code, String msg) { + return restResult(null, code, msg); + } + private static Result restResult(T data, int code, String msg) { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } + /** + * 成功 + * @return 成功 true + */ + public boolean isSuccess(){ + return this.code == SUCCESS; + } + + /** + * 失败 + * @return 失败 false + */ + public boolean isError(){ + return !isSuccess(); + } +} diff --git a/bwie-common/src/main/java/com/bwie/utils/IdUtils.java b/bwie-common/src/main/java/com/bwie/utils/IdUtils.java new file mode 100644 index 0000000..91ae71b --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/utils/IdUtils.java @@ -0,0 +1,19 @@ +package com.bwie.utils; + +import java.util.UUID; + +/** + * @author DongZl + * @description: ID生成工具类 + * @Date 2024-1-10 下午 08:26 + */ +public class IdUtils { + + /** + * 生成UUID + * @return UUID + */ + public static String genId(){ + return UUID.randomUUID().toString().replace("-", ""); + } +} diff --git a/bwie-common/src/main/java/com/bwie/utils/JwtUtils.java b/bwie-common/src/main/java/com/bwie/utils/JwtUtils.java new file mode 100644 index 0000000..1aa1b34 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/utils/JwtUtils.java @@ -0,0 +1,104 @@ +package com.bwie.utils; + +import com.bwie.constant.JwtConstants; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; + +import java.util.Map; + +/** + * @description: Jwt工具类 + * @author DongZl + */ +public class JwtUtils { + + public static String secret = JwtConstants.SECRET; + /** + * 从数据声明生成令牌 + * + * @param claims 数据声明 + * @return 令牌 + */ + public static String createToken(Map claims){ + String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact(); + return token; + } + /** + * 从令牌中获取数据声明 + * + * @param token 令牌 + * @return 数据声明 + */ + public static Claims parseToken(String token){ + return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + } + /** + * 根据令牌获取用户标识 + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserKey(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户标识 + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserKey(Claims claims){ + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户ID + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserId(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据身份信息获取用户ID + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserId(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据令牌获取用户名 + * + * @param token 令牌 + * @return 用户名 + */ + public static String getUserName(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取用户名 + * + * @param claims 身份信息 + * @return 用户名 + */ + public static String getUserName(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取键值 + * + * @param claims 身份信息 + * @param key 键 + * @return 值 + */ + public static String getValue(Claims claims, String key){ + Object obj = claims.get(key); + return obj == null ? "" : obj.toString(); + } +} diff --git a/bwie-common/src/main/java/com/bwie/utils/SecurityUtils.java b/bwie-common/src/main/java/com/bwie/utils/SecurityUtils.java new file mode 100644 index 0000000..b5b5f80 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/utils/SecurityUtils.java @@ -0,0 +1,54 @@ +package com.bwie.utils; + + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +/** + * 安全服务工具类 + * + * @author ruoyi + */ +public class SecurityUtils { + + + /** + * 生成BCryptPasswordEncoder密码 + * + * @param password 密码 + * + * @return 加密字符串 + */ + public static String encryptPassword (String password, String salt) { + return encryptMD5(password, salt); + } + + /** + * 判断密码是否相同 + * + * @param rawPassword 真实密码 + * @param encodedPassword 加密后字符 + * + * @return 结果 + */ + public static boolean matchesPassword (String rawPassword, String salt, String encodedPassword) { + return encryptMD5(rawPassword, salt).equals(encodedPassword); + } + + /** + * 计算字符串的MD5加密值,并返回Base64编码的字符串。 + * @param password 要加密的字符串 + * @return 加密后的Base64编码字符串 + */ + public static String encryptMD5(String password, String salt) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update((password + salt).getBytes()); // 加盐处理 + byte[] digest = md.digest(); + return Base64.getEncoder().encodeToString(digest); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } +} diff --git a/bwie-common/src/main/java/com/bwie/utils/StringUtils.java b/bwie-common/src/main/java/com/bwie/utils/StringUtils.java new file mode 100644 index 0000000..6c3bc6b --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/utils/StringUtils.java @@ -0,0 +1,68 @@ +package com.bwie.utils; + +import org.springframework.util.AntPathMatcher; + +import java.util.Collection; +import java.util.List; + +/** + * @author DongZl + * @description: 字符串处理工具类 + */ +public class StringUtils extends org.apache.commons.lang3.StringUtils { + + /** + * * 判断一个对象是否为空 + * + * @param object Object + * @return true:为空 false:非空 + */ + public static boolean isNull(Object object) { + return object == null; + } + + /** + * * 判断一个Collection是否为空, 包含List,Set,Queue + * + * @param coll 要判断的Collection + * @return true:为空 false:非空 + */ + public static boolean isEmpty(Collection coll) { + return isNull(coll) || coll.isEmpty(); + } + + /** + * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串 + * + * @param str 指定字符串 + * @param strs 需要检查的字符串数组 + * @return 是否匹配 + */ + public static boolean matches(String str, List strs) { + if (isEmpty(str) || isEmpty(strs)) { + return false; + } + for (String pattern : strs) { + if (isMatch(pattern, str)) + { + return true; + } + } + return false; + } + + /** + * 判断url是否与规则配置: + * ? 表示单个字符; + * * 表示一层路径内的任意字符串,不可跨层级; + * ** 表示任意层路径; + * + * @param pattern 匹配规则 + * @param url 需要匹配的url + * @return + */ + public static boolean isMatch(String pattern, String url) { + AntPathMatcher matcher = new AntPathMatcher(); + return matcher.match(pattern, url); + } +} diff --git a/bwie-elastic/pom.xml b/bwie-elastic/pom.xml new file mode 100644 index 0000000..a2ff5de --- /dev/null +++ b/bwie-elastic/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + + + bwie-elastic + + + 17 + 17 + UTF-8 + + + + com.bwie + bwie-common + + + org.springframework.boot + spring-boot-starter-web + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/bwie-elastic/src/main/java/com/bwie/es/ElasticSpringBootApplication.java b/bwie-elastic/src/main/java/com/bwie/es/ElasticSpringBootApplication.java new file mode 100644 index 0000000..d97c248 --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/ElasticSpringBootApplication.java @@ -0,0 +1,55 @@ +package com.bwie.es; + +import com.alibaba.fastjson.JSON; +import com.bwie.config.MybitsPlusConfig; +import com.bwie.domain.Borrow; +import com.bwie.es.feign.SystemFeign; +import com.bwie.handler.BizExceptionHandler; +import com.bwie.handler.GlobalExceptionHandler; +import com.bwie.result.Result; +import lombok.extern.log4j.Log4j2; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.xcontent.XContentType; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Import; + +import java.util.List; + +@Log4j2 +@SpringBootApplication +@EnableFeignClients(basePackages = "com.bwie") +@Import(value = {MybitsPlusConfig.class, GlobalExceptionHandler.class}) +public class ElasticSpringBootApplication implements ApplicationRunner { + public ElasticSpringBootApplication(SystemFeign systemFeign, RestHighLevelClient restHighLevelClient) { + this.systemFeign = systemFeign; + this.restHighLevelClient = restHighLevelClient; + } + + public static void main(String[] args) { + SpringApplication.run(ElasticSpringBootApplication.class); + } + private final SystemFeign systemFeign; + private final RestHighLevelClient restHighLevelClient; + @Override + public void run(ApplicationArguments args) throws Exception { + log.info("这是预加载的方法"); + Result> listResult = systemFeign.showList(); + if(listResult.isSuccess()!=true){ + throw new BizExceptionHandler(408,"网络不好,请稍后再试"); + } + List borrows = listResult.getData(); + BulkRequest bulkRequest = new BulkRequest("borrow"); + for (Borrow borrow : borrows) { + IndexRequest source = new IndexRequest("borrow").id(borrow.getBorrowId() + "").source(JSON.toJSONString(borrow), XContentType.JSON); + bulkRequest.add(source); + } + restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); + } +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/config/EsConfigInit.java b/bwie-elastic/src/main/java/com/bwie/es/config/EsConfigInit.java new file mode 100644 index 0000000..522b625 --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/config/EsConfigInit.java @@ -0,0 +1,28 @@ +package com.bwie.es.config; + +import lombok.Data; +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "es") +@Data +public class EsConfigInit { + + private String host; + private int port; + private String scheme; + + + @Bean + public RestHighLevelClient initRestHighLevelClient(){ + + return new RestHighLevelClient(RestClient.builder(new HttpHost(host,port,scheme))); + } + +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/controller/ElasticController.java b/bwie-elastic/src/main/java/com/bwie/es/controller/ElasticController.java new file mode 100644 index 0000000..036a9d8 --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/controller/ElasticController.java @@ -0,0 +1,70 @@ +package com.bwie.es.controller; + +import com.alibaba.fastjson.JSON; +import com.bwie.constant.TokenConstants; +import com.bwie.domain.Borrow; +import com.bwie.domain.User; +import com.bwie.es.service.ElasticSearchService; +import com.bwie.es.vo.ElasticSearchVo; +import com.bwie.result.Result; +import com.bwie.utils.JwtUtils; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.data.redis.core.RedisTemplate; +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; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; + +@RestController +@RequestMapping("/elastic") +public class ElasticController { + private final ElasticSearchService elasticSearchService; + private final RedisTemplate redisTemplate; + private final HttpServletRequest request; + private final RabbitTemplate rabbitTemplate; + + public ElasticController(ElasticSearchService elasticSearchService, RedisTemplate redisTemplate, HttpServletRequest request, RabbitTemplate rabbitTemplate) { + this.elasticSearchService = elasticSearchService; + this.redisTemplate = redisTemplate; + this.request = request; + this.rabbitTemplate = rabbitTemplate; + } + + /** + * 增量同步 + * @param borrowList + */ + @PostMapping("/addElastic") + public void addElasticRequest(@RequestBody List borrowList){ + elasticSearchService.addElasticSearch(borrowList); + } + + /** + * 展示elastic列表 + * @param elasticSearchVo + * @return + */ + @PostMapping("/showElasticList") + public Result showElasticList(@RequestBody ElasticSearchVo elasticSearchVo){ + List borrows = elasticSearchService.borrowList(elasticSearchVo); + return Result.success(borrows); + } + + /** + * 点击抢单发送短信 + * @return + */ + @PostMapping("/buy") + public Result buyGood(){ + String token = request.getHeader("token"); + String userKey = JwtUtils.getUserKey(token); + Object o = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey); + User user = JSON.parseObject(o.toString(), User.class); + rabbitTemplate.convertAndSend("msgExchange","msgKey",JSON.toJSONString(user)); + return Result.success(); + } + +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/feign/SystemFeign.java b/bwie-elastic/src/main/java/com/bwie/es/feign/SystemFeign.java new file mode 100644 index 0000000..adf3e23 --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/feign/SystemFeign.java @@ -0,0 +1,14 @@ +package com.bwie.es.feign; + +import com.bwie.domain.Borrow; +import com.bwie.result.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; + +@FeignClient(name = "bwie-expressage",path = "/list") +public interface SystemFeign { + @PostMapping("/showList") + public Result> showList(); +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/lister/RabbitLister.java b/bwie-elastic/src/main/java/com/bwie/es/lister/RabbitLister.java new file mode 100644 index 0000000..13481ab --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/lister/RabbitLister.java @@ -0,0 +1,51 @@ +package com.bwie.es.lister; + +import com.alibaba.fastjson.JSON; +import com.bwie.domain.User; +import com.rabbitmq.client.Channel; +import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +@Component +@Log4j2 +@RabbitListener(queues = "msgQueue") +public class RabbitLister { + private final RedisTemplate redisTemplate; + + public RabbitLister(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + + @RabbitHandler + public void sendMsg(Channel channel, Message message,String msg){ + log.info("消费者监听到的消息为: "+msg); + User user = JSON.parseObject(msg, User.class); + log.info("消费者监听到的消息为: "+user); + try { + if(redisTemplate.hasKey(user.getUserId())){ + log.info("消息已经消费过了"); + channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); + } + System.out.println("消息发送成功: "+msg); + channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); + redisTemplate.opsForValue().set(user.getUserId(),JSON.toJSONString(user),6, TimeUnit.HOURS); + } + catch (IOException e) { + System.out.println("消息消费失败,重新进入队列"); + try { + channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + throw new RuntimeException(e); + } + } +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/service/ElasticSearchService.java b/bwie-elastic/src/main/java/com/bwie/es/service/ElasticSearchService.java new file mode 100644 index 0000000..6ac481f --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/service/ElasticSearchService.java @@ -0,0 +1,12 @@ +package com.bwie.es.service; + +import com.bwie.domain.Borrow; +import com.bwie.es.vo.ElasticSearchVo; + +import java.util.List; + +public interface ElasticSearchService { + void addElasticSearch(List borrowList); + + List borrowList(ElasticSearchVo elasticSearchVo); +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/service/impl/ElasticServiceImpl.java b/bwie-elastic/src/main/java/com/bwie/es/service/impl/ElasticServiceImpl.java new file mode 100644 index 0000000..2fb59a5 --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/service/impl/ElasticServiceImpl.java @@ -0,0 +1,77 @@ +package com.bwie.es.service.impl; + +import com.alibaba.fastjson.JSON; +import com.bwie.domain.Borrow; +import com.bwie.es.service.ElasticSearchService; +import com.bwie.es.vo.ElasticSearchVo; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.index.Index; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@Service +public class ElasticServiceImpl implements ElasticSearchService { + private final RestHighLevelClient restHighLevelClient; + + public ElasticServiceImpl(RestHighLevelClient restHighLevelClient) { + this.restHighLevelClient = restHighLevelClient; + } + + @Override + public void addElasticSearch(List borrowList) { + BulkRequest bulkRequest = new BulkRequest("borrow"); + for (Borrow borrow : borrowList) { + IndexRequest source = new IndexRequest("borrow").id(borrow.getBorrowId() + "").source(JSON.toJSONString(borrow), XContentType.JSON); + bulkRequest.add(source); + } + try { + restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public List borrowList(ElasticSearchVo elasticSearchVo) { + SearchRequest searchRequest = new SearchRequest("borrow"); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); + if(elasticSearchVo.getStartMoney()!=null){ + boolQueryBuilder.must(QueryBuilders.rangeQuery("borrowPrice").gte(elasticSearchVo.getStartMoney())); + } + if(elasticSearchVo.getEndMoney()!=null){ + boolQueryBuilder.must(QueryBuilders.rangeQuery("borrowPrice").lte(elasticSearchVo.getEndMoney())); + } + searchSourceBuilder.query(boolQueryBuilder); + searchRequest.source(searchSourceBuilder); + ArrayList borrows = new ArrayList<>(); + try { + SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); + SearchHits hits = response.getHits(); + ObjectMapper objectMapper = new ObjectMapper(); + for (SearchHit hit : hits) { + String sourceAsString = hit.getSourceAsString(); + Borrow borrow = objectMapper.readValue(sourceAsString, Borrow.class); + borrows.add(borrow); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return borrows; + } +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/sync/ElasticSearchSync.java b/bwie-elastic/src/main/java/com/bwie/es/sync/ElasticSearchSync.java new file mode 100644 index 0000000..ed8fe3c --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/sync/ElasticSearchSync.java @@ -0,0 +1,62 @@ +package com.bwie.es.sync; + +import com.alibaba.fastjson.JSON; +import com.bwie.domain.Borrow; +import com.bwie.es.feign.SystemFeign; +import com.bwie.es.vo.ElasticSearchVo; +import com.bwie.handler.BizExceptionHandler; +import com.bwie.result.Result; +import lombok.extern.log4j.Log4j2; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.index.Index; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.reindex.DeleteByQueryRequest; +import org.elasticsearch.xcontent.XContentType; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestBody; + +import java.io.IOException; +import java.util.List; + +@Component +@Log4j2 +public class ElasticSearchSync { + private final SystemFeign systemFeign; + private final RestHighLevelClient restHighLevelClient; + + public ElasticSearchSync(SystemFeign systemFeign, RestHighLevelClient restHighLevelClient) { + this.systemFeign = systemFeign; + this.restHighLevelClient = restHighLevelClient; + } + + @Scheduled(cron = "0 0/10 * * * ?") + public void addElastic(){ + log.info("这是定时任务执行的方法"); + DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest("borrow"); + deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery()); + try { + restHighLevelClient.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + BulkRequest bulkRequest = new BulkRequest("borrow"); + Result> listResult = systemFeign.showList(); + if(listResult.isSuccess()!=true){ + throw new BizExceptionHandler(408,"网络不好,请稍后再试"); + } + List borrows = listResult.getData(); + for (Borrow borrow : borrows) { + IndexRequest source = new IndexRequest("borrow").id(borrow.getBorrowId() + "").source(JSON.toJSONString(borrow), XContentType.JSON); + bulkRequest.add(source); + } + try { + restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/bwie-elastic/src/main/java/com/bwie/es/vo/ElasticSearchVo.java b/bwie-elastic/src/main/java/com/bwie/es/vo/ElasticSearchVo.java new file mode 100644 index 0000000..024bd46 --- /dev/null +++ b/bwie-elastic/src/main/java/com/bwie/es/vo/ElasticSearchVo.java @@ -0,0 +1,16 @@ +package com.bwie.es.vo; + +import com.bwie.domain.Borrow; +import lombok.Data; + +import java.math.BigDecimal; +import java.util.List; + +/** + * elasticSearch搜索所需要的参数 + */ +@Data +public class ElasticSearchVo { + private BigDecimal startMoney; + private BigDecimal endMoney; +} diff --git a/bwie-elastic/src/main/resources/bootstrap.yml b/bwie-elastic/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..f933ebe --- /dev/null +++ b/bwie-elastic/src/main/resources/bootstrap.yml @@ -0,0 +1,41 @@ +# Tomcat +server: + port: 9008 +# Spring +spring: + elasticsearch: + + rest: + + uris: http://124.223.156.14:9200 + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-elastic + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.223.156.14:8848 + config: + # 配置中心地址 + server-addr: 124.223.156.14:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} +es: + hostname: 124.223.156.14 + port: 9200 + scheme: http + + + diff --git a/bwie-gateway/pom.xml b/bwie-gateway/pom.xml new file mode 100644 index 0000000..4234cf9 --- /dev/null +++ b/bwie-gateway/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + + + bwie-gateway + + + 17 + 17 + UTF-8 + + + + + + com.bwie + bwie-common + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + com.alibaba.cloud + spring-cloud-alibaba-sentinel-gateway + + + + com.alibaba.csp + sentinel-spring-cloud-gateway-adapter + + + + diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/GateWaySpringBootApplication.java b/bwie-gateway/src/main/java/com/bwie/gateway/GateWaySpringBootApplication.java new file mode 100644 index 0000000..70765f7 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/GateWaySpringBootApplication.java @@ -0,0 +1,12 @@ +package com.bwie.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) +public class GateWaySpringBootApplication { + public static void main(String[] args) { + SpringApplication.run(GateWaySpringBootApplication.class); + } +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java b/bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java new file mode 100644 index 0000000..a6daed3 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/config/GatewaySentinelConfig.java @@ -0,0 +1,71 @@ +package com.bwie.gateway.config; + +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; +import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.web.reactive.result.view.ViewResolver; + +import javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @deprecation: 网关限流控件 + * @author DongZl + */ +@Configuration +public class GatewaySentinelConfig { + /** + * 查看解析器 + */ + private final List viewResolvers; + /** + * 服务器编解码器配置 + */ + private final ServerCodecConfigurer serverCodecConfigurer; + public GatewaySentinelConfig(ObjectProvider> viewResolversProvider, + ServerCodecConfigurer serverCodecConfigurer) { + this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); + this.serverCodecConfigurer = serverCodecConfigurer; + } + /** + * Sentinel 网关块异常处理程序 + * @return + */ + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { + // 给 Spring Cloud Gateway 注册块异常处理程序。 + return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); + } + + /** + * 初始化网关配置 + */ + @PostConstruct + public void doInit() { + initGatewayRules(); + } + /** + * 配置限流规则 + */ + private void initGatewayRules() { + Set rules = new HashSet<>(); + rules.add(new GatewayFlowRule("cloud-user") + // 限流阈值 + .setCount(1) + // 统计时间窗口,单位是秒,默认是 1 秒 + .setIntervalSec(5) + ); + //添加到限流规则当中 + GatewayRuleManager.loadRules(rules); + } +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java b/bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java new file mode 100644 index 0000000..5705d6f --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/config/IgnoreWhiteConfig.java @@ -0,0 +1,32 @@ +package com.bwie.gateway.config; + +import com.alibaba.fastjson.JSONObject; +import lombok.Data; +import lombok.extern.log4j.Log4j2; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * @description: 放行白名单配置 + * @author DongZl + */ +@Configuration +@RefreshScope +@ConfigurationProperties(prefix = "ignore") +@Data +@Log4j2 +public class IgnoreWhiteConfig { + /** + * 放行白名单配置,网关不校验此处的白名单 + */ + private List whites = new ArrayList<>(); + + public void setWhites(List whites) { + log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites)); + this.whites = whites; + } +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/filters/LoginFilter.java b/bwie-gateway/src/main/java/com/bwie/gateway/filters/LoginFilter.java new file mode 100644 index 0000000..c7154b8 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/filters/LoginFilter.java @@ -0,0 +1,81 @@ +package com.bwie.gateway.filters; + +import com.bwie.constant.JwtConstants; +import com.bwie.constant.TokenConstants; +import com.bwie.gateway.config.IgnoreWhiteConfig; +import com.bwie.gateway.utils.GatewayUtils; +import com.bwie.utils.JwtUtils; +import com.bwie.utils.StringUtils; +import io.jsonwebtoken.Claims; +import lombok.extern.log4j.Log4j2; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +@Component +@Log4j2 +public class LoginFilter implements GlobalFilter, Ordered { + private final IgnoreWhiteConfig ignoreWhiteConfig; + private final StringRedisTemplate stringRedisTemplate; + + + public LoginFilter(IgnoreWhiteConfig ignoreWhiteConfig, StringRedisTemplate stringRedisTemplate) { + this.ignoreWhiteConfig = ignoreWhiteConfig; + this.stringRedisTemplate = stringRedisTemplate; + } + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + //请求头作用域 + ServerHttpRequest request = exchange.getRequest(); + //获取请求头 + HttpHeaders headers = request.getHeaders(); + //获取请求方式 + HttpMethod method = request.getMethod(); + //header操作对象 + ServerHttpRequest.Builder mutate = request.mutate(); + String path = request.getURI().getPath(); + log.info("请求日志: usl: [{}], 请求方式: [{}] ",path,method); + //后台放行方法白名单 + /** + * 查看所请求路径是否是在nacos放行名单里的 + */ + if(StringUtils.matches(path,ignoreWhiteConfig.getWhites())){ + return chain.filter(exchange); + } + String token = headers.getFirst(TokenConstants.TOKEN); + if(StringUtils.isEmpty(token)){ + return GatewayUtils.errorResponse(exchange,"令牌不能为空"); + } + Claims claims = JwtUtils.parseToken(token); + if(claims == null){ + return GatewayUtils.errorResponse(exchange,"令牌已经过期或解析不正确"); + } + String userKey = JwtUtils.getUserKey(claims); + Boolean idLogin = stringRedisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey); + if(!idLogin){ + return GatewayUtils.errorResponse(exchange,"登录状态过期"); + } + String userId = JwtUtils.getUserId(claims); + String userName = JwtUtils.getUserName(claims); + //设置用户信息到请求 + GatewayUtils.addHeader(mutate,JwtConstants.USER_KEY,userKey); + GatewayUtils.addHeader(mutate,JwtConstants.DETAILS_USERNAME,userName); + GatewayUtils.addHeader(mutate,JwtConstants.DETAILS_USER_ID,userId); + //内部请求来源参数清楚 + GatewayUtils.removeHeader(mutate,TokenConstants.TOKEN); + return chain.filter(exchange.mutate().request(mutate.build()).build()); + } + + @Override + public int getOrder() { + return 0; + } +} diff --git a/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java b/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java new file mode 100644 index 0000000..fe69e08 --- /dev/null +++ b/bwie-gateway/src/main/java/com/bwie/gateway/utils/GatewayUtils.java @@ -0,0 +1,98 @@ +package com.bwie.gateway.utils; + +import com.alibaba.fastjson.JSONObject; +import com.bwie.result.Result; +import com.bwie.utils.StringUtils; +import lombok.extern.log4j.Log4j2; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * @author DongZl + * @description: 网关处理工具类 + */ +@Log4j2 +public class GatewayUtils { + /** + * 添加请求头参数 + * @param mutate 修改对象 + * @param key 键 + * @param value 值 + */ + public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) { + if (StringUtils.isEmpty(key)){ + log.warn("添加请求头参数键不可以为空"); + return; + } + if (value == null) { + log.warn("添加请求头参数:[{}]值为空",key); + return; + } + String valueStr = value.toString(); + mutate.header(key, valueStr); + log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value); + } + + /** + * 删除请求头参数 + * @param mutate 修改对象 + * @param key 键 + */ + public static void removeHeader(ServerHttpRequest.Builder mutate, String key) { + if (StringUtils.isEmpty(key)){ + log.warn("删除请求头参数键不可以为空"); + return; + } + mutate.headers(httpHeaders -> httpHeaders.remove(key)).build(); + log.info("删除请求头参数 - 键:[{}]",key); + } + + /** + * 错误结果响应 + * @param exchange 响应上下文 + * @param msg 响应消息 + * @return + */ + public static Mono errorResponse(ServerWebExchange exchange, String msg, HttpStatus httpStatus) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(httpStatus); + //设置HTTP响应头文本格式 + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json"); + //定义响应内容 + Result result = Result.error(msg); + String resultJson = JSONObject.toJSONString(result); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson); + DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes()); + //进行响应 + return response.writeWith(Mono.just(dataBuffer)); + } + + /** + * 错误结果响应 + * @param exchange 响应上下文 + * @param msg 响应消息 + * @return + */ + public static Mono errorResponse(ServerWebExchange exchange, String msg) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(HttpStatus.OK); + //设置HTTP响应头文本格式 + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json"); + //定义响应内容 + Result result = Result.error(msg); + String resultJson = JSONObject.toJSONString(result); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson); + DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes()); + //进行响应 + return response.writeWith(Mono.just(dataBuffer)); + } + + +} diff --git a/bwie-gateway/src/main/resources/bootstrap.yml b/bwie-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..a74c69e --- /dev/null +++ b/bwie-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# Tomcat +server: + port: 18080 +# Spring +spring: + application: + # 应用名称 + name: bwie-gateway + profiles: + # 环境配置 + active: dev + main: + # 允许使用循环引用 + allow-circular-references: true + # 允许定义相同的bean对象 去覆盖原有的 + allow-bean-definition-overriding: true + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.223.156.14:8848 + config: + # 配置中心地址 + server-addr: 124.223.156.14:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-module/bwie-expressage/pom.xml b/bwie-module/bwie-expressage/pom.xml new file mode 100644 index 0000000..fb1d900 --- /dev/null +++ b/bwie-module/bwie-expressage/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.bwie + bwie-module + 1.0-SNAPSHOT + + + bwie-expressage + + + 17 + 17 + UTF-8 + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/SystemSpringBootApplication.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/SystemSpringBootApplication.java new file mode 100644 index 0000000..aaaa885 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/SystemSpringBootApplication.java @@ -0,0 +1,20 @@ +package com.bwie.sysytem; + + +import com.bwie.config.MybitsPlusConfig; +import com.bwie.handler.GlobalExceptionHandler; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Import; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableFeignClients(basePackages = "com.bwie") +@EnableScheduling +@Import(value = {MybitsPlusConfig.class, GlobalExceptionHandler.class}) +public class SystemSpringBootApplication { + public static void main(String[] args) { + SpringApplication.run(SystemSpringBootApplication.class); + } +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/controller/BorrowController.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/controller/BorrowController.java new file mode 100644 index 0000000..0e64765 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/controller/BorrowController.java @@ -0,0 +1,77 @@ +package com.bwie.sysytem.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.bwie.handler.BizExceptionHandler; +import com.bwie.result.Result; +import com.bwie.domain.Borrow; +import com.bwie.sysytem.feign.ElasticFeign; +import com.bwie.sysytem.service.BorrowService; +import com.bwie.sysytem.vo.BorrowPrice; +import com.bwie.sysytem.vo.BorrowVo; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/list") +public class BorrowController { + private final BorrowService borrowService; + private final ElasticFeign elasticFeign; + + public BorrowController(BorrowService borrowService, ElasticFeign elasticFeign) { + this.borrowService = borrowService; + this.elasticFeign = elasticFeign; + } + + /** + * 展示借款记录 + * @return + */ + @PostMapping("/showList") + public Result> showList(){ + List borrowList = borrowService.list(); + return Result.success(borrowList); + } + + + /** + * 添加借款记录 + * @param borrowVo + * @return + */ + @PostMapping("/addBorrow") + public Result addBorrow(@RequestBody BorrowVo borrowVo){ + borrowService.addBorrow(borrowVo); + return Result.success(); + } + + /** + * 计算利率等结果 + * @param money + * @return + */ + @PostMapping("/index/{money}") + public Result sumIndex(@PathVariable Long money){ + if(money<=100){ + throw new BizExceptionHandler(408,"该金额尚不支持借款"); + } + BorrowPrice borrowPrice = borrowService.showPrice(money); + return Result.success(borrowPrice); + } + + + /** + * 增量同步到es + * @return + */ + @PostMapping("/getFlag") + @Scheduled(cron = "0 0/2 * * * ?") + public Result addElastic(){ + LambdaQueryWrapper borrowLambdaQueryWrapper = new LambdaQueryWrapper<>(); + LambdaQueryWrapper eq = borrowLambdaQueryWrapper.eq(Borrow::getBorrowFlag, 2L); + List list = borrowService.list(eq); + elasticFeign.addElasticRequest(list); + return Result.success(); + } +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/feign/ElasticFeign.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/feign/ElasticFeign.java new file mode 100644 index 0000000..0f445df --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/feign/ElasticFeign.java @@ -0,0 +1,14 @@ +package com.bwie.sysytem.feign; + +import com.bwie.domain.Borrow; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; + +@FeignClient(name = "bwie-elastic",path = "/es") +public interface ElasticFeign { + @PostMapping("/addElastic") + public void addElasticRequest(@RequestBody List borrowList); +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/mapper/BorrowMapper.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/mapper/BorrowMapper.java new file mode 100644 index 0000000..ba08bdf --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/mapper/BorrowMapper.java @@ -0,0 +1,9 @@ +package com.bwie.sysytem.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.bwie.domain.Borrow; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface BorrowMapper extends BaseMapper { +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/BorrowService.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/BorrowService.java new file mode 100644 index 0000000..9b57957 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/BorrowService.java @@ -0,0 +1,12 @@ +package com.bwie.sysytem.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bwie.domain.Borrow; +import com.bwie.sysytem.vo.BorrowPrice; +import com.bwie.sysytem.vo.BorrowVo; + +public interface BorrowService extends IService { + void addBorrow(BorrowVo borrowVo); + + BorrowPrice showPrice(Long money); +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/impl/BorrowServiceImpl.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/impl/BorrowServiceImpl.java new file mode 100644 index 0000000..2c05277 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/service/impl/BorrowServiceImpl.java @@ -0,0 +1,117 @@ +package com.bwie.sysytem.service.impl; + +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bwie.domain.Borrow; +import com.bwie.sysytem.mapper.BorrowMapper; +import com.bwie.sysytem.service.BorrowService; +import com.bwie.sysytem.vo.BorrowPrice; +import com.bwie.sysytem.vo.BorrowVo; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.Date; + +@Service +public class BorrowServiceImpl extends ServiceImpl implements BorrowService { + + private final RedisTemplate redisTemplate; + + public BorrowServiceImpl(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + @Override + public void addBorrow(BorrowVo borrowVo) { + Borrow borrow = new Borrow(); + borrow.setBorrowDate(new Date()); + borrow.setBorrowPrice(borrowVo.getPrice()); + borrow.setBorrowNum(borrowVo.getPlane()); + borrow.setBorrowTime(new Date()); + borrow.setBorrowBackPrice(BigDecimal.valueOf(0)); + borrow.setBorrowAuthPrice(borrowVo.getPrice()); + borrow.setBorrowInsert("0.005"); + borrow.setBorrowMoney(borrowVo.getSumMoney()); + borrow.setBorrowPlace("申请中"); + borrow.setBorrowFlag(1L); + this.save(borrow); + } + + @Override + public BorrowPrice showPrice(Long money) { + BorrowPrice borrowPrice = new BorrowPrice(); + if(money<5000){ + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.003)); + int date = new Date().getDate(); + Integer day=0; + if(date<15){ + day=15-date; + } + if(date>15){ + day=date-15; + } + borrowPrice.setSumPrice(BigDecimal.valueOf(day*(money*0.003)).add(BigDecimal.valueOf(money))); + borrowPrice.setSumMoney(BigDecimal.valueOf(day*(money*0.003))); + borrowPrice.setSumIndex(BigDecimal.valueOf(money*0.003)); + } + if(money>=5000 && money<20000){ + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.005)); + int date = new Date().getDate(); + Integer day=0; + if(date<15){ + day=15-date; + } + if(date>15){ + day=date-15; + } + borrowPrice.setSumPrice(BigDecimal.valueOf(day*(money*0.005)).add(BigDecimal.valueOf(money))); + borrowPrice.setSumMoney(BigDecimal.valueOf(day*(money*0.005))); + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.005)); + } + if(money>=20000 && money<30000){ + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.007)); + int date = new Date().getDate(); + Integer day=0; + if(date<15){ + day=15-date; + } + if(date>15){ + day=date-15; + } + borrowPrice.setSumPrice(BigDecimal.valueOf(day*(money*0.007)).add(BigDecimal.valueOf(money))); + borrowPrice.setSumMoney(BigDecimal.valueOf(day*(money*0.007))); + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.007)); + } + if(money>=30000 && money<50000){ + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.01)); + int date = new Date().getDate(); + Integer day=0; + if(date<15){ + day=15-date; + } + if(date>15){ + day=date-15; + } + borrowPrice.setSumPrice(BigDecimal.valueOf(day*(money*0.01)).add(BigDecimal.valueOf(money))); + borrowPrice.setSumMoney(BigDecimal.valueOf(day*(money*0.01))); + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.01)); + } + if(money>=50000){ + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.05)); + int date = new Date().getDate(); + Integer day=0; + if(date<15){ + day=15-date; + } + if(date>15){ + day=date-15; + } + borrowPrice.setSumPrice(BigDecimal.valueOf(day*(money*0.05)).add(BigDecimal.valueOf(money))); + borrowPrice.setSumMoney(BigDecimal.valueOf(day*(money*0.05))); + borrowPrice.setNumPrice(BigDecimal.valueOf(money*0.05)); + } + redisTemplate.opsForValue().set("borrow", JSON.toJSONString(borrowPrice)); + return borrowPrice; + } +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowPrice.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowPrice.java new file mode 100644 index 0000000..4f41c97 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowPrice.java @@ -0,0 +1,28 @@ +package com.bwie.sysytem.vo; + +import lombok.Data; + +import java.math.BigDecimal; + +/** + * 还款利率 + */ +@Data +public class BorrowPrice { + /** + * 每期还款金额 + */ + private BigDecimal numPrice; + /** + * 综合利率 + */ + private BigDecimal sumIndex; + /** + * 综合手续费 + */ + private BigDecimal sumMoney; + /** + * 总共还款 + */ + private BigDecimal sumPrice; +} diff --git a/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowVo.java b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowVo.java new file mode 100644 index 0000000..6375222 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/java/com/bwie/sysytem/vo/BorrowVo.java @@ -0,0 +1,47 @@ +package com.bwie.sysytem.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * 添加借款 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class BorrowVo { + /** + * 借款日期 + */ + private Date time; + /** + * 借款金额 + */ + private BigDecimal price; + /** + * 还款计划 + */ + private Long plane; + /** + * 每期还款金额 + */ + private BigDecimal numPrice; + /** + * 综合利率 + */ + private BigDecimal sumIndex; + /** + * 综合手续费 + */ + private BigDecimal sumMoney; + /** + * 总共还款 + */ + private BigDecimal sumPrice; +} diff --git a/bwie-module/bwie-expressage/src/main/resources/bootstrap.yml b/bwie-module/bwie-expressage/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..5042b64 --- /dev/null +++ b/bwie-module/bwie-expressage/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# Tomcat +server: + port: 9002 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-expressage + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.223.156.14:8848 + config: + # 配置中心地址 + server-addr: 124.223.156.14:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-module/bwie-user/pom.xml b/bwie-module/bwie-user/pom.xml new file mode 100644 index 0000000..b129f4e --- /dev/null +++ b/bwie-module/bwie-user/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.bwie + bwie-module + 1.0-SNAPSHOT + + + bwie-user + + + 17 + 17 + UTF-8 + + + + + com.bwie + bwie-common + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/bwie-module/bwie-user/src/main/java/com/bwie/user/UserSpringBootApplication.java b/bwie-module/bwie-user/src/main/java/com/bwie/user/UserSpringBootApplication.java new file mode 100644 index 0000000..2a696ed --- /dev/null +++ b/bwie-module/bwie-user/src/main/java/com/bwie/user/UserSpringBootApplication.java @@ -0,0 +1,15 @@ +package com.bwie.user; + +import com.bwie.config.MybitsPlusConfig; +import com.bwie.handler.GlobalExceptionHandler; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; + +@SpringBootApplication +@Import(value = {MybitsPlusConfig.class, GlobalExceptionHandler.class}) +public class UserSpringBootApplication { + public static void main(String[] args) { + SpringApplication.run(UserSpringBootApplication.class); + } +} diff --git a/bwie-module/bwie-user/src/main/java/com/bwie/user/controller/UserController.java b/bwie-module/bwie-user/src/main/java/com/bwie/user/controller/UserController.java new file mode 100644 index 0000000..f87d8f9 --- /dev/null +++ b/bwie-module/bwie-user/src/main/java/com/bwie/user/controller/UserController.java @@ -0,0 +1,30 @@ +package com.bwie.user.controller; + +import com.bwie.domain.User; +import com.bwie.result.Result; +import com.bwie.user.service.UserService; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/user") +public class UserController { + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } + + /** + * 登录查询用户名是否存在 + * @param userName + * @return + */ + @PostMapping("/getByName/{userName}") + public Result getByName(@PathVariable String userName){ + User user = userService.loginName(userName); + return Result.success(user); + } +} diff --git a/bwie-module/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java b/bwie-module/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java new file mode 100644 index 0000000..eb60586 --- /dev/null +++ b/bwie-module/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java @@ -0,0 +1,10 @@ +package com.bwie.user.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.bwie.domain.User; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface UserMapper extends BaseMapper { + +} diff --git a/bwie-module/bwie-user/src/main/java/com/bwie/user/service/UserService.java b/bwie-module/bwie-user/src/main/java/com/bwie/user/service/UserService.java new file mode 100644 index 0000000..21176e3 --- /dev/null +++ b/bwie-module/bwie-user/src/main/java/com/bwie/user/service/UserService.java @@ -0,0 +1,8 @@ +package com.bwie.user.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bwie.domain.User; + +public interface UserService extends IService { + User loginName(String userName); +} diff --git a/bwie-module/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java b/bwie-module/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..3f2d6ac --- /dev/null +++ b/bwie-module/bwie-user/src/main/java/com/bwie/user/service/impl/UserServiceImpl.java @@ -0,0 +1,18 @@ +package com.bwie.user.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bwie.domain.User; +import com.bwie.user.mapper.UserMapper; +import com.bwie.user.service.UserService; +import org.springframework.stereotype.Service; + +@Service +public class UserServiceImpl extends ServiceImpl implements UserService { + @Override + public User loginName(String userName) { + LambdaQueryWrapper eq = new LambdaQueryWrapper().eq(User::getUserName, userName); + User user = getOne(eq); + return user; + } +} diff --git a/bwie-module/bwie-user/src/main/resources/bootstrap.yml b/bwie-module/bwie-user/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..a00be17 --- /dev/null +++ b/bwie-module/bwie-user/src/main/resources/bootstrap.yml @@ -0,0 +1,29 @@ +# Tomcat +server: + port: 9001 +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: bwie-user + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 124.223.156.14:8848 + config: + # 配置中心地址 + server-addr: 124.223.156.14:8848 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/bwie-module/pom.xml b/bwie-module/pom.xml new file mode 100644 index 0000000..7061e40 --- /dev/null +++ b/bwie-module/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + + + bwie-module + pom + + bwie-user + bwie-expressage + + + + 17 + 17 + UTF-8 + + + diff --git a/bwie-rabbit/pom.xml b/bwie-rabbit/pom.xml new file mode 100644 index 0000000..04a5471 --- /dev/null +++ b/bwie-rabbit/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + + + bwie-rabbit + + + 17 + 17 + UTF-8 + + + + com.bwie + bwie-common + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-amqp + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.9.10 + + + + org.springframework.boot + spring-boot-starter-web + + + + + diff --git a/bwie-rabbit/src/main/java/com/bwie/rabbitmq/RabbitMqSpringBootApplication.java b/bwie-rabbit/src/main/java/com/bwie/rabbitmq/RabbitMqSpringBootApplication.java new file mode 100644 index 0000000..053d2db --- /dev/null +++ b/bwie-rabbit/src/main/java/com/bwie/rabbitmq/RabbitMqSpringBootApplication.java @@ -0,0 +1,17 @@ +package com.bwie.rabbitmq; + +import com.bwie.config.MybitsPlusConfig; +import com.bwie.handler.GlobalExceptionHandler; +import com.bwie.rabbitmq.config.RabbitMQConfig; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.context.annotation.Import; + +@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) +@Import(value = {MybitsPlusConfig.class, GlobalExceptionHandler.class}) +public class RabbitMqSpringBootApplication { + public static void main(String[] args) { + SpringApplication.run(RabbitMqSpringBootApplication.class); + } +} diff --git a/bwie-rabbit/src/main/java/com/bwie/rabbitmq/config/RabbitMQConfig.java b/bwie-rabbit/src/main/java/com/bwie/rabbitmq/config/RabbitMQConfig.java new file mode 100644 index 0000000..ddbd4ac --- /dev/null +++ b/bwie-rabbit/src/main/java/com/bwie/rabbitmq/config/RabbitMQConfig.java @@ -0,0 +1,134 @@ +package com.bwie.rabbitmq.config; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.amqp.core.*; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import javax.annotation.PostConstruct; + +@Configuration +public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback{ + + public static final String QUEUE = "msgQueue"; + public static final String EXCHANGE = "msgExchange"; + public static final String ROUTINGKEY = "msgKey"; + + public static final String QUEUEs = "ays"; + public static final String EXCHANGEs = "aysc"; + public static final String ROUTINGKEYs = "aysk"; + + + + public static final Logger logger = LoggerFactory.getLogger(RabbitMQConfig.class); + + + @Autowired + private RabbitTemplate rabbitTemplate; + + + //创建消息转换器 + @Bean + public MessageConverter messageConverter(){ + return new Jackson2JsonMessageConverter(); + } + + + @Bean + public Queue queues(){ + return new Queue(QUEUEs,true); + } + + @Bean + public DirectExchange directExchanges(){ + + return new DirectExchange(EXCHANGEs); + + } + + @Bean + public Binding bindings(){ + return BindingBuilder.bind(queues()).to(directExchanges()).with(ROUTINGKEYs); + } + + +// + @Bean + public Queue queue(){ + return new Queue(QUEUE,true); + } + + @Bean + public DirectExchange directExchange(){ + + return new DirectExchange(EXCHANGE); + + } + + @Bean + public Binding binding(){ + return BindingBuilder.bind(queue()).to(directExchange()).with(ROUTINGKEY); + } + + + +// @Primary +// @Bean +// public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){ +// RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); +// rabbitTemplate.setMessageConverter(messageConverter()); +// +// this.rabbitTemplate = rabbitTemplate; +// rabbitTemplate(); +// return rabbitTemplate; +// } + + /** + * 开启事务机制 针对生产者投递消息到交换机或队列 注意:事务机制和消息确认机制不能同时使用 + * @param connectionFactory + * @return + */ +// @Bean +// public RabbitTransactionManager transactionManager(ConnectionFactory connectionFactory){ +// return new RabbitTransactionManager(connectionFactory); +// } + + + @PostConstruct + public void rabbitTemplate(){ + + rabbitTemplate.setConfirmCallback(this); + rabbitTemplate.setReturnsCallback(this); + } + + + + + @Override + public void confirm(CorrelationData correlationData, boolean ack, String s) { + + if (ack){ + logger.info("{}消息到达交换机",correlationData.getId()); + }else { + logger.info("{}消息丢失", correlationData.getId()); + } + + } + + @Override + public void returnedMessage(ReturnedMessage returnedMessage) { + + logger.error("{}消息未到达队列",returnedMessage.getMessage().getMessageProperties().getMessageId()); + + } +} diff --git a/bwie-rabbit/src/main/resources/bootstrap.yml b/bwie-rabbit/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..1a71925 --- /dev/null +++ b/bwie-rabbit/src/main/resources/bootstrap.yml @@ -0,0 +1,33 @@ +server: + port: 9003 +spring: + main: + allow-circular-references: true + application: + name: bwie-rabbitmq + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + serverAddr: 124.223.156.14:8848 + config: + serverAddr: 124.223.156.14:8848 + fileExtension: yml + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + + rabbitmq: + username: guest + password: guest + virtualHost: / + port: 5672 + host: 124.223.156.14 + listener: + simple: + prefetch: 1 # 每次只能获取一条,处理完成才能获取下一条 + publisher-confirm-type: correlated #确认消息已发送到交换机(Exchange) + publisher-returns: true #确认消息已发送到队列(Queue) + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9779806 --- /dev/null +++ b/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.bwie + five-mouth + 1.0-SNAPSHOT + pom + + bwie-common + bwie-module + bwie-auth + bwie-gateway + bwie-elastic + bwie-rabbit + + + + 17 + 17 + UTF-8 + + + + + spring-boot-starter-parent + org.springframework.boot + 2.7.15 + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2021.0.8 + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2021.0.5.0 + pom + import + + + + com.alibaba.nacos + nacos-client + 2.0.4 + + + + com.bwie + bwie-common + 1.0-SNAPSHOT + + + + + +