소스 검색

兼容一个交易所查询价差

JiahengHe 8 달 전
부모
커밋
ee47afb30f

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@
 basic/target
 logs
 price_collection/target/
+hot_collection/target/

+ 0 - 1
price_collection/src/main/java/com/liangjiang/price_collection/controller/ApiController.java

@@ -28,7 +28,6 @@ public class ApiController {
 
     @PostMapping("/add")
     public void saveInfo(@RequestBody List<PriceInfoDto> priceInfoDtos){
-        log.info("开始添加 size: {}", priceInfoDtos.size());
         // 开启持久化
         myJmsTemplate.setDeliveryMode(2);
         myJmsTemplate.setExplicitQosEnabled(true);

+ 3 - 0
price_collection/src/main/java/com/liangjiang/price_collection/service/impl/QueryService.java

@@ -7,6 +7,7 @@ import com.liangjiang.price_collection.dto.PriceListDto;
 import com.liangjiang.price_collection.service.ITableService;
 import com.liangjiang.price_collection.utils.DataAssembleTask;
 import com.liangjiang.price_collection.utils.TableQueryTask;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -16,6 +17,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.stream.Collectors;
 
+@Slf4j
 @Service
 public class QueryService {
     private final ExecutorService executorService;
@@ -44,6 +46,7 @@ public class QueryService {
             }
         }
         if(CollectionUtil.isEmpty(results)){
+            log.warn("没有查询到数据");
             return PriceListDto.builder().time(new HashSet<>()).values(new LinkedList<>()).build();
         }
         List<Future<ExchangeCoinPriceArrDto>> resultFutures = new LinkedList<>();

+ 18 - 0
price_collection/src/main/java/com/liangjiang/price_collection/service/impl/RedisServiceImpl.java

@@ -50,6 +50,24 @@ public class RedisServiceImpl {
             log.error("保存价格数据到Redis失败", e);
         }
     }
+
+    /**
+     * 获取某个交易所支持的币对
+     * @param exchange 交易所名称
+     * @return
+     */
+    public Set<String> getCoins(String exchange) {
+        // 获取第一个交易所的币对
+        Set<Object> firstExchangeCoins = redisTemplate.opsForSet().members(EXCHANGE_COIN_KEY + ":" + exchange.toUpperCase());
+        if (firstExchangeCoins == null || firstExchangeCoins.isEmpty()) {
+            return new HashSet<String>();
+        }
+
+        // 转换为字符串集合
+        return firstExchangeCoins.stream()
+                .map(Object::toString)
+                .collect(Collectors.toSet());
+    }
     
     /**
      * 获取两个交易所共有的币对

+ 8 - 4
price_collection/src/main/java/com/liangjiang/price_collection/service/impl/TableServiceImpl.java

@@ -27,12 +27,16 @@ public class TableServiceImpl implements ITableService {
     
     @Override
     public Set<String> getCoinSelect(String[] exchanges) {
-        if (exchanges == null || exchanges.length < 2) {
+        if (exchanges == null || exchanges.length == 0) {
             return new HashSet<>();
         }
-        
         // 使用Redis服务获取共有币对
-        return redisService.getCommonCoins(exchanges);
+        if (exchanges.length == 1){
+            return redisService.getCoins(exchanges[0]);
+        } else {
+            // 使用Redis服务获取共有币对
+            return redisService.getCommonCoins(exchanges);
+        }
     }
 
     @Override
@@ -69,7 +73,7 @@ public class TableServiceImpl implements ITableService {
     @Override
     public List<PriceDto> getPriceInfo(String tableName, Integer startTime, Integer endTime){
         // 从表名解析币对和交易所
-        String[] parts = tableName.split("_");
+        String[] parts = tableName.split("=");
         if (parts.length < 2) {
             return new ArrayList<>();
         }

+ 1 - 1
price_collection/src/main/java/com/liangjiang/price_collection/utils/TableQueryTask.java

@@ -28,7 +28,7 @@ public class TableQueryTask implements Callable<ExchangeCoinPriceDto> {
     @Override
     public ExchangeCoinPriceDto call() {
         try {
-            String tableName = String.format("%s_%s", this.coin.toUpperCase(), this.exchange.toUpperCase());
+            String tableName = String.format("%s=%s", this.coin.toUpperCase(), this.exchange.toUpperCase());
             List<PriceDto> price = tableService.getPriceInfo(tableName, this.startTime, this.endTime);
             // 获取最近价格 增量
             if(isAdd && CollectionUtil.isEmpty(price)){

+ 2 - 2
price_collection/src/main/resources/application.yml

@@ -39,11 +39,11 @@ spring:
     # Redis服务器地址
     host: localhost
     # Redis数据库索引(默认为0)
-    database: 1
+    database: 4
     # Redis服务器连接端口
     port: 6379
     # Redis服务器连接密码(默认为空)
-    password: 43626546
+#    password: 43626546
     # 连接超时时间
     timeout: 1000ms
     lettuce: