From c22e26cc604c52c83f1d04e6ffca909e2a407683 Mon Sep 17 00:00:00 2001 From: Francis Dong Date: Wed, 14 Apr 2021 15:46:08 +0800 Subject: [PATCH] get non service discovery HTTP service instance from HttpInstanceService #112 --- .../input/extension/request/RequestInput.java | 29 +++++++- .../extension/request/RequestInputConfig.java | 70 +++++++++++++++++-- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java b/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java index 35c9961..79278c3 100644 --- a/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java +++ b/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java @@ -44,6 +44,7 @@ import we.fizz.StepResponse; import we.fizz.input.*; import we.flume.clients.log4j2appender.LogService; import we.proxy.FizzWebClient; +import we.proxy.http.HttpInstanceService; import we.util.JacksonUtils; import we.util.MapUtil; @@ -69,6 +70,8 @@ public class RequestInput extends RPCInput implements IInput{ private static final String CONTENT_TYPE = "content-type"; + private static final Integer SERVICE_TYPE_HTTP = 2; + private String respContentType; public InputType getType() { @@ -160,10 +163,30 @@ public class RequestInput extends RPCInput implements IInput{ } } } + + if (config.isNewVersion()) { + String host = config.getServiceName(); + if (SERVICE_TYPE_HTTP.equals(config.getServiceType().intValue())) { + HttpInstanceService httpInstanceService = this.getCurrentApplicationContext() + .getBean(HttpInstanceService.class); + String instance = httpInstanceService.getInstance(config.getServiceName()); + if (instance != null) { + host = instance; + } + } + StringBuffer sb = new StringBuffer(); + sb.append(config.getProtocol()).append("://").append(host) + .append(config.getPath().startsWith("/") ? "" : "/").append(config.getPath()); - UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + config.getPath()) - .queryParams(MapUtil.toMultiValueMap(params)).build(); - request.put("url", uriComponents.toUriString()); + UriComponents uriComponents = UriComponentsBuilder.fromUriString(sb.toString()) + .queryParams(MapUtil.toMultiValueMap(params)).build(); + + request.put("url", uriComponents.toUriString()); + } else { + UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + config.getPath()) + .queryParams(MapUtil.toMultiValueMap(params)).build(); + request.put("url", uriComponents.toUriString()); + } } @Override diff --git a/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInputConfig.java b/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInputConfig.java index d7f723a..0031ad4 100644 --- a/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInputConfig.java +++ b/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInputConfig.java @@ -21,8 +21,8 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.springframework.util.MultiValueMap; -import org.springframework.util.StringUtils; import org.springframework.web.util.UriComponentsBuilder; import we.fizz.input.InputConfig; @@ -38,14 +38,32 @@ public class RequestInputConfig extends InputConfig { private URL url ; private String method ; private int timeout = 3; + private String protocol; + /** + * Service Type, 1 service discovery, 2 HTTP service + */ + private Integer serviceType; + private String serviceName; + private String path; public RequestInputConfig(Map configBody) { super(configBody); - String url = (String) configBody.get("url"); - if(StringUtils.isEmpty(url)) { - throw new RuntimeException("Request URL can not be blank"); + + if (configBody.get("serviceType") != null && StringUtils.isNotBlank((String) configBody.get("protocol")) + && StringUtils.isNotBlank((String) configBody.get("serviceName")) + && StringUtils.isNotBlank((String) configBody.get("path"))) { + serviceType = Integer.valueOf(configBody.get("serviceType").toString()); + protocol = ((String) configBody.get("protocol")).toLowerCase(); + serviceName = (String) configBody.get("serviceName"); + path = (String) configBody.get("path"); + } else { + String url = (String) configBody.get("url"); + if (StringUtils.isBlank(url)) { + throw new RuntimeException("Request URL can not be blank"); + } + setUrl(url); } - setUrl(url); + if (configBody.get("method") != null) { setMethod(((String)configBody.get("method")).toUpperCase()); } else { @@ -63,11 +81,22 @@ public class RequestInputConfig extends InputConfig { } } + public boolean isNewVersion() { + if (serviceType != null && StringUtils.isNotBlank(protocol) && StringUtils.isNotBlank(serviceName) + && StringUtils.isNotBlank(path)) { + return true; + } + return false; + } + public String getQueryStr(){ return url.getQuery(); } public MultiValueMap getQueryParams(){ + if (isNewVersion()) { + return null; + } MultiValueMap parameters = UriComponentsBuilder.fromUriString(url.toString()).build().getQueryParams(); return parameters; @@ -79,6 +108,9 @@ public class RequestInputConfig extends InputConfig { } public String getPath() { + if (isNewVersion()) { + return this.path; + } return url.getPath(); } @@ -112,4 +144,32 @@ public class RequestInputConfig extends InputConfig { this.timeout = timeout; } + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public Integer getServiceType() { + return serviceType; + } + + public void setServiceType(Integer serviceType) { + this.serviceType = serviceType; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public void setPath(String path) { + this.path = path; + } + }