|
@ -7,20 +7,31 @@ import com.yihu.hos.core.http.HTTPResponse;
|
|
|
import com.yihu.hos.core.http.HttpClientKit;
|
|
|
import org.apache.camel.Exchange;
|
|
|
import org.apache.camel.Processor;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.client.ServiceInstance;
|
|
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
@Component
|
|
|
public class GatewayProcessor implements Processor {
|
|
|
static Map<String, AppApi> apiMap = new HashMap<>();
|
|
|
static final String agUrl = "http://192.168.1.221:10000";
|
|
|
@Autowired
|
|
|
ParamVerifyBean paramSign;
|
|
|
|
|
|
@Autowired
|
|
|
private DiscoveryClient discoveryClient;
|
|
|
|
|
|
public void process(Exchange exchange) throws Exception {
|
|
|
Map<String, Object> headers = exchange.getIn().getHeaders();
|
|
@ -37,7 +48,7 @@ public class GatewayProcessor implements Processor {
|
|
|
public String route(Exchange exchange) throws IOException, ParseException {
|
|
|
// body = URLDecoder.decode(body, "UTF-8");
|
|
|
Map<String, Object> params = exchange.getIn().getHeaders();
|
|
|
if (params.get("appKey")==null) {
|
|
|
if (params.get("appKey") == null) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/paramError"; //TODO:
|
|
|
}
|
|
|
String appKey = params.get("appKey").toString();
|
|
@ -108,7 +119,6 @@ public class GatewayProcessor implements Processor {
|
|
|
try {
|
|
|
String sign = params.get("sign").toString(); // 簽名
|
|
|
|
|
|
ParamVerifyBean paramSign = new ParamVerifyBean();
|
|
|
paramSign.addParam(params);
|
|
|
// paramSign.genParam();
|
|
|
//TODO 获取app secret传入验证
|
|
@ -172,7 +182,11 @@ public class GatewayProcessor implements Processor {
|
|
|
methodMap.put("3", "put");
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readValue(param, JsonNode.class);
|
|
|
String url = appApi.getMicroServiceUri().split(",")[0];
|
|
|
String url = serviceUrl(appApi.getMicroServiceName());
|
|
|
if (StringUtil.isEmpty(url)) {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
final String[] endPoint = {"restlet:" + url + appApi.getMsMethodName() + "?socketTimeout=60000&connectionTimeout=60000&restletMethod=" + methodMap.get(appApi.getMethod())};
|
|
|
final String[] body = {""};
|
|
|
appApi.getParameters().forEach(p -> {
|
|
@ -186,7 +200,7 @@ public class GatewayProcessor implements Processor {
|
|
|
endPoint[0] = endPoint[0].replaceAll("\\{[^}]*\\}", value);
|
|
|
} else if (p.getType().equals("2")) { //head param
|
|
|
exchange.getOut().setHeader(p.getName(), value);
|
|
|
}else {
|
|
|
} else {
|
|
|
body[0] += ("&" + p.getName() + "=" + value);
|
|
|
}
|
|
|
|
|
@ -254,4 +268,20 @@ public class GatewayProcessor implements Processor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过服务名获取微服务地址
|
|
|
*
|
|
|
* @param serviceName 微服务名
|
|
|
* @return
|
|
|
*/
|
|
|
public String serviceUrl(String serviceName) {
|
|
|
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
|
|
if (instances != null && !instances.isEmpty()) {
|
|
|
ServiceInstance serviceInstance = instances.get(0);
|
|
|
return serviceInstance.getUri().toString();
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|