1
zj
2026-01-28 eb1a2bad998331a3f61664d41423c42d172e4d8f
1
2 files modified
76 ■■■■■ changed files
.idea/jarRepositories.xml 20 ●●●● patch | view | raw | blame | history
src/main/java/com/nq/utils/redis/RedisKeyUtil.java 56 ●●●● patch | view | raw | blame | history
.idea/jarRepositories.xml
@@ -2,14 +2,14 @@
<project version="4">
  <component name="RemoteRepositoriesConfiguration">
    <remote-repository>
      <option name="id" value="nexus-aliyun" />
      <option name="name" value="nexus-aliyun" />
      <option name="url" value="https://maven.aliyun.com/repository/public" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="central" />
      <option name="name" value="Central Repository" />
      <option name="url" value="https://repo.maven.apache.org/maven2" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="nexus-aliyun" />
      <option name="name" value="nexus-aliyun" />
      <option name="url" value="https://maven.aliyun.com/repository/public" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="central" />
@@ -27,14 +27,14 @@
      <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="central" />
      <option name="name" value="Central Repository" />
      <option name="url" value="https://maven.aliyun.com/repository/public" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="nexus-aliyun" />
      <option name="name" value="nexus-aliyun" />
      <option name="url" value="https://maven.aliyun.com/nexus/content/groups/public/" />
    </remote-repository>
    <remote-repository>
      <option name="id" value="central" />
      <option name="name" value="Central Repository" />
      <option name="url" value="https://maven.aliyun.com/repository/public" />
    </remote-repository>
  </component>
</project>
src/main/java/com/nq/utils/redis/RedisKeyUtil.java
@@ -15,6 +15,7 @@
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
@@ -90,44 +91,69 @@
    }
    public static String doPost(String pid) {
        // 从配置中获取 API URL,并拼接 key
        String apiUrl = PropertiesUtil.getProperty("JP_HTTP_API") + "stock?key=" + PropertiesUtil.getProperty("JP_KEY");
        EStockType eStockType = EStockType.JP;
        String apiUrl = eStockType.getStockUrl() + "stock?key=" + eStockType.getStockKey();
        String result = null;
        // 创建Redis限流的key
        String redisKey = "limit:post:" + EStockType.JP.getCode() + ":" + pid;
        try {
            // 尝试从Redis获取,如果存在则直接返回
            String cachedResult = RedisShardedPoolUtils.get(redisKey);
            if (cachedResult != null && !cachedResult.isEmpty() && !"ERROR".equals(cachedResult)) {
                return cachedResult;
            }
            // 实际执行HTTP请求
            result = executeHttpPost(apiUrl, pid);
            // 将结果存入Redis
            if (result != null && !result.isEmpty()) {
                RedisShardedPoolUtils.set(redisKey, result);
                RedisShardedPoolUtils.expire(redisKey, 1); // 设置1秒过期
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 异常处理
            try {
                RedisShardedPoolUtils.set(redisKey, "ERROR");
                RedisShardedPoolUtils.expire(redisKey, 1);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
    // HTTP请求执行方法
    private static String executeHttpPost(String apiUrl, String pid) throws IOException {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true); // 允许向连接输出
        connection.setDoOutput(true);
            // 构建 POST 数据
            String postData = "pid=" + pid;
            // 发送 POST 请求
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = postData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }
            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
        StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            result = response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
        return response.toString();
    }
    public  static  void setCacheCompanies(Stock stock,String companiesInfo){
        RedisShardedPoolUtils.set(RedisKeyConstant.RK_COMPANY_INFO+":"+stock.getStockType()+":"+stock.getStockCode(),