以下使用 Spring Boot 开发的示例代码,用于获取指定币种的 K 线数据,并控制请求限速:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class KLineDataCollectorApplication {

    private static final String API_URL = 'https://api.example.com/api/v5/market/index-candles';

    private static final String API_KEY = 'your-api-key';

    private static final int REQUEST_LIMIT = 20; // 请求限速(次数)
    private static final int REQUEST_INTERVAL = 2000; // 请求间隔(毫秒)

    private final RestTemplate restTemplate = new RestTemplate();

    public static void main(String[] args) {
        SpringApplication.run(KLineDataCollectorApplication.class, args);
    }

    @GetMapping("/kline/{symbol}/{interval}")
    public String getKLineData(@PathVariable String symbol, @PathVariable String interval) throws InterruptedException {
        // 控制请求限速
        Thread.sleep(REQUEST_INTERVAL);

        // 构建请求 URL
        String url = API_URL + '?symbol=' + symbol + '&interval=' + interval;

        // 发送请求并返回结果
        return restTemplate.getForObject(url, String.class);
    }
}

请注意替换API_URLAPI_KEY为您自己的实际值。此示例代码中,我们定义了一个getKLineData接口,用于获取指定币种和时间间隔的 K 线数据。在每次请求之前,我们使用Thread.sleep方法来控制请求的频率,确保在请求限速内。

您可以使用定时器来循环调用getKLineData接口获取不同币种和时间间隔的 K 线数据。

请注意遵守 API 提供商的限速规则,并根据需要进行必要的调整。

SpringBoot 获取指数 K 线数据并控制限速

原文地址: https://www.cveoy.top/t/topic/o9ar 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录