get non service discovery HTTP service instance from HttpInstanceService #112

This commit is contained in:
Francis Dong
2021-04-14 15:46:08 +08:00
committed by hongqiaowei
parent cf3711b6cd
commit d7158a4e9c
2 changed files with 91 additions and 8 deletions

View File

@@ -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

View File

@@ -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<String, String> getQueryParams(){
if (isNewVersion()) {
return null;
}
MultiValueMap<String, String> 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;
}
}