This commit is contained in:
tanghc
2019-08-27 17:29:02 +08:00
parent ba7f9a4e4d
commit 6c9f5528f2
9 changed files with 74 additions and 15 deletions

View File

@@ -51,14 +51,17 @@ public class HttpTool {
protected void initHttpClient(HttpToolConfig httpToolConfig) {
httpClient = new OkHttpClient.Builder()
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS) // 设置链接超时时间默认10秒
// 设置链接超时时间默认10秒
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS)
.readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS)
.writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS)
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
cookieStore.put(httpUrl.host(), list);
}
@Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
List<Cookie> cookies = cookieStore.get(httpUrl.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
@@ -211,9 +214,12 @@ public class HttpTool {
bodyBuilder.setType(MultipartBody.FORM);
for (UploadFile uploadFile : files) {
bodyBuilder.addFormDataPart(uploadFile.getName(), // 请求的名字
uploadFile.getFileName(), // 文件的文字,服务器端用来解析的
RequestBody.create(null, uploadFile.getFileData()) // 创建RequestBody把上传的文件放入
// 请求的名字
bodyBuilder.addFormDataPart(uploadFile.getName(),
// 文件的文字,服务器端用来解析的
uploadFile.getFileName(),
// 创建RequestBody把上传的文件放入
RequestBody.create(null, uploadFile.getFileData())
);
}
@@ -255,10 +261,15 @@ public class HttpTool {
}
public enum HTTPMethod {
/** http GET */
GET,
/** http POST */
POST,
/** http PUT */
PUT,
/** http HEAD */
HEAD,
/** http DELETE */
DELETE;
private HTTPMethod() {

View File

@@ -20,15 +20,17 @@ public class ByteArrayStreamWrapper extends ServletInputStream {
* @param data a <code>byte[]</code> value
*/
public ByteArrayStreamWrapper(byte[] data) {
if (data == null)
if (data == null) {
data = new byte[0];
}
this.data = data;
}
@Override
public int read() throws IOException {
if (idx == data.length)
if (idx == data.length) {
return -1;
}
// I have to AND the byte with 0xff in order to ensure that it is returned as an unsigned integer
// the lack of this was causing a weird bug when manually unzipping gzipped request bodies
return data[idx++] & 0xff;

View File

@@ -42,6 +42,9 @@ import static org.springframework.util.StringUtils.isEmpty;
import static org.springframework.util.StringUtils.tokenizeToStringArray;
import static org.springframework.util.StringUtils.uriDecode;
/**
* @author tanghc
*/
public class RequestContentDataExtractor {
public static MultiValueMap<String, Object> extract(HttpServletRequest request) throws IOException {
return (request instanceof MultipartHttpServletRequest) ?

View File

@@ -8,7 +8,18 @@ import java.util.function.Consumer;
* @author tanghc
*/
public interface RouteLoader {
/**
* 加载路由
*
* @param serviceRouteInfo 服务路由信息
* @param callback 加载成功后回调
*/
void load(ServiceRouteInfo serviceRouteInfo, Consumer<Object> callback);
/**
* 移除某个微服务下的所有路由信息
*
* @param serviceId 服务id
*/
void remove(String serviceId);
}

View File

@@ -89,7 +89,7 @@ public class ZuulParameterUtil {
log.error("修改上传文件请求参数失败, apiParam:{}", apiParam, e);
}
} else if(HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) {
Map<String, List<String>> newParams = new HashMap<>();
Map<String, List<String>> newParams = new HashMap<>(apiParam.size() * 2);
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
Object value = entry.getValue();
if (value instanceof List) {

View File

@@ -7,6 +7,9 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tanghc
*/
@RestController
public class EasyopenServiceRouteController extends ServiceRouteController implements ApplicationContextAware {

View File

@@ -118,6 +118,12 @@ public class Client {
}
public interface Callback {
/**
* 请求成功后回调
*
* @param requestInfo 请求信息
* @param responseData 返回结果
*/
void callback(RequestInfo requestInfo, String responseData);
}
@@ -347,6 +353,7 @@ public class Client {
/**
* 返回json跟节点名称
*
* @return 返回json跟节点名称
*/
public String getDataNode() {

View File

@@ -51,14 +51,17 @@ public class HttpTool {
protected void initHttpClient(HttpToolConfig httpToolConfig) {
httpClient = new OkHttpClient.Builder()
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS) // 设置链接超时时间默认10秒
// 设置链接超时时间默认10秒
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS)
.readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS)
.writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS)
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
cookieStore.put(httpUrl.host(), list);
}
@Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
List<Cookie> cookies = cookieStore.get(httpUrl.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
@@ -211,9 +214,12 @@ public class HttpTool {
bodyBuilder.setType(MultipartBody.FORM);
for (UploadFile uploadFile : files) {
bodyBuilder.addFormDataPart(uploadFile.getName(), // 请求的名字
uploadFile.getFileName(), // 文件的文字,服务器端用来解析的
RequestBody.create(null, uploadFile.getFileData()) // 创建RequestBody把上传的文件放入
// 请求的名字
bodyBuilder.addFormDataPart(uploadFile.getName(),
// 文件的文字,服务器端用来解析的
uploadFile.getFileName(),
// 创建RequestBody把上传的文件放入
RequestBody.create(null, uploadFile.getFileData())
);
}
@@ -255,10 +261,15 @@ public class HttpTool {
}
public enum HTTPMethod {
/** http GET */
GET,
/** http POST */
POST,
/** http PUT */
PUT,
/** http HEAD */
HEAD,
/** http DELETE */
DELETE;
private HTTPMethod() {

View File

@@ -51,14 +51,17 @@ public class HttpTool {
protected void initHttpClient(HttpToolConfig httpToolConfig) {
httpClient = new OkHttpClient.Builder()
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS) // 设置链接超时时间默认10秒
// 设置链接超时时间默认10秒
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS)
.readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS)
.writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS)
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
cookieStore.put(httpUrl.host(), list);
}
@Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
List<Cookie> cookies = cookieStore.get(httpUrl.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
@@ -211,9 +214,12 @@ public class HttpTool {
bodyBuilder.setType(MultipartBody.FORM);
for (UploadFile uploadFile : files) {
bodyBuilder.addFormDataPart(uploadFile.getName(), // 请求的名字
uploadFile.getFileName(), // 文件的文字,服务器端用来解析的
RequestBody.create(null, uploadFile.getFileData()) // 创建RequestBody把上传的文件放入
// 请求的名字
bodyBuilder.addFormDataPart(uploadFile.getName(),
// 文件的文字,服务器端用来解析的
uploadFile.getFileName(),
// 创建RequestBody把上传的文件放入
RequestBody.create(null, uploadFile.getFileData())
);
}
@@ -255,10 +261,15 @@ public class HttpTool {
}
public enum HTTPMethod {
/** http GET */
GET,
/** http POST */
POST,
/** http PUT */
PUT,
/** http HEAD */
HEAD,
/** http DELETE */
DELETE;
private HTTPMethod() {