Commit d5775c3f authored by 李欣峰's avatar 李欣峰

<dev>

1.代码同步(2021/10/09)
parent be6d1b21
package com.zorkdata.apiservice.gateway.config; package com.zorkdata.apiservice.gateway.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.validation.Valid;
/** /**
* @author zyw * @author zyw
* @create 2021-10-08-17:57 * @create 2021-10-08-17:57
...@@ -13,11 +16,15 @@ import org.springframework.web.client.RestTemplate; ...@@ -13,11 +16,15 @@ import org.springframework.web.client.RestTemplate;
@Configuration @Configuration
public class GateWayConfig { public class GateWayConfig {
@Value("${api-service-address.uri}")
private String uri;
@Value("${api-service-address.path}")
private String path;
@Bean @Bean
public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder){ public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("api-router", r->r.path("/apiservice/**") routes.route("api-router", r->r.path(path).uri(uri)).build();
.uri("http://localhost:9086")).build();
return routes.build(); return routes.build();
} }
......
package com.zorkdata.apiservice.gateway.schedule; package com.zorkdata.apiservice.gateway.schedule;
import com.google.gson.Gson;
import io.github.bucket4j.Bandwidth; import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket4j; import io.github.bucket4j.Bucket4j;
import io.github.bucket4j.Refill; import io.github.bucket4j.Refill;
import io.github.bucket4j.local.LocalBucket;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -24,6 +29,7 @@ public class RefreshApiLimitSchedule { ...@@ -24,6 +29,7 @@ public class RefreshApiLimitSchedule {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
/** /**
* 添加定时任务 * 添加定时任务
* 每小时执行一次 * 每小时执行一次
...@@ -32,13 +38,39 @@ public class RefreshApiLimitSchedule { ...@@ -32,13 +38,39 @@ public class RefreshApiLimitSchedule {
@PostConstruct @PostConstruct
private void refreshTask() { private void refreshTask() {
String url = "http://";
int capacity = 2000;//桶的最大容量,即能装载 Token 的最大数量
int refillTokens = 2000; //每次 Token 补充量 String url = "http://127.0.0.1:6725/v1/dataService/api/getApiLimit";
Duration duration = Duration.ofSeconds(1); //补充 Token 的时间间隔 String queryResult = restTemplate.getForObject(url, String.class);
Refill refill = Refill.greedy(refillTokens, duration); try {
Bandwidth limit = Bandwidth.classic(capacity, refill); JSONObject jo = new JSONObject(queryResult);
ApiLimitCache.BUCKET_CACHE.put("api",Bucket4j.builder().addLimit(limit).build()); 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 的时间间隔
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);
}
} catch (JSONException e) {
e.printStackTrace();
}
} }
} }
...@@ -4,15 +4,9 @@ server: ...@@ -4,15 +4,9 @@ server:
spring: spring:
application: application:
name: @artifactId@ name: @artifactId@
# cloud: api-service-address:
# gateway: path: /v1/apiService/**
# routes: uri: http://localhost:6725
# - id: api-service
# uri: http://localhost:9086
# predicates:
# - Path=/apiservice/**
......
...@@ -133,6 +133,7 @@ public interface ApiApplication { ...@@ -133,6 +133,7 @@ public interface ApiApplication {
/** /**
* 查询我创建的Api * 查询我创建的Api
*
* @param userId * @param userId
* @param groupId * @param groupId
* @param keyword * @param keyword
...@@ -149,6 +150,7 @@ public interface ApiApplication { ...@@ -149,6 +150,7 @@ public interface ApiApplication {
/** /**
* 查询市场api * 查询市场api
*
* @param keyword * @param keyword
* @param pageNo * @param pageNo
* @param pageSize * @param pageSize
...@@ -162,6 +164,7 @@ public interface ApiApplication { ...@@ -162,6 +164,7 @@ public interface ApiApplication {
/** /**
* 查看我授权的Api * 查看我授权的Api
*
* @param username * @param username
* @param pageNo * @param pageNo
* @param pageSize * @param pageSize
...@@ -187,6 +190,7 @@ public interface ApiApplication { ...@@ -187,6 +190,7 @@ public interface ApiApplication {
/** /**
* 接口执行的通用方法 * 接口执行的通用方法
*
* @param apiName * @param apiName
* @param inParam * @param inParam
* @param username * @param username
...@@ -224,4 +228,11 @@ public interface ApiApplication { ...@@ -224,4 +228,11 @@ public interface ApiApplication {
* @return * @return
*/ */
InvokeResult queryAuthSingle(Integer apiId); InvokeResult queryAuthSingle(Integer apiId);
/**
* 获取Api Limit,name属性
*
* @return
*/
InvokeResult getApiLimit();
} }
...@@ -28,6 +28,7 @@ import com.zorkdata.dddlib.core.sdk.InvokeResult; ...@@ -28,6 +28,7 @@ import com.zorkdata.dddlib.core.sdk.InvokeResult;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.checkerframework.common.reflection.qual.Invoke;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -745,6 +746,21 @@ public class ApiServiceApplicationImpl implements ApiApplication { ...@@ -745,6 +746,21 @@ public class ApiServiceApplicationImpl implements ApiApplication {
return invokeResult; return invokeResult;
} }
@Override
public InvokeResult getApiLimit() {
InvokeResult invokeResult;
try {
List<Map<String,Integer>> limitAndName = apiRepository.findLimitAndName();
invokeResult = InvokeResult.success();
invokeResult.setData(limitAndName);
} catch (Exception e) {
log.error("异常了,", e);
invokeResult = InvokeResult.fail();
invokeResult.setMessage("查询失败" + e.getMessage());
}
return invokeResult;
}
@Override @Override
public InvokeResult executeApi(String apiName, Map<String, String> inParam, String username) { public InvokeResult executeApi(String apiName, Map<String, String> inParam, String username) {
//开始时间 //开始时间
......
package com.zorkdata.apiservice.domain.repository; package com.zorkdata.apiservice.domain.repository;
import com.zorkdata.apiservice.domain.domain.Api; import com.zorkdata.apiservice.domain.domain.Api;
import com.zorkdata.apiservice.domain.dto.ApiDTO;
import com.zorkdata.dddlib.domain.EntityRepository; import com.zorkdata.dddlib.domain.EntityRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Date; import java.util.*;
import java.util.List;
/** /**
* @title: ApiServiceRepositoryB * @title: ApiServiceRepositoryB
...@@ -170,4 +170,7 @@ public interface ApiRepository extends EntityRepository<Api, Integer> { ...@@ -170,4 +170,7 @@ public interface ApiRepository extends EntityRepository<Api, Integer> {
@Query(value = "select count(*) from api_call where api_id = ?1 and call_time >= ?2 and call_time <= ?3", nativeQuery = true) @Query(value = "select count(*) from api_call where api_id = ?1 and call_time >= ?2 and call_time <= ?3", nativeQuery = true)
Integer findLastCall24ByApiCall(Integer apiId, Date startTime, Date endTime); Integer findLastCall24ByApiCall(Integer apiId, Date startTime, Date endTime);
@Query(value = "select `name`,single_limit from api where status = 1",nativeQuery = true)
List<Map<String,Integer>> findLimitAndName();
} }
...@@ -32,6 +32,7 @@ public interface ApiFacade { ...@@ -32,6 +32,7 @@ public interface ApiFacade {
/** /**
* 添加接口分组 * 添加接口分组
*
* @param apiGroupDTO * @param apiGroupDTO
* @param username * @param username
* @return * @return
...@@ -69,6 +70,7 @@ public interface ApiFacade { ...@@ -69,6 +70,7 @@ public interface ApiFacade {
/** /**
* 新增Api接口 * 新增Api接口
*
* @param apiDTO * @param apiDTO
* @param username * @param username
* @param userid * @param userid
...@@ -86,6 +88,7 @@ public interface ApiFacade { ...@@ -86,6 +88,7 @@ public interface ApiFacade {
/** /**
* 添加接口授权 * 添加接口授权
*
* @param apiAuthDTO * @param apiAuthDTO
* @param username * @param username
* @return * @return
...@@ -118,6 +121,7 @@ public interface ApiFacade { ...@@ -118,6 +121,7 @@ public interface ApiFacade {
/** /**
* 生成注册Api * 生成注册Api
*
* @param apiDTO * @param apiDTO
* @param username * @param username
* @param userid * @param userid
...@@ -128,6 +132,7 @@ public interface ApiFacade { ...@@ -128,6 +132,7 @@ public interface ApiFacade {
/** /**
* 查询我创建的Api * 查询我创建的Api
*
* @param userId * @param userId
* @param groupId * @param groupId
* @param keyword * @param keyword
...@@ -143,6 +148,7 @@ public interface ApiFacade { ...@@ -143,6 +148,7 @@ public interface ApiFacade {
/** /**
* 查看API市场 * 查看API市场
*
* @param keyword * @param keyword
* @param pageNo * @param pageNo
* @param pageSize * @param pageSize
...@@ -156,6 +162,7 @@ public interface ApiFacade { ...@@ -156,6 +162,7 @@ public interface ApiFacade {
/** /**
* 查看我授权的api * 查看我授权的api
*
* @param username * @param username
* @param pageNo * @param pageNo
* @param pageSize * @param pageSize
...@@ -171,6 +178,7 @@ public interface ApiFacade { ...@@ -171,6 +178,7 @@ public interface ApiFacade {
/** /**
* 数据预览 * 数据预览
*
* @param userId * @param userId
* @param apiId * @param apiId
* @param username * @param username
...@@ -181,12 +189,13 @@ public interface ApiFacade { ...@@ -181,12 +189,13 @@ public interface ApiFacade {
/** /**
* ll * ll
*
* @param apiName * @param apiName
* @param inParam * @param inParam
* @param username * @param username
* @return * @return
*/ */
InvokeResult<Void> executeApi(String apiName, Map<String, String> inParam,String username); InvokeResult<Void> executeApi(String apiName, Map<String, String> inParam, String username);
/** /**
* 获取Mysql数据类型 * 获取Mysql数据类型
...@@ -197,6 +206,7 @@ public interface ApiFacade { ...@@ -197,6 +206,7 @@ public interface ApiFacade {
/** /**
* 获取SqlServer数据类型 * 获取SqlServer数据类型
*
* @return * @return
*/ */
List<String> getSqlServerDataType(); List<String> getSqlServerDataType();
...@@ -216,4 +226,13 @@ public interface ApiFacade { ...@@ -216,4 +226,13 @@ public interface ApiFacade {
* @return * @return
*/ */
InvokeResult queryAuthSingle(Integer apiId); InvokeResult queryAuthSingle(Integer apiId);
/**
* 获取Api Limit,name属性
*
* @return
*/
InvokeResult getApiLimit();
} }
...@@ -258,4 +258,9 @@ public class ApiServicueFacadeImpl implements ApiFacade { ...@@ -258,4 +258,9 @@ public class ApiServicueFacadeImpl implements ApiFacade {
return apiApplication.queryAuthSingle(apiId); return apiApplication.queryAuthSingle(apiId);
} }
@Override
public InvokeResult getApiLimit() {
return apiApplication.getApiLimit();
}
} }
...@@ -258,6 +258,13 @@ public class ApiServicueController extends BaseController { ...@@ -258,6 +258,13 @@ public class ApiServicueController extends BaseController {
return InvokeResult.success(dataTypes, "获取SqlServer数据类型成功!"); return InvokeResult.success(dataTypes, "获取SqlServer数据类型成功!");
} }
@Inner(value = false)
@GetMapping("/getApiLimit")
public InvokeResult getApiLimit() {
return apiFacade.getApiLimit();
}
@Inner(value = false) @Inner(value = false)
@PostMapping("/executeApi") @PostMapping("/executeApi")
public InvokeResult executeApi(@RequestParam String apiName, public InvokeResult executeApi(@RequestParam String apiName,
...@@ -268,6 +275,24 @@ public class ApiServicueController extends BaseController { ...@@ -268,6 +275,24 @@ public class ApiServicueController extends BaseController {
} }
@Inner(value = false) @Inner(value = false)
@PutMapping("/csPut/{id}") @PutMapping("/csPut/{id}")
public InvokeResult csPut(@RequestParam String name, public InvokeResult csPut(@RequestParam String name,
......
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