优化配置,Face++宕机、可以给出友情提示

This commit is contained in:
limqhz
2020-05-10 02:04:58 +08:00
parent c69c799f4a
commit ed6d251ff3
4 changed files with 64 additions and 38 deletions

View File

@@ -12,6 +12,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
@@ -55,18 +56,24 @@ public class FaceService {
/**
* face++登录
*/
public boolean login() throws IOException {
public boolean login() {
JsonObject param = new JsonObject();
param.addProperty("username", account);
param.addProperty("password", pwd);
CloseableHttpResponse response = post(param, "auth/login");
if (response.getStatusLine().getStatusCode() == 200) {
LoginResponse loginResponse = JsonMapper.nonNullMapper().fromJson(response.getEntity().getContent(), LoginResponse.class);
logger.info("登录成功" + ToStringBuilder.reflectionToString(loginResponse));
return true;
} else {
logger.error("face++登陆失败!");
throw new FaceServiceException("登录失败");
CloseableHttpResponse response = null;
try {
response = post(param, "auth/login");
if (response.getStatusLine().getStatusCode() == 200) {
LoginResponse loginResponse = JsonMapper.nonNullMapper().fromJson(response.getEntity().getContent(), LoginResponse.class);
logger.info("登录成功" + ToStringBuilder.reflectionToString(loginResponse));
return true;
} else {
logger.error("face++登陆失败!");
throw new FaceServiceException("无法连接人脸服务器");
}
} catch (IOException e) {
logger.error("login无法连接人脸服务器");
throw new FaceServiceException("无法连接人脸服务器");
}
}
@@ -77,7 +84,7 @@ public class FaceService {
* @param member
* @return
*/
public Integer addSubject(Integer faceId, Member member) throws IOException {
public Integer addSubject(Integer faceId, Member member) throws UnsupportedEncodingException {
JsonObject param = new JsonObject();
param.addProperty("gender", 1);
String nickname = member.getNickname();
@@ -92,18 +99,23 @@ public class FaceService {
JsonArray faceIds = new JsonArray();
faceIds.add(faceId);
param.add("photo_ids", faceIds);
CloseableHttpResponse response = post(param, "subject");
if (response.getStatusLine().getStatusCode() == 200) {
AddMemberResponse addMemberResponse = JsonMapper.nonNullMapper().fromJson(response.getEntity().getContent(), AddMemberResponse.class);
if (addMemberResponse.getCode() == 0) {
Integer subjectId = addMemberResponse.getData().getId();
return subjectId;
try {
CloseableHttpResponse response = post(param, "subject");
if (response.getStatusLine().getStatusCode() == 200) {
AddMemberResponse addMemberResponse = JsonMapper.nonNullMapper().fromJson(response.getEntity().getContent(), AddMemberResponse.class);
if (addMemberResponse.getCode() == 0) {
Integer subjectId = addMemberResponse.getData().getId();
return subjectId;
} else {
throw new FaceServiceException(addMemberResponse.getDesc());
}
} else {
throw new FaceServiceException(addMemberResponse.getDesc());
logger.error("添加用户失败 code:" + response.getStatusLine().getStatusCode());
throw new FaceServiceException("添加用户失败");
}
} else {
logger.error("添加用户失败 code:" + response.getStatusLine().getStatusCode());
throw new FaceServiceException("添加用户失败");
} catch (IOException e) {
logger.error("addSubject无法连接人脸服务器");
throw new FaceServiceException("无法连接人脸服务器");
}
}
@@ -122,6 +134,7 @@ public class FaceService {
try {
CloseableHttpResponse response = httpClient.execute(req);
} catch (IOException e) {
logger.error("show发生异常",e);
e.printStackTrace();
}
}
@@ -183,9 +196,15 @@ public class FaceService {
HttpPost httpPost = new HttpPost("http://" + faceUrl + "/" + uri);
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("user-agent", "Koala Admin");
if (param != null)
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(7000)
.setSocketTimeout(7000).setConnectTimeout(1000).build();
httpPost.setConfig(requestConfig);
if (param != null){
httpPost.setEntity(new StringEntity(param.toString()));
}
CloseableHttpResponse response = httpClient.execute(httpPost);
logger.info(uri+"返回编码"+response.getStatusLine().getStatusCode());
logger.info(uri+"返回结果"+response.toString());
return response;
}