专高六月考
commit
769dac6930
|
@ -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
|
|
@ -0,0 +1,8 @@
|
|||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
|
||||
<file url="file://$PROJECT_DIR$/bwie-auth/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-common/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-elastic/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-gateway/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-module/bwie-auth/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-module/bwie-expressage/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-module/bwie-user/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-module/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-module/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/bwie-rabbit/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="PROJECT" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,124 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-auth</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 项目公共 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringBoot Web-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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<User> 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<String, Object> 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.bwie.auth.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 登录信息
|
||||
*/
|
||||
@Data
|
||||
public class AuthRequestVo {
|
||||
/**
|
||||
* 登录名
|
||||
*/
|
||||
private String userName;
|
||||
/**
|
||||
* 登录密码
|
||||
*/
|
||||
private String userPwd;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
|
@ -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}
|
|
@ -0,0 +1,142 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-common</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- bootstrap 启动器 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
<!-- 负载均衡-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Openfeign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- JWT -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.9.1</version>
|
||||
</dependency>
|
||||
<!-- Alibaba Fastjson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.80</version>
|
||||
</dependency>
|
||||
<!-- SpringBoot Boot Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<!-- Druid -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.2.8</version>
|
||||
</dependency>
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
<!-- Mybatis-plus 配置 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.4.1</version>
|
||||
</dependency>
|
||||
<!-- Mybatis 依赖配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<!-- Hibernate Validator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<!-- Apache Lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<!-- lombok依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<!-- hutool -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- oss 图片上传 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- <!–mq 依赖–>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>-->
|
||||
<!--fastDfs文件上传-->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringBoot 测试框架 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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 = "操作异常";
|
||||
}
|
|
@ -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";
|
||||
}
|
|
@ -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";
|
||||
|
||||
}
|
|
@ -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";
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<String> runtimeException(MethodArgumentNotValidException exception){
|
||||
return Result.error(
|
||||
JSONObject.toJSONString(
|
||||
exception.getBindingResult().getAllErrors()
|
||||
.stream()
|
||||
.map(ObjectError::getDefaultMessage)
|
||||
.toList()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = IllegalArgumentException.class)
|
||||
public Result<String> illegalArgumentExceptionHandler(IllegalArgumentException exception){
|
||||
log.error("请求异常:[{}]", exception.getMessage(), exception);
|
||||
return Result.error(exception.getMessage());
|
||||
}
|
||||
|
||||
}
|
|
@ -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<User> getByName(@PathVariable String userName);
|
||||
}
|
|
@ -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<UserFeign> {
|
||||
|
||||
@Override
|
||||
public UserFeign create(Throwable cause) {
|
||||
return new UserFeign() {
|
||||
@Override
|
||||
public Result<User> getByName(String userName) {
|
||||
log.error("登录查询用户名feign调用失败");
|
||||
return Result.error();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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<T> implements Serializable {
|
||||
/**
|
||||
* 总条数
|
||||
*/
|
||||
private long total;
|
||||
/**
|
||||
* 结果集合
|
||||
*/
|
||||
private List<T> list;
|
||||
public PageResult() {
|
||||
}
|
||||
public PageResult(long total, List<T> list) {
|
||||
this.total = total;
|
||||
this.list = list;
|
||||
}
|
||||
public static <T> PageResult<T> toPageResult(long total, List<T> list){
|
||||
return new PageResult(total , list);
|
||||
}
|
||||
public static <T> Result<PageResult<T>> toResult(long total, List<T> list){
|
||||
return Result.success(PageResult.toPageResult(total,list));
|
||||
}
|
||||
}
|
|
@ -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<T> 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 <T> Result<T> success() {
|
||||
return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
|
||||
}
|
||||
public static <T> Result<T> success(T data) {
|
||||
return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
|
||||
}
|
||||
public static <T> Result<T> success(T data, String msg) {
|
||||
return restResult(data, SUCCESS, msg);
|
||||
}
|
||||
public static <T> Result<T> error() {
|
||||
return restResult(null, FAIL, Constants.ERROR_MSG);
|
||||
}
|
||||
public static <T> Result<T> error(String msg) {
|
||||
return restResult(null, FAIL, msg);
|
||||
}
|
||||
public static <T> Result<T> error(T data) {
|
||||
return restResult(data, FAIL, Constants.ERROR_MSG);
|
||||
}
|
||||
public static <T> Result<T> error(T data, String msg) {
|
||||
return restResult(data, FAIL, msg);
|
||||
}
|
||||
public static <T> Result<T> error(int code, String msg) {
|
||||
return restResult(null, code, msg);
|
||||
}
|
||||
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||
Result<T> 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();
|
||||
}
|
||||
}
|
|
@ -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("-", "");
|
||||
}
|
||||
}
|
|
@ -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<String, Object> 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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-elastic</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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<List<Borrow>> listResult = systemFeign.showList();
|
||||
if(listResult.isSuccess()!=true){
|
||||
throw new BizExceptionHandler(408,"网络不好,请稍后再试");
|
||||
}
|
||||
List<Borrow> 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);
|
||||
}
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Borrow> borrowList){
|
||||
elasticSearchService.addElasticSearch(borrowList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 展示elastic列表
|
||||
* @param elasticSearchVo
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/showElasticList")
|
||||
public Result showElasticList(@RequestBody ElasticSearchVo elasticSearchVo){
|
||||
List<Borrow> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<List<Borrow>> showList();
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Borrow> borrowList);
|
||||
|
||||
List<Borrow> borrowList(ElasticSearchVo elasticSearchVo);
|
||||
}
|
|
@ -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<Borrow> 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<Borrow> 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<Borrow> 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;
|
||||
}
|
||||
}
|
|
@ -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<List<Borrow>> listResult = systemFeign.showList();
|
||||
if(listResult.isSuccess()!=true){
|
||||
throw new BizExceptionHandler(408,"网络不好,请稍后再试");
|
||||
}
|
||||
List<Borrow> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-gateway</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 公共模块 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
</dependency>
|
||||
<!-- 网关依赖 -->
|
||||
<!-- SpringCloud Gateway -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Sentinel Gateway -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>
|
||||
<!-- 引入阿里巴巴sentinel限流 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<ViewResolver> viewResolvers;
|
||||
/**
|
||||
* 服务器编解码器配置
|
||||
*/
|
||||
private final ServerCodecConfigurer serverCodecConfigurer;
|
||||
public GatewaySentinelConfig(ObjectProvider<List<ViewResolver>> 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<GatewayFlowRule> rules = new HashSet<>();
|
||||
rules.add(new GatewayFlowRule("cloud-user")
|
||||
// 限流阈值
|
||||
.setCount(1)
|
||||
// 统计时间窗口,单位是秒,默认是 1 秒
|
||||
.setIntervalSec(5)
|
||||
);
|
||||
//添加到限流规则当中
|
||||
GatewayRuleManager.loadRules(rules);
|
||||
}
|
||||
}
|
|
@ -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<String> whites = new ArrayList<>();
|
||||
|
||||
public void setWhites(List<String> whites) {
|
||||
log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
|
||||
this.whites = whites;
|
||||
}
|
||||
}
|
|
@ -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<Void> 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;
|
||||
}
|
||||
}
|
|
@ -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<Void> 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<Void> 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));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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}
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-module</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-expressage</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 公共模块 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<List<Borrow>> showList(){
|
||||
List<Borrow> 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<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
LambdaQueryWrapper<Borrow> eq = borrowLambdaQueryWrapper.eq(Borrow::getBorrowFlag, 2L);
|
||||
List<Borrow> list = borrowService.list(eq);
|
||||
elasticFeign.addElasticRequest(list);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -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<Borrow> borrowList);
|
||||
}
|
|
@ -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<Borrow> {
|
||||
}
|
|
@ -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<Borrow> {
|
||||
void addBorrow(BorrowVo borrowVo);
|
||||
|
||||
BorrowPrice showPrice(Long money);
|
||||
}
|
|
@ -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<BorrowMapper, Borrow> 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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}
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-module</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-user</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 公共模块 -->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<User> getByName(@PathVariable String userName){
|
||||
User user = userService.loginName(userName);
|
||||
return Result.success(user);
|
||||
}
|
||||
}
|
|
@ -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<User> {
|
||||
|
||||
}
|
|
@ -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> {
|
||||
User loginName(String userName);
|
||||
}
|
|
@ -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<UserMapper, User> implements UserService {
|
||||
@Override
|
||||
public User loginName(String userName) {
|
||||
LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getUserName, userName);
|
||||
User user = getOne(eq);
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -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}
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-module</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>bwie-user</module>
|
||||
<module>bwie-expressage</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,46 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bwie-rabbit</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- rabbitMQ -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>2.9.10</version>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>five-mouth</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>bwie-common</module>
|
||||
<module>bwie-module</module>
|
||||
<module>bwie-auth</module>
|
||||
<module>bwie-gateway</module>
|
||||
<module>bwie-elastic</module>
|
||||
<module>bwie-rabbit</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<!-- 规定SpringBoot版本 -->
|
||||
<!-- 父级pom文件 主要用于规定项目依赖的各个版本,用于进行项目版本约束 -->
|
||||
<parent>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<version>2.7.15</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<!-- 依赖声明 -->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- SpringCloud 微服务 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>2021.0.8</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba 微服务 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>2021.0.5.0</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<!-- Alibaba Nacos 配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-client</artifactId>
|
||||
<version>2.0.4</version>
|
||||
</dependency>
|
||||
<!-- 系统公共 依赖 版本号定义-->
|
||||
<dependency>
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>bwie-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue