get non service discovery HTTP service instance from HttpInstanceService #112
This commit is contained in:
@@ -44,6 +44,7 @@ import we.fizz.StepResponse;
|
|||||||
import we.fizz.input.*;
|
import we.fizz.input.*;
|
||||||
import we.flume.clients.log4j2appender.LogService;
|
import we.flume.clients.log4j2appender.LogService;
|
||||||
import we.proxy.FizzWebClient;
|
import we.proxy.FizzWebClient;
|
||||||
|
import we.proxy.http.HttpInstanceService;
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
import we.util.MapUtil;
|
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 String CONTENT_TYPE = "content-type";
|
||||||
|
|
||||||
|
private static final Integer SERVICE_TYPE_HTTP = 2;
|
||||||
|
|
||||||
private String respContentType;
|
private String respContentType;
|
||||||
|
|
||||||
public InputType getType() {
|
public InputType getType() {
|
||||||
@@ -161,10 +164,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(sb.toString())
|
||||||
|
.queryParams(MapUtil.toMultiValueMap(params)).build();
|
||||||
|
|
||||||
|
request.put("url", uriComponents.toUriString());
|
||||||
|
} else {
|
||||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + config.getPath())
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + config.getPath())
|
||||||
.queryParams(MapUtil.toMultiValueMap(params)).build();
|
.queryParams(MapUtil.toMultiValueMap(params)).build();
|
||||||
request.put("url", uriComponents.toUriString());
|
request.put("url", uriComponents.toUriString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doResponseMapping(InputConfig aConfig, InputContext inputContext, Object responseBody) {
|
public void doResponseMapping(InputConfig aConfig, InputContext inputContext, Object responseBody) {
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ import java.net.MalformedURLException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
import we.fizz.input.InputConfig;
|
import we.fizz.input.InputConfig;
|
||||||
|
|
||||||
@@ -38,14 +38,32 @@ public class RequestInputConfig extends InputConfig {
|
|||||||
private URL url ;
|
private URL url ;
|
||||||
private String method ;
|
private String method ;
|
||||||
private int timeout = 3;
|
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) {
|
public RequestInputConfig(Map configBody) {
|
||||||
super(configBody);
|
super(configBody);
|
||||||
|
|
||||||
|
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");
|
String url = (String) configBody.get("url");
|
||||||
if(StringUtils.isEmpty(url)) {
|
if (StringUtils.isBlank(url)) {
|
||||||
throw new RuntimeException("Request URL can not be blank");
|
throw new RuntimeException("Request URL can not be blank");
|
||||||
}
|
}
|
||||||
setUrl(url);
|
setUrl(url);
|
||||||
|
}
|
||||||
|
|
||||||
if (configBody.get("method") != null) {
|
if (configBody.get("method") != null) {
|
||||||
setMethod(((String)configBody.get("method")).toUpperCase());
|
setMethod(((String)configBody.get("method")).toUpperCase());
|
||||||
} else {
|
} 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(){
|
public String getQueryStr(){
|
||||||
return url.getQuery();
|
return url.getQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MultiValueMap<String, String> getQueryParams(){
|
public MultiValueMap<String, String> getQueryParams(){
|
||||||
|
if (isNewVersion()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
MultiValueMap<String, String> parameters =
|
MultiValueMap<String, String> parameters =
|
||||||
UriComponentsBuilder.fromUriString(url.toString()).build().getQueryParams();
|
UriComponentsBuilder.fromUriString(url.toString()).build().getQueryParams();
|
||||||
return parameters;
|
return parameters;
|
||||||
@@ -79,6 +108,9 @@ public class RequestInputConfig extends InputConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getPath() {
|
public String getPath() {
|
||||||
|
if (isNewVersion()) {
|
||||||
|
return this.path;
|
||||||
|
}
|
||||||
return url.getPath();
|
return url.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,4 +144,32 @@ public class RequestInputConfig extends InputConfig {
|
|||||||
this.timeout = timeout;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user