Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
api-service-gateway
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
朱允伟
api-service-gateway
Commits
0f9842ab
Commit
0f9842ab
authored
Oct 09, 2021
by
李欣峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
<dev>
1.GetWay代码规范(2021/10/09)
parent
d5775c3f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
20 deletions
+24
-20
api-service-gateway/src/main/java/com/zorkdata/apiservice/gateway/schedule/RefreshApiLimitSchedule.java
.../apiservice/gateway/schedule/RefreshApiLimitSchedule.java
+16
-17
api-service-gateway/src/main/resources/bootstrap.yml
api-service-gateway/src/main/resources/bootstrap.yml
+2
-1
api-service-project/api-service-domain/src/main/java/com/zorkdata/apiservice/domain/repository/ApiRepository.java
.../zorkdata/apiservice/domain/repository/ApiRepository.java
+4
-0
api-service-project/api-service-web/pom.xml
api-service-project/api-service-web/pom.xml
+2
-2
No files found.
api-service-gateway/src/main/java/com/zorkdata/apiservice/gateway/schedule/RefreshApiLimitSchedule.java
View file @
0f9842ab
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 {
}
}
}
api-service-gateway/src/main/resources/bootstrap.yml
View file @
0f9842ab
...
...
@@ -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
...
...
api-service-project/api-service-domain/src/main/java/com/zorkdata/apiservice/domain/repository/ApiRepository.java
View file @
0f9842ab
...
...
@@ -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
();
}
api-service-project/api-service-web/pom.xml
View file @
0f9842ab
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment