use of com.z.gateway.common.entity.ApiInterface in project gateway-dubbox by zhuzhong.
the class ZkApiInterfaceServiceImpl method queryApiInterfaceByApiId.
@Override
public ApiInterface queryApiInterfaceByApiId(String apiId, String version) {
List<String> sets = hosts.get(apiId);
if (sets != null) {
String hostAddress = loadBalancerService.chooseOne(apiId, version, sets);
ApiInterface apiInterface = new ApiInterface();
apiInterface.setApiId(apiId);
apiInterface.setProtocol(CommonCodeConstants.HTTP);
apiInterface.setHostAddress(hostAddress);
return apiInterface;
}
return null;
}
use of com.z.gateway.common.entity.ApiInterface in project gateway-dubbox by zhuzhong.
the class OpenApiReqHandler method doInvokeBackService.
/**
* 根据routeBean信息,通过httpclient调用后端信息,然后将返回值构建成string
*
* @param bean
* @return
*/
private String doInvokeBackService(OpenApiRouteBean bean) {
logger.info("step5...");
// 后端服务返回值
String serviceRspData = null;
String operationType = bean.getOperationType();
String requestMethod = bean.getRequestMethod();
if (operationType.equals(CommonCodeConstants.API_SYSERVICE_KEY)) {
} else if (CommonCodeConstants.API_GETDATA_KEY.equals(operationType)) {
} else if (CommonCodeConstants.API_SERVICE_KEY.equals(operationType)) {
logger.info(String.format("{serviceId:%s ,version:%s }", bean.getApiId(), bean.getVersion()));
ApiInterface apiInfo = apiInterfaceService.queryApiInterfaceByApiId(bean.getApiId(), bean.getVersion());
if (apiInfo == null) {
return String.format("this apiId=%s,version=%s has off line,please use another one", bean.getApiId(), bean.getVersion());
}
apiInfo.setTargetUrl(bean.getTargetUrl());
apiInfo.setRequestMethod(bean.getRequestMethod());
if (CommonCodeConstants.REQUEST_METHOD.GET.name().equalsIgnoreCase(requestMethod)) {
// get请求
String url = apiInfo.getUrl();
url = UrlUtil.dealUrl(url, bean.getThdApiUrlParams());
/*
* if (StringUtils.isNotBlank(bean.getQueryString())) { url +=
* "?" + bean.getQueryString(); // 串好url 地址 }
*/
logger.info(String.format("{service url:%s} ", url));
// bean.getReqHeader().get(CONTENT_TYPE_KEY);
if (url.startsWith(CommonCodeConstants.HTTPS)) {
if (bean.getServiceGetReqData() == null) {
serviceRspData = apiHttpClientService.doHttpsGet(url, bean.getTraceId());
} else {
serviceRspData = apiHttpClientService.doHttpsGet(url, bean.getServiceGetReqData(), bean.getTraceId());
}
} else {
if (bean.getServiceGetReqData() == null) {
serviceRspData = apiHttpClientService.doGet(url, bean.getTraceId());
} else {
serviceRspData = apiHttpClientService.doGet(url, bean.getServiceGetReqData(), bean.getTraceId());
}
}
} else if (CommonCodeConstants.REQUEST_METHOD.POST.name().equalsIgnoreCase(requestMethod)) {
// post请求
String url = apiInfo.getUrl();
logger.info(String.format("{service url:%s} ", url));
// 请求的json格式数据参数
String reqData = bean.getServiceReqData();
if (StringUtils.isNotBlank(reqData) && reqData.length() > this.maxReqDataLth) {
reqData = reqData.substring(0, this.maxReqDataLth - 1);
}
logger.info(String.format("{serviceId:%s ,reqData:%s }", bean.getApiId(), reqData));
String contentType = bean.getReqHeader().get(CONTENT_TYPE_KEY);
if (url.startsWith(CommonCodeConstants.HTTPS)) {
serviceRspData = apiHttpClientService.doHttpsPost(url, bean.getServiceReqData(), contentType, bean.getTraceId());
} else {
serviceRspData = apiHttpClientService.doPost(url, bean.getServiceReqData(), contentType, bean.getTraceId());
}
if ("timeout".equals(serviceRspData)) {
logger.error("invoke service: response is null!");
throw new OpenApiException(OauthErrorEnum.ERROR.getErrCode(), OauthErrorEnum.ERROR.getErrMsg());
}
}
} else {
}
return serviceRspData;
}
Aggregations