pull/4/head
fengyu.wang 4 years ago
parent 6e43b3feb8
commit 07a37581ba
  1. 4
      blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/CurrencyMarketScheduling.java
  2. 13
      blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/Scheduling/HuobiMarketScheduling.java
  3. 5
      blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/redis/MarketKCache.java
  4. 54
      blockchain-server/blockchain-server-currency/src/main/java/com/blockchain/server/currency/service/impl/CurrencyMarketServiceImpl.java
  5. 5
      pom.xml
  6. 2
      spring-cloud/spring-cloud-config/src/main/resources/properties/dbconf-dev.yml
  7. 2
      spring-cloud/spring-cloud-config/src/main/resources/properties/txconf-dev.yml

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.blockchain.server.currency.common.constant.BaseCoinEnums; import com.blockchain.server.currency.common.constant.BaseCoinEnums;
import com.blockchain.server.currency.common.constant.RatesEnums; import com.blockchain.server.currency.common.constant.RatesEnums;
import com.blockchain.server.currency.redis.MarketLegalCache; 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.Autowired;
import org.springframework.beans.factory.annotation.Configurable; import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -15,6 +16,7 @@ import org.springframework.web.client.RestTemplate;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Slf4j
@Component @Component
@Configurable @Configurable
@EnableScheduling @EnableScheduling
@ -37,7 +39,7 @@ public class CurrencyMarketScheduling {
try { try {
setLegalMarket(); setLegalMarket();
} catch (Exception e) { } catch (Exception e) {
// e.printStackTrace(); log.error("CurrencyMarketScheduling.getCurrencyMarket: get currency market error", e);
} }
} }

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.blockchain.common.base.util.HttpUtilManager; import com.blockchain.common.base.util.HttpUtilManager;
import com.blockchain.server.currency.dto.CurrencyMarketDTO; import com.blockchain.server.currency.dto.CurrencyMarketDTO;
import com.blockchain.server.currency.service.CurrencyMarketService; 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.Autowired;
import org.springframework.beans.factory.annotation.Configurable; import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -19,6 +20,7 @@ import java.util.concurrent.Executors;
@Component @Component
@Configurable @Configurable
@EnableScheduling @EnableScheduling
@Slf4j
public class HuobiMarketScheduling { public class HuobiMarketScheduling {
@Autowired @Autowired
@ -45,8 +47,12 @@ public class HuobiMarketScheduling {
private void setHuobiMarket() { private void setHuobiMarket() {
List<List> currencys = JSONObject.parseArray(huobiCurrencys,List.class); List<List> currencys = JSONObject.parseArray(huobiCurrencys,List.class);
for (List data : currencys){ for (List data : currencys){
saveMarket(getHuobiMarket(huobiUrl + data.get(0).toString()), try {
data.get(1).toString(),data.get(2).toString()); 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); CurrencyMarketDTO now = currencyMarketService.get(coinName + "-" + unitName);
for (JSONObject obj : list) { for (JSONObject obj : list) {
Long timestamp = Long.parseLong(obj.get("ts").toString()); Long timestamp = Long.parseLong(obj.get("ts").toString());
if(timestamp >= now.getTimestamp()) { if(now == null || timestamp >= now.getTimestamp()) {
List<JSONObject> data = (List<JSONObject>)obj.get("data"); List<JSONObject> data = (List<JSONObject>)obj.get("data");
currencyMarketService.save(coinName, unitName, currencyMarketService.save(coinName, unitName,
new BigDecimal(data.get(0).get("price").toString()), new BigDecimal(data.get(0).get("price").toString()),
new BigDecimal(data.get(0).get("amount").toString()), new BigDecimal(data.get(0).get("amount").toString()),
timestamp,data.get(0).get("direction").toString().toUpperCase()); timestamp,data.get(0).get("direction").toString().toUpperCase());
} else { } else {
log.warn("HuobiMarketScheduling.saveMarket: save market ignore");
break; break;
} }
} }

@ -51,8 +51,9 @@ public class MarketKCache {
} }
public void setMarketKListCache(SortedMap<String,CurrencyMarketKDTO> map, String currencyPair, String timeType, int timeNumber){ public void setMarketKListCache(SortedMap<String,CurrencyMarketKDTO> map, String currencyPair, String timeType, int timeNumber){
redisTemplate.opsForValue().set(MARKET_LIST_CACHE + currencyPair + ":" + timeNumber + timeType if (map != null && !map.isEmpty()) {
,map); redisTemplate.opsForValue().set(MARKET_LIST_CACHE + currencyPair + ":" + timeNumber + timeType, map);
}
} }
public SortedMap<String,CurrencyMarketKDTO> getMarketKListCache(String currencyPair, String timeType, int timeNumber){ public SortedMap<String,CurrencyMarketKDTO> getMarketKListCache(String currencyPair, String timeType, int timeNumber){

@ -21,8 +21,10 @@ import com.blockchain.server.currency.redis.MarketLegalCache;
import com.blockchain.server.currency.service.CurrencyMarketService; import com.blockchain.server.currency.service.CurrencyMarketService;
import com.blockchain.server.currency.service.CurrencyPairService; import com.blockchain.server.currency.service.CurrencyPairService;
import com.blockchain.server.currency.websocket.WebSocketSendMsgUtils; import com.blockchain.server.currency.websocket.WebSocketSendMsgUtils;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -32,6 +34,7 @@ import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@Slf4j
@Service @Service
public class CurrencyMarketServiceImpl implements CurrencyMarketService { public class CurrencyMarketServiceImpl implements CurrencyMarketService {
@ -95,24 +98,30 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService {
*/ */
@Override @Override
public CurrencyMarketDTO save(String coinName, String unitName, BigDecimal amount, BigDecimal total, Long timestamp, String tradingType) { public CurrencyMarketDTO save(String coinName, String unitName, BigDecimal amount, BigDecimal total, Long timestamp, String tradingType) {
String currencyPair = coinName + "-" + unitName; try {
//检测币对是否合法 String currencyPair = coinName + "-" + unitName;
checkCurrency(currencyPair); //检测币对是否合法
CurrencyMarket currencyMarket = new CurrencyMarket(); checkCurrency(currencyPair);
currencyMarket.setCurrencyPair(currencyPair); CurrencyMarket currencyMarket = new CurrencyMarket();
currencyMarket.setTotal(total); currencyMarket.setCurrencyPair(currencyPair);
currencyMarket.setAmount(amount); currencyMarket.setTotal(total);
currencyMarket.setTimestamp(timestamp); currencyMarket.setAmount(amount);
//保存新数据 currencyMarket.setTimestamp(timestamp);
save(currencyMarket); //保存新数据
CurrencyMarketDTO dto = new CurrencyMarketDTO(); save(currencyMarket);
BeanUtils.copyProperties(currencyMarket, dto); CurrencyMarketDTO dto = new CurrencyMarketDTO();
//发送历史成交记录信息 BeanUtils.copyProperties(currencyMarket, dto);
sendHistoryMsg(dto, tradingType); //发送历史成交记录信息
//添加缓存数据,发送行情变动信息 sendHistoryMsg(dto, tradingType);
setMarketCache(dto); //添加缓存数据,发送行情变动信息
setMarketCache(dto);
return 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 @Override
@ -122,6 +131,9 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService {
//获取一天的K线行情 //获取一天的K线行情
SortedMap<String, CurrencyMarketKDTO> map = getOneDayMarketK(currencyPair, SortedMap<String, CurrencyMarketKDTO> map = getOneDayMarketK(currencyPair,
MarketKTypeEnums.ONEDAY.getTimeType(), MarketKTypeEnums.ONEDAY.getTimeNumber()); MarketKTypeEnums.ONEDAY.getTimeType(), MarketKTypeEnums.ONEDAY.getTimeNumber());
if (map == null || map.isEmpty()) {
return null;
}
CurrencyMarketDTO dto = getMarketInfo(currencyPair, map); CurrencyMarketDTO dto = getMarketInfo(currencyPair, map);
return dto; return dto;
} }
@ -432,9 +444,9 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService {
ktype.getTimeType(), ktype.getTimeNumber()); ktype.getTimeType(), ktype.getTimeNumber());
if (lock) { if (lock) {
SortedMap<String, CurrencyMarketKDTO> map = marketKCache.getMarketKListCache(dto.getCurrencyPair(), ktype.getTimeType(), ktype.getTimeNumber()); SortedMap<String, CurrencyMarketKDTO> 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()); 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(); Long timestamp = (dto.getTimestamp() / (ktype.getTimeNumber() * MarketKEnums.valueOf(ktype.getTimeType()).getSecond())) * ktype.getTimeNumber() * MarketKEnums.valueOf(ktype.getTimeType()).getSecond();
CurrencyMarketKDTO kdto = map.get(timestamp.toString()); CurrencyMarketKDTO kdto = map.get(timestamp.toString());
if (kdto == null) { if (kdto == null) {
@ -608,7 +620,7 @@ public class CurrencyMarketServiceImpl implements CurrencyMarketService {
*/ */
private SortedMap<String, CurrencyMarketKDTO> getOneDayMarketK(String currencyPair, String timeType, Integer timeNumber) { private SortedMap<String, CurrencyMarketKDTO> getOneDayMarketK(String currencyPair, String timeType, Integer timeNumber) {
SortedMap<String, CurrencyMarketKDTO> map = marketKCache.getMarketKListCache(currencyPair, timeType, timeNumber); SortedMap<String, CurrencyMarketKDTO> map = marketKCache.getMarketKListCache(currencyPair, timeType, timeNumber);
if (map == null) { if (map == null || map.isEmpty()) {
boolean lock = marketKCache.tryFairLock(redissonClient, currencyPair, boolean lock = marketKCache.tryFairLock(redissonClient, currencyPair,
timeType, timeNumber); timeType, timeNumber);
if (lock) { if (lock) {

@ -33,8 +33,9 @@
<custom-project-version>latest</custom-project-version> <custom-project-version>latest</custom-project-version>
<!-- 配置信息 --> <!-- 配置信息 -->
<eureka-host>zhixinlian:zhi123xin678lian@127.0.0.1</eureka-host> <eureka-host>zhixinlian:zhi123xin678lian@spring-cloud-eureka</eureka-host>
<redis-host>127.0.0.1</redis-host> <redis-host>redis</redis-host>
<tx-host>tx-manager</tx-host>
</properties> </properties>
<dependencies> <dependencies>

@ -1,6 +1,6 @@
spring: spring:
datasource: 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 username: root
password: 123456 password: 123456
driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver

@ -1,4 +1,4 @@
#分布式事务配置 #分布式事务配置
tm: tm:
manager: manager:
url: http://${redis-host}:7000/tx/manager/ url: http://${tx-host}:7000/tx/manager/

Loading…
Cancel
Save