feat(mall): 代码优化
parent
8dba83e2de
commit
5cfe58d100
@ -1,82 +0,0 @@
|
||||
package com.wayn.admin.framework.config;
|
||||
|
||||
import com.wayn.common.constant.Constants;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.schmizz.sshj.SSHClient;
|
||||
import net.schmizz.sshj.connection.channel.direct.Parameters;
|
||||
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
|
||||
/**
|
||||
* ssh使用端口转发
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SSHProxyConfig {
|
||||
|
||||
public static final String localHost = "localhost";
|
||||
private SSHClient client;
|
||||
private final SSHProxyProperties sshProxyProperties;
|
||||
|
||||
public SSHProxyConfig(SSHProxyProperties sshProxyProperties) {
|
||||
this.sshProxyProperties = sshProxyProperties;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void connect() {
|
||||
|
||||
if (!sshProxyProperties.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
new Thread(() -> {
|
||||
try {
|
||||
log.info(Constants.LOG_PREFIX + "ssh connect");
|
||||
client = new SSHClient();
|
||||
client.addHostKeyVerifier(new PromiscuousVerifier());
|
||||
client.connect(sshProxyProperties.getHost(), sshProxyProperties.getPost());
|
||||
client.loadKnownHosts();
|
||||
|
||||
client.authPassword(sshProxyProperties.getUserName(), sshProxyProperties.getPassword());
|
||||
|
||||
final Parameters params
|
||||
= new Parameters(localHost, sshProxyProperties.getLocalPort(),
|
||||
sshProxyProperties.getRemoteHost(),
|
||||
sshProxyProperties.getRemotePort());
|
||||
final ServerSocket ss = new ServerSocket();
|
||||
try (ss) {
|
||||
ss.setReuseAddress(true);
|
||||
ss.bind(new InetSocketAddress(params.getLocalHost(), params.getLocalPort()));
|
||||
client.newLocalPortForwarder(params, ss).listen();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
client.disconnect();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}, "ssh-connect").start();
|
||||
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void close() {
|
||||
try {
|
||||
if (client != null) {
|
||||
log.info(Constants.LOG_PREFIX + "ssh disconnect");
|
||||
client.disconnect();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.wayn.admin.framework.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "wayn.ssh-proxy")
|
||||
public class SSHProxyProperties {
|
||||
|
||||
private boolean enabled;
|
||||
|
||||
private String host;
|
||||
private Integer post;
|
||||
|
||||
private String userName;
|
||||
private String password;
|
||||
|
||||
private Integer localPort;
|
||||
|
||||
private String remoteHost;
|
||||
private Integer remotePort;
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.wayn.admin.framework.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* websocket配置
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.wayn.common.annotation;
|
||||
|
||||
import com.wayn.common.enums.DataSourceEnum;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 数据源注解
|
||||
*/
|
||||
@Inherited
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataSource {
|
||||
|
||||
DataSourceEnum value() default DataSourceEnum.MASTER;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package com.wayn.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 自定义注解防止表单重复提交
|
||||
*/
|
||||
@Inherited
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface RepeatSubmit {
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package com.wayn.mobile.api.controller;
|
||||
|
||||
|
||||
import com.wayn.common.base.controller.BaseController;
|
||||
import com.wayn.common.util.R;
|
||||
import com.wayn.mobile.api.service.ISeckillService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 秒杀库存表 前端控制器
|
||||
*
|
||||
* @author wayn
|
||||
* @since 2020-08-04
|
||||
*/
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("seckill")
|
||||
public class SeckillController extends BaseController {
|
||||
|
||||
private ISeckillService iSeckillService;
|
||||
|
||||
@GetMapping("update")
|
||||
public R update(Long id) {
|
||||
return iSeckillService.updateSec(id);
|
||||
}
|
||||
|
||||
@GetMapping("update1")
|
||||
public R update1(Long id) {
|
||||
return iSeckillService.updateSec1(id);
|
||||
}
|
||||
|
||||
@GetMapping("update2")
|
||||
public R update2(Long id) {
|
||||
return iSeckillService.updateSec2(id);
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package com.wayn.mobile.api.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 秒杀库存表
|
||||
*
|
||||
* @author wayn
|
||||
* @since 2020-08-04
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class Seckill implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 商品库存id
|
||||
*/
|
||||
@TableId(value = "seckill_id", type = IdType.AUTO)
|
||||
private Long seckillId;
|
||||
|
||||
/**
|
||||
* 商品名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 库存数量
|
||||
*/
|
||||
private Integer number;
|
||||
|
||||
/**
|
||||
* 秒杀开始时间
|
||||
*/
|
||||
private LocalDateTime startTime;
|
||||
|
||||
/**
|
||||
* 秒杀结束时间
|
||||
*/
|
||||
private LocalDateTime endTime;
|
||||
|
||||
/**
|
||||
* 秒杀创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
private Integer version;
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package com.wayn.mobile.api.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.wayn.mobile.api.domain.Seckill;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 秒杀库存表 Mapper 接口
|
||||
*
|
||||
* @author wayn
|
||||
* @since 2020-08-04
|
||||
*/
|
||||
public interface SeckillMapper extends BaseMapper<Seckill> {
|
||||
|
||||
boolean updateSec(@Param("id") Long id, @Param("newVersion") Integer newVersion, @Param("oldVersion") Integer oldVersion, @Param("newNum") Integer newNum);
|
||||
|
||||
boolean updateSec1(@Param("id") Long id, @Param("newNum") Integer newNum, @Param("oldNum") Integer oldNum);
|
||||
|
||||
boolean updateSec2(@Param("id") Long id);
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package com.wayn.mobile.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.wayn.common.util.R;
|
||||
import com.wayn.mobile.api.domain.Seckill;
|
||||
import com.wayn.mobile.api.mapper.SeckillMapper;
|
||||
import com.wayn.mobile.api.service.ISeckillService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 秒杀库存表 服务实现类
|
||||
*
|
||||
* @author wayn
|
||||
* @since 2020-08-04
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class SeckillServiceImpl extends ServiceImpl<SeckillMapper, Seckill> implements ISeckillService {
|
||||
|
||||
private final AtomicInteger atomicInteger = new AtomicInteger(0);
|
||||
private SeckillMapper seckillMapper;
|
||||
|
||||
/**
|
||||
* 使用
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public R updateSec(Long id) {
|
||||
Seckill seckill = getById(id);
|
||||
if (seckill.getNumber() <= 0) {
|
||||
return R.error();
|
||||
}
|
||||
Integer newNum = seckill.getNumber() - 1;
|
||||
Integer oldVersion = seckill.getVersion();
|
||||
Integer newVersion = seckill.getVersion() + 1;
|
||||
System.out.println(Thread.currentThread().getName() + ":剩余库存:" + newNum);
|
||||
boolean b = seckillMapper.updateSec(id, newVersion, oldVersion, newNum);
|
||||
System.out.println("result:" + b);
|
||||
int i = atomicInteger.incrementAndGet();
|
||||
System.out.println("update count:" + i);
|
||||
return R.result(b);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public R updateSec1(Long id) {
|
||||
Seckill seckill = getById(id);
|
||||
if (seckill.getNumber() <= 0) {
|
||||
return R.error();
|
||||
}
|
||||
Integer oldNum = seckill.getNumber();
|
||||
Integer newNum = seckill.getNumber() - 1;
|
||||
System.out.println(Thread.currentThread().getName() + ":剩余库存:" + newNum);
|
||||
boolean b = seckillMapper.updateSec1(id, newNum, oldNum);
|
||||
System.out.println("result:" + b);
|
||||
int i = atomicInteger.incrementAndGet();
|
||||
System.out.println("update count:" + i);
|
||||
return R.result(b);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public R updateSec2(Long id) {
|
||||
boolean b = seckillMapper.updateSec2(id);
|
||||
System.out.println("result:" + b);
|
||||
return R.result(b);
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.wayn.mobile.api.mapper.SeckillMapper">
|
||||
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="com.wayn.mobile.api.domain.Seckill">
|
||||
<id column="seckill_id" property="seckillId" />
|
||||
<result column="name" property="name" />
|
||||
<result column="number" property="number" />
|
||||
<result column="start_time" property="startTime" />
|
||||
<result column="end_time" property="endTime" />
|
||||
<result column="create_time" property="createTime" />
|
||||
</resultMap>
|
||||
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
seckill_id, name, number, start_time, end_time, create_time
|
||||
</sql>
|
||||
<update id="updateSec">
|
||||
update seckill set number = #{newNum} , version = #{newVersion} where seckill_id = #{id} and version = #{oldVersion}
|
||||
</update>
|
||||
|
||||
<update id="updateSec1">
|
||||
update seckill set number = #{newNum} where seckill_id = #{id} and number = #{oldNum}
|
||||
</update>
|
||||
|
||||
<update id="updateSec2">
|
||||
update seckill set number = number - 1 where seckill_id = #{id} and number - 1 >= 0
|
||||
</update>
|
||||
</mapper>
|
Loading…
Reference in New Issue