ariesy 增加redisson集群配置

release
yefei 2 years ago
parent 374a93297e
commit 1812801da7
  1. 154
      dxhy-core/src/main/java/com/dxhy/core/config/RedissionConfig.java
  2. 230
      dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.java

@ -1,33 +1,135 @@
package com.dxhy.core.config; package com.dxhy.core.config;
import org.springframework.context.annotation.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson; import org.redisson.Redisson;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config; 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.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration import java.util.List;
//public class RedissionConfig { import java.util.Objects;
//
// @Value("${spring.redis.host}") @Configuration
// private String host; public class RedissionConfig {
// Logger log = LoggerFactory.getLogger(RedissionConfig.class);
// @Value("${spring.redis.port}")
// private String port; //直接使用starter注入的redis配置信息
// @Autowired
// @Value("${spring.redis.password}") private RedisProperties redisProperties;
// private String redisPassword;
// private int timeout = 3000;
// @Bean private int connectionPoolSize = 64;
// public RedissonClient getRedisson(){ private int connectionMinimumIdleSize = 10;
// private int pingConnectionInterval = 60000;
// Config config = new Config(); private static String ADDRESS_PREFIX = "redis://";
// //单机模式 依次设置redis地址和密码
// config.useSingleServer() /**
// .setAddress("redis://" + host + ":" + port) * 单机模式
// .setPassword(redisPassword); */
// return Redisson.create(config); @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<String> 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<String> 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);
}
}

