瀏覽代碼

Merge branch 'master' of luofaqiang/esb into master

luofaqiang 8 年之前
父節點
當前提交
0917d403b5

+ 5 - 0
hos-camel/src/main/java/gateway/README.md

@ -0,0 +1,5 @@
# 说明
Gateway是智慧医疗云的统一网关。所有外部访问的请求都需要经过统一网关进行访问。
# 配置说明
信息共享平台使用动态加载路由的方式,所以在路由的配置信息可以直接书写在代码中,
为了统一修改参数会定义出来一个统一的配置文件。

+ 0 - 36
hos-camel/src/main/java/gateway/processor/ApiInfo.java

@ -1,36 +0,0 @@
package gateway.processor;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
/**
 * @author Airhead
 * @since 2017/3/15.
 */
public class ApiInfo {
    String apiInfo;
    String gatewayApi;
    String methodName;
    String method;
    String host;
    List<Param> paramList;
    public ApiInfo(String apiInfo) {
        this.apiInfo = apiInfo;
        this.parse();
    }
    private void parse() {
        ObjectMapper objectMapper = new ObjectMapper();
    }
    class Param {
        String name;
        String type;
        String dataType;
    }
}

+ 190 - 0
hos-camel/src/main/java/gateway/processor/AppApi.java

@ -0,0 +1,190 @@
package gateway.processor;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Collection;
/**
 * @author Airhead
 * @since 2017/3/15.
 */
public class AppApi {
    private String name;
    private String type;
    private String method;
    private String protocol;
    private String version;
    private String activityType;
    private String openLevel;
    private String auditLevel;
    private String methodName;
    private String microServiceUri;
    private String msMethodName;
    private String microServiceName;
    private Collection<Param> parameters;
    public AppApi(String apiInfo) throws IOException {
        this.parse(apiInfo);
    }
    public static AppApi parse(String apiInfo) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(apiInfo, AppApi.class);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getMethod() {
        return method;
    }
    public void setMethod(String method) {
        this.method = method;
    }
    public String getProtocol() {
        return protocol;
    }
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public String getActivityType() {
        return activityType;
    }
    public void setActivityType(String activityType) {
        this.activityType = activityType;
    }
    public String getOpenLevel() {
        return openLevel;
    }
    public void setOpenLevel(String openLevel) {
        this.openLevel = openLevel;
    }
    public String getAuditLevel() {
        return auditLevel;
    }
    public void setAuditLevel(String auditLevel) {
        this.auditLevel = auditLevel;
    }
    public String getMethodName() {
        return methodName;
    }
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
    public String getMicroServiceUri() {
        return microServiceUri;
    }
    public void setMicroServiceUri(String microServiceUri) {
        this.microServiceUri = microServiceUri;
    }
    public String getMsMethodName() {
        return msMethodName;
    }
    public void setMsMethodName(String msMethodName) {
        this.msMethodName = msMethodName;
    }
    public String getMicroServiceName() {
        return microServiceName;
    }
    public void setMicroServiceName(String microServiceName) {
        this.microServiceName = microServiceName;
    }
    public Collection<Param> getParameters() {
        return parameters;
    }
    public void setParameters(Collection<Param> parameters) {
        this.parameters = parameters;
    }
    static class Param {
        private String name;
        private String type;
        private String dataType;
        private String required;
        private String defaultValue;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getDataType() {
            return dataType;
        }
        public void setDataType(String dataType) {
            this.dataType = dataType;
        }
        public String getRequired() {
            return required;
        }
        public void setRequired(String required) {
            this.required = required;
        }
        public String getDefaultValue() {
            return defaultValue;
        }
        public void setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }
    }
}

+ 23 - 19
hos-camel/src/main/java/gateway/processor/GatewayProcessor.java

@ -20,7 +20,7 @@ import java.util.stream.Stream;
public class GatewayProcessor implements Processor {
    static Map<String, ApiInfo> apiMap = new HashMap<>();
    static Map<String, AppApi> apiMap = new HashMap<>();
    public void process(Exchange exchange) throws Exception {
    }
@ -136,24 +136,24 @@ public class GatewayProcessor implements Processor {
        String api = params.get("api").toString();                     // API接口名称
        String param = params.get("param").toString();
        ApiInfo apiInfo = getApiInfo(api);
        AppApi appApi = getApiInfo(api);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (apiInfo == null) {
            if (appApi == null) {
                return "";
            }
            JsonNode jsonNode = objectMapper.readValue(param, JsonNode.class);
            String host = apiInfo.host.split(",")[0];
            String endPoint = "restlet:http://" + host + apiInfo.methodName + "restletMethods=" + apiInfo.method;
            String host = appApi.getMicroServiceUri().split(",")[0];
            String endPoint = "restlet:http://" + host + appApi.getMethodName() + "restletMethods=" + appApi.getMethod();
            final String[] body = {""};
            apiInfo.paramList.forEach(p -> {
                String value = jsonNode.asText(p.name);
                if (p.type.equals("2")) { //path param
                    endPoint.replace("{" + p.name + "}", value);
            appApi.getParameters().forEach(p -> {
                String value = jsonNode.asText(p.getName());
                if (p.getType().equals("2")) { //path param
                    endPoint.replace("{" + p.getName() + "}", value);
                }
                body[0] += ("&" + p.name + "=" + value);
                body[0] += ("&" + p.getName() + "=" + value);
            });
@ -166,23 +166,27 @@ public class GatewayProcessor implements Processor {
        return "";
    }
    private ApiInfo getApiInfo(String api) {
        ApiInfo apiInfo = apiMap.get(api);
        if (apiInfo != null) {
            return apiInfo;
    private AppApi getApiInfo(String api) {
        AppApi appApi = apiMap.get(api);
        if (appApi != null) {
            return appApi;
        }
        HTTPResponse response = HttpClientKit.get("http://192.168.1.221:10000/api/v1.0/admin/appApi?fields=appId%2Cmethod%2Cprotocol%2CmethodName&filters=methodName%3D" + api + "&size=15&page=1");
        HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/appApi/search?filters=msMethodName%3D" + api + "&size=15&page=1");
        if (response.getStatusCode() != 200) {
            return null;
        }
        apiInfo = new ApiInfo(response.getBody());
        apiMap.put(api, apiInfo);
        try {
            appApi = AppApi.parse(response.getBody());
            apiMap.put(api, appApi);
        } catch (IOException e) {
            e.printStackTrace();
        return apiInfo;
            return null;
        }
        return appApi;
    }

+ 1 - 1
hos-camel/src/main/java/gateway/route/GatewayRouterBuilder.java

@ -14,7 +14,7 @@ public class GatewayRouterBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty:http://localhost:8888/api/v1").routeId("mock:collect")
        from("jetty:http://0.0.0.0:8888/api/v1").routeId("mock:collect")
                .process(new GatewayProcessor())
                .routingSlip(method(GatewayProcessor.class, "secret"))
                .routingSlip(method(GatewayProcessor.class, "route"));