diff --git a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/CurrencyMarketScheduling.java b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/CurrencyMarketScheduling.java index 997bc55..96ce817 100644 --- a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/CurrencyMarketScheduling.java +++ b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/CurrencyMarketScheduling.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; import com.blockchain.server.currency.common.constant.BaseCoinEnums; import com.blockchain.server.currency.common.constant.RatesEnums; import com.blockchain.server.currency.redis.MarketLegalCache; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.beans.factory.annotation.Value; @@ -15,6 +16,7 @@ import org.springframework.web.client.RestTemplate; import java.util.List; import java.util.Map; +@Slf4j @Component @Configurable @EnableScheduling @@ -37,7 +39,7 @@ public class CurrencyMarketScheduling { try { setLegalMarket(); } catch (Exception e) { -// e.printStackTrace(); + log.error("CurrencyMarketScheduling.getCurrencyMarket: get currency market error", e); } } diff --git a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/HuobiMarketScheduling.java b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/HuobiMarketScheduling.java index 9b42377..536d8b6 100644 --- a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/HuobiMarketScheduling.java +++ b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/HuobiMarketScheduling.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; import com.blockchain.common.base.util.HttpUtilManager; import com.blockchain.server.currency.dto.CurrencyMarketDTO; import com.blockchain.server.currency.service.CurrencyMarketService; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.beans.factory.annotation.Value; @@ -19,6 +20,7 @@ import java.util.concurrent.Executors; @Component @Configurable @EnableScheduling +@Slf4j public class HuobiMarketScheduling { @Autowired @@ -45,8 +47,12 @@ public class HuobiMarketScheduling { private void setHuobiMarket() { List currencys = JSONObject.parseArray(huobiCurrencys,List.class); for (List data : currencys){ - saveMarket(getHuobiMarket(huobiUrl + data.get(0).toString()), - data.get(1).toString(),data.get(2).toString()); + try { + saveMarket(getHuobiMarket(huobiUrl + data.get(0).toString()), + data.get(1).toString(),data.get(2).toString()); + } catch (Exception e) { + log.error("HuobiMarketScheduling.setHuobiMarket: save market error, data={}", data, e); + } } } @@ -60,13 +66,14 @@ public class HuobiMarketScheduling { CurrencyMarketDTO now = currencyMarketService.get(coinName + "-" + unitName); for (JSONObject obj : list) { Long timestamp = Long.parseLong(obj.get("ts").toString()); - if(timestamp >= now.getTimestamp()) { + if(now == null || timestamp >= now.getTimestamp()) { List data = (List)obj.get("data"); currencyMarketService.save(coinName, unitName, new BigDecimal(data.get(0).get("price").toString()), new BigDecimal(data.get(0).get("amount").toString()), timestamp,data.get(0).get("direction").toString().toUpperCase()); } else { + log.warn("HuobiMarketScheduling.saveMarket: save market ignore"); break; } } diff --git a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/redis/MarketKCache.java b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/redis/MarketKCache.java index 2f68194..cf4c3ed 100644 --- a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/redis/MarketKCache.java +++ b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/redis/MarketKCache.java @@ -51,8 +51,9 @@ public class MarketKCache { } public void setMarketKListCache(SortedMap map, String currencyPair, String timeType, int timeNumber){ - redisTemplate.opsForValue().set(MARKET_LIST_CACHE + currencyPair + ":" + timeNumber + timeType - ,map); + if (map != null && !map.isEmpty()) { + redisTemplate.opsForValue().set(MARKET_LIST_CACHE + currencyPair + ":" + timeNumber + timeType, map); + } } public SortedMap getMarketKListCache(String currencyPair, String timeType, int timeNumber){ diff --git a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/service/impl/CurrencyMarketServiceImpl.java b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/service/impl/CurrencyMarketServiceImpl.java index f5bf687..48386f4 100644 --- a/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/service/impl/CurrencyMarketServiceImpl.java +++ b/blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/service/impl/CurrencyMarketServiceImpl.java @@ -21,8 +21,10 @@ import com.blockchain.server.currency.redis.MarketLegalCache; import com.blockchain.server.currency.service.CurrencyMarketService; import com.blockchain.server.currency.service.CurrencyPairService; import com.blockchain.server.currency.websocket.WebSocketSendMsgUtils; +import lombok.extern.slf4j.Slf4j; import org.redisson.api.RedissonClient; import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; @@ -32,6 +34,7 @@ import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +@Slf4j @Service public class CurrencyMarketServiceImpl implements CurrencyMarketService { @@ -95,24 +98,30 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService { */ @Override public CurrencyMarketDTO save(String coinName, String unitName, BigDecimal amount, BigDecimal total, Long timestamp, String tradingType) { - String currencyPair = coinName + "-" + unitName; - //检测币对是否合法 - checkCurrency(currencyPair); - CurrencyMarket currencyMarket = new CurrencyMarket(); - currencyMarket.setCurrencyPair(currencyPair); - currencyMarket.setTotal(total); - currencyMarket.setAmount(amount); - currencyMarket.setTimestamp(timestamp); - //保存新数据 - save(currencyMarket); - CurrencyMarketDTO dto = new CurrencyMarketDTO(); - BeanUtils.copyProperties(currencyMarket, dto); - //发送历史成交记录信息 - sendHistoryMsg(dto, tradingType); - //添加缓存数据,发送行情变动信息 - setMarketCache(dto); - - return dto; + try { + String currencyPair = coinName + "-" + unitName; + //检测币对是否合法 + checkCurrency(currencyPair); + CurrencyMarket currencyMarket = new CurrencyMarket(); + currencyMarket.setCurrencyPair(currencyPair); + currencyMarket.setTotal(total); + currencyMarket.setAmount(amount); + currencyMarket.setTimestamp(timestamp); + //保存新数据 + save(currencyMarket); + CurrencyMarketDTO dto = new CurrencyMarketDTO(); + BeanUtils.copyProperties(currencyMarket, dto); + //发送历史成交记录信息 + sendHistoryMsg(dto, tradingType); + //添加缓存数据,发送行情变动信息 + setMarketCache(dto); + + return dto; + } catch (BeansException e) { + log.error("CurrencyMarketServiceImpl.save: save error, coinName={}, unitName={}, amount={}, total={}, timestamp={}, tradingType={}", + coinName, unitName, amount, total, timestamp, tradingType); + return null; + } } @Override @@ -122,6 +131,9 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService { //获取一天的K线行情 SortedMap map = getOneDayMarketK(currencyPair, MarketKTypeEnums.ONEDAY.getTimeType(), MarketKTypeEnums.ONEDAY.getTimeNumber()); + if (map == null || map.isEmpty()) { + return null; + } CurrencyMarketDTO dto = getMarketInfo(currencyPair, map); return dto; } @@ -432,9 +444,9 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService { ktype.getTimeType(), ktype.getTimeNumber()); if (lock) { SortedMap map = marketKCache.getMarketKListCache(dto.getCurrencyPair(), ktype.getTimeType(), ktype.getTimeNumber()); - if (map == null) + if (map == null || map.isEmpty()) map = selectMarketK(dto.getCurrencyPair(), ktype.getTimeType(), ktype.getTimeNumber()); - if (map == null) continue; + if (map == null || map.isEmpty()) continue; Long timestamp = (dto.getTimestamp() / (ktype.getTimeNumber() * MarketKEnums.valueOf(ktype.getTimeType()).getSecond())) * ktype.getTimeNumber() * MarketKEnums.valueOf(ktype.getTimeType()).getSecond(); CurrencyMarketKDTO kdto = map.get(timestamp.toString()); if (kdto == null) { @@ -608,7 +620,7 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService { */ private SortedMap getOneDayMarketK(String currencyPair, String timeType, Integer timeNumber) { SortedMap map = marketKCache.getMarketKListCache(currencyPair, timeType, timeNumber); - if (map == null) { + if (map == null || map.isEmpty()) { boolean lock = marketKCache.tryFairLock(redissonClient, currencyPair, timeType, timeNumber); if (lock) { diff --git a/pom.xml b/pom.xml index edf54a7..6c9deb0 100644 --- a/pom.xml +++ b/pom.xml @@ -33,8 +33,9 @@ latest - zhixinlian:zhi123xin678lian@127.0.0.1 - 127.0.0.1 + zhixinlian:zhi123xin678lian@spring-cloud-eureka + redis + tx-manager @@ -117,4 +118,4 @@ - \ No newline at end of file + diff --git a/spring-cloud/spring-cloud-config/src/main/resources/properties/dbconf-dev.yml b/spring-cloud/spring-cloud-config/src/main/resources/properties/dbconf-dev.yml index 6df60f3..6e4e044 100644 --- a/spring-cloud/spring-cloud-config/src/main/resources/properties/dbconf-dev.yml +++ b/spring-cloud/spring-cloud-config/src/main/resources/properties/dbconf-dev.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:mysql://127.0.0.1:3306/zhixinlian?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true + url: jdbc:mysql://mysql:3306/zhixinlian?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver diff --git a/spring-cloud/spring-cloud-config/src/main/resources/properties/txconf-dev.yml b/spring-cloud/spring-cloud-config/src/main/resources/properties/txconf-dev.yml index 3bd6f8e..7de9172 100644 --- a/spring-cloud/spring-cloud-config/src/main/resources/properties/txconf-dev.yml +++ b/spring-cloud/spring-cloud-config/src/main/resources/properties/txconf-dev.yml @@ -1,4 +1,4 @@ #分布式事务配置 tm: manager: - url: http://${redis-host}:7000/tx/manager/ \ No newline at end of file + url: http://${tx-host}:7000/tx/manager/