Commit 0f9842ab authored by 李欣峰's avatar 李欣峰

<dev>

1.GetWay代码规范(2021/10/09)
parent d5775c3f
package com.zorkdata.apiservice.gateway.schedule;
import com.google.gson.Gson;
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket4j;
import io.github.bucket4j.Refill;
import io.github.bucket4j.local.LocalBucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
......@@ -14,6 +14,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.time.Duration;
......@@ -29,6 +30,9 @@ public class RefreshApiLimitSchedule {
@Autowired
private RestTemplate restTemplate;
@Value("${api-service-address.url}")
private String url;
/**
* 添加定时任务
......@@ -39,26 +43,26 @@ public class RefreshApiLimitSchedule {
private void refreshTask() {
String url = "http://127.0.0.1:6725/v1/dataService/api/getApiLimit";
String queryResult = restTemplate.getForObject(url, String.class);
try {
JSONObject jo = new JSONObject(queryResult);
JSONArray data = jo.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject o = (JSONObject)data.get(i);
String name = o.get("name").toString();
Integer single_limit = Integer.parseInt(o.get("single_limit").toString());
int capacity = single_limit;//桶的最大容量,即能装载 Token 的最大数量
int refillTokens = single_limit; //每次 Token 补充量
Duration duration = Duration.ofSeconds(1); //补充 Token 的时间间隔
JSONObject o = (JSONObject) data.get(i);
String name = o.get("name").toString();
Integer singleLimit = Integer.parseInt(o.get("single_limit").toString());
//桶的最大容量,即能装载 Token 的最大数量
int capacity = singleLimit;
//每次 Token 补充量
int refillTokens = singleLimit;
//补充 Token 的时间间隔
Duration duration = Duration.ofSeconds(1);
Refill refill = Refill.greedy(refillTokens, duration);
Bandwidth limit = Bandwidth.classic(capacity, refill);
LocalBucket bucket = Bucket4j.builder().addLimit(limit).build();
ApiLimitCache.BUCKET_CACHE.put(name,bucket);
ApiLimitCache.BUCKET_CACHE.put(name, bucket);
}
......@@ -67,10 +71,5 @@ public class RefreshApiLimitSchedule {
}
}
}
......@@ -6,7 +6,8 @@ spring:
name: @artifactId@
api-service-address:
path: /v1/apiService/**
uri: http://localhost:6725
uri: http://192.168.70.46:6725
url: http://192.168.70.46:6725/v1/dataService/api/getApiLimit
......
......@@ -171,6 +171,10 @@ public interface ApiRepository extends EntityRepository<Api, Integer> {
Integer findLastCall24ByApiCall(Integer apiId, Date startTime, Date endTime);
/**
* 查询api name & limit
* @return
*/
@Query(value = "select `name`,single_limit from api where status = 1",nativeQuery = true)
List<Map<String,Integer>> findLimitAndName();
}
......@@ -33,9 +33,9 @@
<!-- 发布的输出格式 -->
<pkg.format>tar.gz</pkg.format>
<!-- 最终出包将以APP_ID作为标识命名 -->
<pkg.app.id>onlyone-gen-domain</pkg.app.id>
<pkg.app.id>api-service</pkg.app.id>
<!-- 此处修改为你的SpringBoot 启动类,也可直接用默认的 -->
<mainClass>com.onlyone.gen.domain.WebApplication</mainClass>
<mainClass>com.zorkdata.apiservice.WebApplication</mainClass>
</properties>
<dependencies>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment