From 1812801da7656f1d78e866f4b632f150931085d9 Mon Sep 17 00:00:00 2001 From: yefei Date: Thu, 19 Oct 2023 10:17:13 +0800 Subject: [PATCH] =?UTF-8?q?ariesy=20=E5=A2=9E=E5=8A=A0redisson=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dxhy/core/config/RedissionConfig.java | 154 ++++++++++-- .../impl/InvoiceInterfaceServiceImpl.java | 230 +++++++++--------- 2 files changed, 243 insertions(+), 141 deletions(-) diff --git a/dxhy-core/src/main/java/com/dxhy/core/config/RedissionConfig.java b/dxhy-core/src/main/java/com/dxhy/core/config/RedissionConfig.java index 33478921..074883d7 100644 --- a/dxhy-core/src/main/java/com/dxhy/core/config/RedissionConfig.java +++ b/dxhy-core/src/main/java/com/dxhy/core/config/RedissionConfig.java @@ -1,33 +1,135 @@ package com.dxhy.core.config; +import org.springframework.context.annotation.Configuration; +import org.apache.commons.lang3.StringUtils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; +import org.redisson.config.ClusterServersConfig; import org.redisson.config.Config; -import org.springframework.beans.factory.annotation.Value; +import org.redisson.config.SentinelServersConfig; +import org.redisson.config.SingleServerConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -//@Configuration -//public class RedissionConfig { -// -// @Value("${spring.redis.host}") -// private String host; -// -// @Value("${spring.redis.port}") -// private String port; -// -// @Value("${spring.redis.password}") -// private String redisPassword; -// -// @Bean -// public RedissonClient getRedisson(){ -// -// Config config = new Config(); -// //单机模式 依次设置redis地址和密码 -// config.useSingleServer() -// .setAddress("redis://" + host + ":" + port) -// .setPassword(redisPassword); -// return Redisson.create(config); -// } -//} -// +import java.util.List; +import java.util.Objects; + +@Configuration +public class RedissionConfig { + Logger log = LoggerFactory.getLogger(RedissionConfig.class); + + //直接使用starter注入的redis配置信息 + @Autowired + private RedisProperties redisProperties; + + private int timeout = 3000; + private int connectionPoolSize = 64; + private int connectionMinimumIdleSize = 10; + private int pingConnectionInterval = 60000; + private static String ADDRESS_PREFIX = "redis://"; + + /** + * 单机模式 + */ + @Bean + public RedissonClient initBean() { + // 哨兵模式 + RedisProperties.Sentinel sentinel = redisProperties.getSentinel(); + if (Objects.nonNull(sentinel)) { + log.info("redis is sentinel mode"); + return redissonSentinel(); + } + // 集群模式 + RedisProperties.Cluster cluster = redisProperties.getCluster(); + if (Objects.nonNull(cluster)) { + log.info("redis is cluster mode"); + return redissonCluster(); + } + // 单机模式 + String host = redisProperties.getHost(); + if (StringUtils.isNotBlank(host)) { + log.info("redis is single mode"); + return redissonSingle(); + } + + log.error("redisson config can not support this redis mode"); + return null; + } + + /** + * 单机模式 + */ + private RedissonClient redissonSingle() { + String host = redisProperties.getHost(); + String password = redisProperties.getPassword(); + int port = redisProperties.getPort(); + // 声明一个配置类 + Config config = new Config(); + SingleServerConfig serverConfig = config.useSingleServer() + .setAddress(ADDRESS_PREFIX + host + ":" + port) + .setTimeout(timeout) + .setPingConnectionInterval(pingConnectionInterval) + .setConnectionPoolSize(this.connectionPoolSize) + .setConnectionMinimumIdleSize(this.connectionMinimumIdleSize); + // 判断密码 + if (!StringUtils.isEmpty(password)) { + serverConfig.setPassword(password); + } + return Redisson.create(config); + } + + + /** + * 哨兵模式 + */ + private RedissonClient redissonSentinel() { + // mymaster + String masterName = redisProperties.getSentinel().getMaster(); + // 127.0.0.1:26389,127.0.0.1:26379 + List nodes = redisProperties.getSentinel().getNodes(); + String password = redisProperties.getPassword(); + // 声明一个配置类 + Config config = new Config(); + SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); + // 扫描间隔 + sentinelServersConfig.setScanInterval(2000); + // 判断密码 + if (!StringUtils.isEmpty(password)) { + sentinelServersConfig.setPassword(password); + } + sentinelServersConfig.setMasterName(masterName); + String[] nodeArr = nodes.toArray(new String[nodes.size()]); + for (int i = 0; i < nodeArr.length; i++) { + nodeArr[i] = ADDRESS_PREFIX + nodeArr[i]; + } + // 添加redis节点 + sentinelServersConfig.addSentinelAddress(nodeArr); + return Redisson.create(config); + } + + /** + * 集群模式 + */ + private RedissonClient redissonCluster() { + // 192.168.116.156:1901,192.168.116.156:1902 + List nodes = redisProperties.getCluster().getNodes(); + String password = redisProperties.getPassword(); + // 声明一个配置类 + Config config = new Config(); + ClusterServersConfig clusterServersConfig = config.useClusterServers(); + // 扫描间隔 + clusterServersConfig.setScanInterval(2000); + // 判断密码 + if (!StringUtils.isEmpty(password)) { + clusterServersConfig.setPassword(password); + } + // 添加redis节点 + for (String node : nodes) { + clusterServersConfig.addNodeAddress(ADDRESS_PREFIX + node); + } + return Redisson.create(config); + } +} \ No newline at end of file diff --git a/dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.java b/dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.java index c62e119f..80887a56 100644 --- a/dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.java +++ b/dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.java @@ -78,8 +78,8 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService { @Resource private StringRedisTemplate stringRedisTemplate; -// @Resource -// private RedissonClient redisson; + @Resource + private RedissonClient redisson; private static final String REDIS_KEY = "collect_invoice_"; @@ -658,8 +658,8 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService { List logList = new ArrayList<>(); for (InvoiceSelectInfo invoiceSelectInfo : invoicesList) { //加分布式锁 -// String lockKey = REDIS_KEY + invoiceSelectInfo.getUuid(); -// RLock lock = redisson.getLock(lockKey); + String lockKey = REDIS_KEY + invoiceSelectInfo.getUuid(); + RLock lock = redisson.getLock(lockKey); invoiceSelectInfo.setUuid(invoiceSelectInfo.getInvoiceCode() + invoiceSelectInfo.getInvoiceNo()); if ("1".equals(invoiceSelectInfo.getLegalizeState())) { //认证处理状态 0-未认证 1-已勾选未确认,2已确认 3 已发送认证 4 认证成功 5 认证失败 @@ -668,124 +668,124 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService { invoiceSelectInfo.setAuthStatus("0"); } try { -// boolean isLock = lock.tryLock(); - -// if(!isLock){ -// log.info("当前已有线程获取到锁"); -// }else { - // 判断库里是否已经存在,存在则只更新发票状态,状态更新时间,认证状态以及相关字段更新 - DynamicContextHolder.push(db + DbConstant.BUSINESS_READ); - TDxRecordInvoiceJobEntity entity = tDxRecordInvoiceJobDao.findInvoiceByUUid(invoiceSelectInfo.getUuid()); - if (entity != null) { - boolean flag = false; - if (StringUtils.isBlank(entity.getCheckCode()) - && StringUtils.isNotBlank(invoiceSelectInfo.getCheckCode())) { - entity.setCheckCode(invoiceSelectInfo.getCheckCode()); - entity.setCheckDate(new Date()); - flag = true; - } - if (!entity.getInvoiceStatus().equals(invoiceSelectInfo.getInvoiceStatus())) { - entity.setInvoiceStatus(invoiceSelectInfo.getInvoiceStatus()); - entity.setStatusUpdateDate(new Date()); - flag = true; - } - if ("0".equals(entity.getRzhYesorno()) - && !entity.getRzhYesorno().equals(invoiceSelectInfo.getLegalizeState())) { - if ("1".equals(invoiceSelectInfo.getRzlx())) { - entity.setRzhYesorno(invoiceSelectInfo.getLegalizeState()); - entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); - entity.setRzlx(invoiceSelectInfo.getRzlx()); - entity.setRzhType(invoiceSelectInfo.getLegalizeType()); - entity.setSfygx("1"); - if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { - SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); - entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); - } - if ("1".equals(invoiceSelectInfo.getLegalizeState())) { - entity.setAuthStatus("4"); - } else { - entity.setAuthStatus("0"); - } - entity.setBdkStatus("1"); - } else if ("4".equals(invoiceSelectInfo.getRzlx())) { - entity.setRzhYesorno("2"); - entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); - entity.setRzlx(invoiceSelectInfo.getRzlx()); - entity.setRzhType(invoiceSelectInfo.getLegalizeType()); - entity.setSfygx("1"); - if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { - SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); - entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); + boolean isLock = lock.tryLock(); + + if(!isLock){ + log.info("当前已有线程获取到锁"); + }else { + // 判断库里是否已经存在,存在则只更新发票状态,状态更新时间,认证状态以及相关字段更新 + DynamicContextHolder.push(db + DbConstant.BUSINESS_READ); + TDxRecordInvoiceJobEntity entity = tDxRecordInvoiceJobDao.findInvoiceByUUid(invoiceSelectInfo.getUuid()); + if (entity != null) { + boolean flag = false; + if (StringUtils.isBlank(entity.getCheckCode()) + && StringUtils.isNotBlank(invoiceSelectInfo.getCheckCode())) { + entity.setCheckCode(invoiceSelectInfo.getCheckCode()); + entity.setCheckDate(new Date()); + flag = true; + } + if (!entity.getInvoiceStatus().equals(invoiceSelectInfo.getInvoiceStatus())) { + entity.setInvoiceStatus(invoiceSelectInfo.getInvoiceStatus()); + entity.setStatusUpdateDate(new Date()); + flag = true; + } + if ("0".equals(entity.getRzhYesorno()) + && !entity.getRzhYesorno().equals(invoiceSelectInfo.getLegalizeState())) { + if ("1".equals(invoiceSelectInfo.getRzlx())) { + entity.setRzhYesorno(invoiceSelectInfo.getLegalizeState()); + entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); + entity.setRzlx(invoiceSelectInfo.getRzlx()); + entity.setRzhType(invoiceSelectInfo.getLegalizeType()); + entity.setSfygx("1"); + if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { + SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); + entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); + } + if ("1".equals(invoiceSelectInfo.getLegalizeState())) { + entity.setAuthStatus("4"); + } else { + entity.setAuthStatus("0"); + } + entity.setBdkStatus("1"); + } else if ("4".equals(invoiceSelectInfo.getRzlx())) { + entity.setRzhYesorno("2"); + entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); + entity.setRzlx(invoiceSelectInfo.getRzlx()); + entity.setRzhType(invoiceSelectInfo.getLegalizeType()); + entity.setSfygx("1"); + if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { + SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); + entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); + } + if ("1".equals(invoiceSelectInfo.getLegalizeState())) { + entity.setAuthStatus("4"); + } else { + entity.setAuthStatus("0"); + } + entity.setBdkStatus("2"); } - if ("1".equals(invoiceSelectInfo.getLegalizeState())) { - entity.setAuthStatus("4"); - } else { - entity.setAuthStatus("0"); + flag = true; + + } else if (!"0".equals(entity.getRzhYesorno()) + && !"1".equals(invoiceSelectInfo.getLegalizeState())) { + entity.setRzhYesorno("0"); + entity.setRzhBelongDate(null); + entity.setRzlx("0"); + entity.setRzhType(null); + entity.setRzhDate(null); + entity.setAuthStatus("0"); + entity.setBdkStatus("0"); + entity.setSfygx("0"); + flag = true; + } + //TODO 这块业务不理解?? + if ("1".equals(entity.getSourceSystem()) && "0".equals(entity.getCollectStatus())) { + entity.setCollectStatus("1"); + entity.setCollectFrom("0"); + entity.setCollectDate(new Date()); + entity.setPoolStatus("1"); + entity.setInPoolReason("数据重复"); + flag = true; + } + if (flag) { + DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE); + if (StringUtils.isBlank(entity.getInvoiceCode())) { + entity.setInvoiceCode(""); } - entity.setBdkStatus("2"); + tDxRecordInvoiceJobDao.updateInvoice(entity, taxno); } - flag = true; - - } else if (!"0".equals(entity.getRzhYesorno()) - && !"1".equals(invoiceSelectInfo.getLegalizeState())) { - entity.setRzhYesorno("0"); - entity.setRzhBelongDate(null); - entity.setRzlx("0"); - entity.setRzhType(null); - entity.setRzhDate(null); - entity.setAuthStatus("0"); - entity.setBdkStatus("0"); - entity.setSfygx("0"); - flag = true; - } - //TODO 这块业务不理解?? - if ("1".equals(entity.getSourceSystem()) && "0".equals(entity.getCollectStatus())) { - entity.setCollectStatus("1"); - entity.setCollectFrom("0"); - entity.setCollectDate(new Date()); - entity.setPoolStatus("1"); - entity.setInPoolReason("数据重复"); - flag = true; - } - if (flag) { - DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE); - if (StringUtils.isBlank(entity.getInvoiceCode())) { - entity.setInvoiceCode(""); + // 艺龙推送数据 + if (yLcompany.equals(company)) { + DynamicContextHolder.push(db + DbConstant.BUSINESS_READ); + InvoiceScanEntity selectSign = tDxRecordInvoiceJobDao + .selectByScan(entity.getInvoiceCode() + entity.getInvoiceNo(), dxhyAdmin); + if (selectSign != null && "1".equals(selectSign.getQsStatus())) { + String record = JSON.toJSONString(entity); + sender.sendToStatus(Base64.encode(record)); + } } - tDxRecordInvoiceJobDao.updateInvoice(entity, taxno); - } - // 艺龙推送数据 - if (yLcompany.equals(company)) { - DynamicContextHolder.push(db + DbConstant.BUSINESS_READ); - InvoiceScanEntity selectSign = tDxRecordInvoiceJobDao - .selectByScan(entity.getInvoiceCode() + entity.getInvoiceNo(), dxhyAdmin); - if (selectSign != null && "1".equals(selectSign.getQsStatus())) { - String record = JSON.toJSONString(entity); - sender.sendToStatus(Base64.encode(record)); + } else { + if (StringUtils.isBlank(invoiceSelectInfo.getLegalizeDate())) { + invoiceSelectInfo.setLegalizeDate(null); } + TDxRecordInvoiceJobEntity record = exchangePo2Entity(invoiceSelectInfo); + record.setCompany(company); + record.setCollectStatus("1"); + record.setCollectFrom("0"); + record.setCollectDate(new Date()); + record.setPoolStatus("0"); + DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE); + tDxRecordInvoiceJobDao.insert(record); } - } else { - if (StringUtils.isBlank(invoiceSelectInfo.getLegalizeDate())) { - invoiceSelectInfo.setLegalizeDate(null); - } - TDxRecordInvoiceJobEntity record = exchangePo2Entity(invoiceSelectInfo); - record.setCompany(company); - record.setCollectStatus("1"); - record.setCollectFrom("0"); - record.setCollectDate(new Date()); - record.setPoolStatus("0"); - DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE); - tDxRecordInvoiceJobDao.insert(record); + InvoiceLog invoiceLog = new InvoiceLog(); + invoiceLog.setInvoiceCode(invoiceSelectInfo.getInvoiceCode()); + invoiceLog.setInvoiceNo(invoiceSelectInfo.getInvoiceNo()); + invoiceLog.setType("1"); + invoiceLog.setInputStatus("0"); + invoiceLog.setCreateDate(new Date()); + invoiceLog.setInputName("系统自动"); + logList.add(invoiceLog); } - InvoiceLog invoiceLog = new InvoiceLog(); - invoiceLog.setInvoiceCode(invoiceSelectInfo.getInvoiceCode()); - invoiceLog.setInvoiceNo(invoiceSelectInfo.getInvoiceNo()); - invoiceLog.setType("1"); - invoiceLog.setInputStatus("0"); - invoiceLog.setCreateDate(new Date()); - invoiceLog.setInputName("系统自动"); - logList.add(invoiceLog); -// } }catch (Exception e) { e.printStackTrace(); }