腾讯并发量不超过20 进行控制
This commit is contained in:
parent
3c47a0b3f3
commit
02db2636b6
|
@ -9,6 +9,7 @@ import com.face.global.controller.ManageBaseController;
|
||||||
import com.face.global.vo.ApiResult;
|
import com.face.global.vo.ApiResult;
|
||||||
import com.face.global.vo.PageResponseVO;
|
import com.face.global.vo.PageResponseVO;
|
||||||
import com.face.util.ApiResultBuilder;
|
import com.face.util.ApiResultBuilder;
|
||||||
|
import com.face.util.SystemExceptionUtils;
|
||||||
import com.tencentcloudapi.common.AbstractModel;
|
import com.tencentcloudapi.common.AbstractModel;
|
||||||
import com.tencentcloudapi.common.Credential;
|
import com.tencentcloudapi.common.Credential;
|
||||||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||||
|
@ -40,6 +41,7 @@ import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/face/attachment")
|
@RequestMapping("/face/attachment")
|
||||||
|
@ -47,6 +49,9 @@ import java.util.List;
|
||||||
@Tag(name = "人脸融合基础管理")
|
@Tag(name = "人脸融合基础管理")
|
||||||
public class FaceInfoManagerController extends ManageBaseController {
|
public class FaceInfoManagerController extends ManageBaseController {
|
||||||
|
|
||||||
|
//定义信号资源包的总数 只有2个
|
||||||
|
Semaphore semaphore=new Semaphore(20);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IFaceInfoService faceInfoService;
|
private IFaceInfoService faceInfoService;
|
||||||
|
|
||||||
|
@ -118,6 +123,12 @@ public class FaceInfoManagerController extends ManageBaseController {
|
||||||
String imgUrl="";
|
String imgUrl="";
|
||||||
FuseFaceResponse resp=null;
|
FuseFaceResponse resp=null;
|
||||||
|
|
||||||
|
//设置这个接口可用的资源数
|
||||||
|
int availablePermits=semaphore.availablePermits();
|
||||||
|
if(availablePermits<=0){
|
||||||
|
throw SystemExceptionUtils.buildBusinessException("当前请求已经过载,请稍后再尝试!");
|
||||||
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
||||||
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
||||||
|
@ -133,14 +144,9 @@ public class FaceInfoManagerController extends ManageBaseController {
|
||||||
FacefusionClient client = new FacefusionClient(cred, "ap-chengdu", clientProfile);
|
FacefusionClient client = new FacefusionClient(cred, "ap-chengdu", clientProfile);
|
||||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||||
FuseFaceRequest req = new FuseFaceRequest();
|
FuseFaceRequest req = new FuseFaceRequest();
|
||||||
|
|
||||||
|
|
||||||
//活动ID
|
//活动ID
|
||||||
req.setProjectId(activityId);
|
req.setProjectId(activityId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Path imageFile = Paths.get("F:\\体验腾讯人脸融合-素材\\222.jpg");
|
// Path imageFile = Paths.get("F:\\体验腾讯人脸融合-素材\\222.jpg");
|
||||||
byte[] imageBytes = file.getBytes();
|
byte[] imageBytes = file.getBytes();
|
||||||
|
|
||||||
|
@ -186,8 +192,8 @@ public class FaceInfoManagerController extends ManageBaseController {
|
||||||
req.setMergeInfos(mergeInfos);
|
req.setMergeInfos(mergeInfos);
|
||||||
|
|
||||||
req.setLogoAdd(0L);
|
req.setLogoAdd(0L);
|
||||||
|
//请求占用一个资源
|
||||||
|
semaphore.acquire(1);
|
||||||
// 返回的resp是一个FuseFaceResponse的实例,与请求对象对应
|
// 返回的resp是一个FuseFaceResponse的实例,与请求对象对应
|
||||||
resp = client.FuseFace(req);
|
resp = client.FuseFace(req);
|
||||||
// 输出json格式的字符串回包
|
// 输出json格式的字符串回包
|
||||||
|
@ -197,6 +203,8 @@ public class FaceInfoManagerController extends ManageBaseController {
|
||||||
System.out.println(e.toString());
|
System.out.println(e.toString());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
semaphore.release(1);//释放一个资源
|
||||||
}
|
}
|
||||||
|
|
||||||
return ApiResultBuilder.getCommonBuilder(FuseFaceResponse.class).success(resp).build();
|
return ApiResultBuilder.getCommonBuilder(FuseFaceResponse.class).success(resp).build();
|
||||||
|
|
Loading…
Reference in New Issue