@ -78,8 +78,8 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate stringRedisTemplate;
// @Resource @Resource
// private RedissonClient redisson; private RedissonClient redisson;
private static final String REDIS_KEY = "collect_invoice_"; private static final String REDIS_KEY = "collect_invoice_";
@ -658,8 +658,8 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService {
List<InvoiceLog> logList = new ArrayList<>(); List<InvoiceLog> logList = new ArrayList<>();
for (InvoiceSelectInfo invoiceSelectInfo : invoicesList) { for (InvoiceSelectInfo invoiceSelectInfo : invoicesList) {
//加分布式锁 //加分布式锁
// String lockKey = REDIS_KEY + invoiceSelectInfo.getUuid(); String lockKey = REDIS_KEY + invoiceSelectInfo.getUuid();
// RLock lock = redisson.getLock(lockKey); RLock lock = redisson.getLock(lockKey);
invoiceSelectInfo.setUuid(invoiceSelectInfo.getInvoiceCode() + invoiceSelectInfo.getInvoiceNo()); invoiceSelectInfo.setUuid(invoiceSelectInfo.getInvoiceCode() + invoiceSelectInfo.getInvoiceNo());
if ("1".equals(invoiceSelectInfo.getLegalizeState())) { if ("1".equals(invoiceSelectInfo.getLegalizeState())) {
//认证处理状态 0-未认证 1-已勾选未确认,2已确认 3 已发送认证 4 认证成功 5 认证失败 //认证处理状态 0-未认证 1-已勾选未确认,2已确认 3 已发送认证 4 认证成功 5 认证失败
@ -668,124 +668,124 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService {
invoiceSelectInfo.setAuthStatus("0"); invoiceSelectInfo.setAuthStatus("0");
} }
try { try {
// boolean isLock = lock.tryLock(); boolean isLock = lock.tryLock();
// if(!isLock){ if(!isLock){
// log.info("当前已有线程获取到锁"); log.info("当前已有线程获取到锁");
// }else { }else {
// 判断库里是否已经存在,存在则只更新发票状态,状态更新时间,认证状态以及相关字段更新 // 判断库里是否已经存在,存在则只更新发票状态,状态更新时间,认证状态以及相关字段更新
DynamicContextHolder.push(db + DbConstant.BUSINESS_READ); DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
TDxRecordInvoiceJobEntity entity = tDxRecordInvoiceJobDao.findInvoiceByUUid(invoiceSelectInfo.getUuid()); TDxRecordInvoiceJobEntity entity = tDxRecordInvoiceJobDao.findInvoiceByUUid(invoiceSelectInfo.getUuid());
if (entity != null) { if (entity != null) {
boolean flag = false; boolean flag = false;
if (StringUtils.isBlank(entity.getCheckCode()) if (StringUtils.isBlank(entity.getCheckCode())
&& StringUtils.isNotBlank(invoiceSelectInfo.getCheckCode())) { && StringUtils.isNotBlank(invoiceSelectInfo.getCheckCode())) {
entity.setCheckCode(invoiceSelectInfo.getCheckCode()); entity.setCheckCode(invoiceSelectInfo.getCheckCode());
entity.setCheckDate(new Date()); entity.setCheckDate(new Date());
flag = true; flag = true;
} }
if (!entity.getInvoiceStatus().equals(invoiceSelectInfo.getInvoiceStatus())) { if (!entity.getInvoiceStatus().equals(invoiceSelectInfo.getInvoiceStatus())) {
entity.setInvoiceStatus(invoiceSelectInfo.getInvoiceStatus()); entity.setInvoiceStatus(invoiceSelectInfo.getInvoiceStatus());
entity.setStatusUpdateDate(new Date()); entity.setStatusUpdateDate(new Date());
flag = true; flag = true;
} }
if ("0".equals(entity.getRzhYesorno()) if ("0".equals(entity.getRzhYesorno())
&& !entity.getRzhYesorno().equals(invoiceSelectInfo.getLegalizeState())) { && !entity.getRzhYesorno().equals(invoiceSelectInfo.getLegalizeState())) {
if ("1".equals(invoiceSelectInfo.getRzlx())) { if ("1".equals(invoiceSelectInfo.getRzlx())) {
entity.setRzhYesorno(invoiceSelectInfo.getLegalizeState()); entity.setRzhYesorno(invoiceSelectInfo.getLegalizeState());
entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate());
entity.setRzlx(invoiceSelectInfo.getRzlx()); entity.setRzlx(invoiceSelectInfo.getRzlx());
entity.setRzhType(invoiceSelectInfo.getLegalizeType()); entity.setRzhType(invoiceSelectInfo.getLegalizeType());
entity.setSfygx("1"); entity.setSfygx("1");
if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) {
SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate()));
} }
if ("1".equals(invoiceSelectInfo.getLegalizeState())) { if ("1".equals(invoiceSelectInfo.getLegalizeState())) {
entity.setAuthStatus("4"); entity.setAuthStatus("4");
} else { } else {
entity.setAuthStatus("0"); entity.setAuthStatus("0");
} }
entity.setBdkStatus("1"); entity.setBdkStatus("1");
} else if ("4".equals(invoiceSelectInfo.getRzlx())) { } else if ("4".equals(invoiceSelectInfo.getRzlx())) {
entity.setRzhYesorno("2"); entity.setRzhYesorno("2");
entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate());
entity.setRzlx(invoiceSelectInfo.getRzlx()); entity.setRzlx(invoiceSelectInfo.getRzlx());
entity.setRzhType(invoiceSelectInfo.getLegalizeType()); entity.setRzhType(invoiceSelectInfo.getLegalizeType());
entity.setSfygx("1"); entity.setSfygx("1");
if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) {
SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); 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())) { flag = true;
entity.setAuthStatus("4");
} else { } else if (!"0".equals(entity.getRzhYesorno())
entity.setAuthStatus("0"); && !"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; // 艺龙推送数据
if (yLcompany.equals(company)) {
} else if (!"0".equals(entity.getRzhYesorno()) DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
&& !"1".equals(invoiceSelectInfo.getLegalizeState())) { InvoiceScanEntity selectSign = tDxRecordInvoiceJobDao
entity.setRzhYesorno("0"); .selectByScan(entity.getInvoiceCode() + entity.getInvoiceNo(), dxhyAdmin);
entity.setRzhBelongDate(null); if (selectSign != null && "1".equals(selectSign.getQsStatus())) {
entity.setRzlx("0"); String record = JSON.toJSONString(entity);
entity.setRzhType(null); sender.sendToStatus(Base64.encode(record));
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("");
} }
tDxRecordInvoiceJobDao.updateInvoice(entity, taxno); } else {
} if (StringUtils.isBlank(invoiceSelectInfo.getLegalizeDate())) {
// 艺龙推送数据 invoiceSelectInfo.setLegalizeDate(null);
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));
} }
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 { InvoiceLog invoiceLog = new InvoiceLog();
if (StringUtils.isBlank(invoiceSelectInfo.getLegalizeDate())) { invoiceLog.setInvoiceCode(invoiceSelectInfo.getInvoiceCode());
invoiceSelectInfo.setLegalizeDate(null); invoiceLog.setInvoiceNo(invoiceSelectInfo.getInvoiceNo());
} invoiceLog.setType("1");
TDxRecordInvoiceJobEntity record = exchangePo2Entity(invoiceSelectInfo); invoiceLog.setInputStatus("0");
record.setCompany(company); invoiceLog.setCreateDate(new Date());
record.setCollectStatus("1"); invoiceLog.setInputName("系统自动");
record.setCollectFrom("0"); logList.add(invoiceLog);
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);
// }
}catch (Exception e) { }catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

Loading…
Cancel
Save