package spring.cloud.client.uitils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
/**
* Created by Harry on 17/3/18.
*/
public class HttpClientUtil {
private static final String UTF_8 = "utf-8";
private static int time_out = 10*1000;
private static int retry_time = 2;
/**
* 发送post数据
* @param url
* @param treeMap
* @return
*/
public static Optional<String> post(String url, TreeMap<String, String> treeMap) {
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(time_out)
.setConnectTimeout(time_out)
.build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
List<BasicNameValuePair> formparams = HttpClientUtil.getNamedValuePairFromTreeMap( treeMap );
int times = 0;
while ( times < retry_time ) {
try {
if ( null != formparams && !formparams.isEmpty() ) {
httpPost.setEntity(new UrlEncodedFormEntity(formparams, UTF_8));
}
CloseableHttpClient httpclient = HttpClients.createDefault();
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString( entity );
EntityUtils.consume(entity);
return Optional.ofNullable(content);
}catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (null!=response) response.close();
} catch (IOException e) {}
}
times ++;
}
return Optional.empty();
}
public static Optional<String> postWidthBody(String url, String body, String contentType) {
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(time_out)
.setConnectTimeout(time_out)
.build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
if ( !Strings.isNullOrEmpty(body) ) {
StringEntity stringEntity = new StringEntity(body, UTF_8);
if (!Strings.isNullOrEmpty(contentType)){
stringEntity.setContentType(contentType);
}
httpPost.setEntity(stringEntity);
}
int times = 0;
while ( times < retry_time ) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString( entity );
EntityUtils.consume(entity);
return Optional.ofNullable(content);
}catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (null!=response) response.close();
} catch (IOException e) {}
}
times ++;
}
return Optional.empty();
}
public static Optional<String> postWidthBody(String url, String body){
return postWidthBody(url, body, null);
}
public static Optional<String> post(String url) {
return HttpClientUtil.post(url, null);
}
private static List<BasicNameValuePair> getNamedValuePairFromTreeMap(TreeMap<String, String> treeMap ) {
List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
if (null != treeMap && !treeMap.isEmpty()) {
Iterator<String> keyIte = treeMap.keySet().iterator();
while (keyIte.hasNext()) {
String key = keyIte.next();
String value = treeMap.get(key);
if ( Strings.isNullOrEmpty(key) || Strings.isNullOrEmpty(value) ) {
continue;
}
formparams.add(new BasicNameValuePair(key, value));
}
}
return formparams;
}
public static Optional<String> postMultiPart(String url, String fileName, byte[] fileData){
if (Strings.isNullOrEmpty(fileName)) {
fileName = "file.jpg";
}
if ( null == fileData ) {
return Optional.empty();
}
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(time_out)
.setConnectTimeout(time_out)
.build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("upload_file", fileData, ContentType.MULTIPART_FORM_DATA, fileName);
HttpEntity requestEntity = builder.build();
httpPost.setEntity(requestEntity);
int times = 0;
while ( times < retry_time ) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString( entity );
EntityUtils.consume(entity);
return Optional.ofNullable(content);
}catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (null!=response) response.close();
} catch (IOException e) {}
}
times ++;
}
return Optional.empty();
}
public static String getParamStringFromMap(TreeMap<String, String> treeMap) {
if ( null == treeMap || treeMap.isEmpty() ) {
return "";
}
List<String> keyValueList = new ArrayList<>();
if (null != treeMap && !treeMap.isEmpty()) {
Iterator<String> keyIte = treeMap.keySet().iterator();
while (keyIte.hasNext()) {
String key = keyIte.next();
String value = treeMap.get(key);
if ( Strings.isNullOrEmpty(key) || Strings.isNullOrEmpty(value) ) {
continue;
}
keyValueList.add( String.format("%s=%s", key,value) );
}
}
return Joiner.on("&").skipNulls().join(keyValueList);
}
/**
* 发送get数据
* @param url
* @param treeMap
* @return
*/
public static Optional<String> get(String url, TreeMap<Stri
没有合适的资源?快使用搜索试试~ 我知道了~
springcloud全家桶(eureka+ribbon+feign+hystrix+turbine)
共351个文件
class:118个
java:118个
properties:37个
1星 需积分: 50 56 下载量 32 浏览量
2018-11-16
16:07:55
上传
评论 3
收藏 3.87MB ZIP 举报
温馨提示
spring-cloud微服务框架demo,可以完整运行。这里面包含的组件和内容有: spring cloud eureka,服务注册和服务发现 spring cloud config,动态配置项 ribbon,客户端负载均衡 feign, hystrix,熔断 turbine Spring Cloud Starters 同一个服务中的多数据库支持(AOP) 全链路traceId追踪 velocity 前端模板 mybatis, pageHelper (分页), druid (连接池) redis(序列化采用的是jdk默认序列化方案) slf4j & logback(及其配置) 国际化配置 全局错误信息catch 线程池 服务健康检查, 服务全链路健康检查
资源推荐
资源详情
资源评论
收起资源包目录
springcloud全家桶(eureka+ribbon+feign+hystrix+turbine) (351个子文件)
HttpClientUtil.class 11KB
EncryptUtil.class 10KB
GlobalBean.class 7KB
FileTypeHelper.class 6KB
MomentServiceImpl.class 6KB
CommentServiceImpl.class 5KB
InteceptorAdder.class 5KB
InteceptorAdder.class 4KB
ListResultModel.class 4KB
InteceptorAdder.class 4KB
AccountServiceImpl.class 4KB
CookieCheckInterceptor.class 4KB
GlobalBeanConfig.class 4KB
CacheServiceImpl.class 4KB
HealthCheckController.class 4KB
AccountDataAccessImpl.class 3KB
HealthController.class 3KB
HealthController.class 3KB
AccountServiceImpl.class 3KB
DynamicDataSourceAspect.class 3KB
RefreshController.class 3KB
ResultModel.class 3KB
GlobalAspectInteceptor.class 3KB
SessionFactoryConfig.class 3KB
CacheServiceAutoConfig.class 3KB
SessionFactoryConfig.class 3KB
GlobalBeanConfig.class 3KB
GlobalBeanConfig.class 3KB
DynamicDataSource.class 3KB
MomentController.class 3KB
ObjectParser.class 3KB
CopyProperityUtils.class 2KB
GlobalAspectInteceptor.class 2KB
CommentController.class 2KB
GlobalAspectInteceptor.class 2KB
XmlParser.class 2KB
AccountController.class 2KB
SpringUtil.class 2KB
MomentController.class 2KB
GlobalCacheHelper.class 2KB
GlobalCacheHelper.class 2KB
GlobalCacheHelper.class 2KB
DateUtils.class 2KB
CommentController.class 2KB
AccountController.class 2KB
TraceIdFeignHttpRequrestInterceptor.class 2KB
LocaleAutoConfig.class 2KB
CustomVelocityConfig.class 2KB
RandomGenerator.class 2KB
MomentModel.class 2KB
GlobalExceptionHandlerForRestRequest.class 2KB
GlobalExceptionHandlerForRestRequest.class 2KB
AppEnvType.class 2KB
TraceIdHttpRequestInterceptor.class 2KB
BaseIntegrationTest.class 2KB
BaseIntegrationTest.class 2KB
BaseIntegrationTest.class 2KB
RefreshController.class 2KB
RefreshController.class 2KB
RefreshController.class 2KB
FilterDemo.class 2KB
FilterDemo.class 2KB
FilterDemo.class 2KB
CommentSourceConfiguration.class 2KB
CommentModel.class 2KB
ParamUtils.class 1KB
ParamCheckUtils.class 1KB
PrimarySourceConfiguration.class 1KB
AccountDo.class 1KB
DataSourceType.class 1KB
PrimarySourceConfiguration.class 1KB
AccountApplication.class 1KB
BizApplication.class 1KB
DynamicDataSourceContextHolder.class 1KB
InternationalDemoTest.class 1KB
InternationalDemoTest.class 1KB
InternationalDemoTest.class 1KB
TraceIdHelper.class 1KB
RemoteIpUtil.class 1KB
LocaleMessageService.class 1KB
CustomCallable.class 1KB
RequestLocaleResolver.class 1KB
CustomHystrixConcurrencyStrategy.class 1KB
GateWayApplication.class 1KB
CommentFeignService.class 1KB
MomentFeignService.class 1KB
AccountFeignService.class 1KB
ErrorCodeMap.class 1KB
JSONUtil.class 1016B
AccountHelper.class 932B
EurekaServerApplication.class 890B
AccountModel.class 855B
CommentDoMapper.class 807B
AccountDoMapper.class 762B
MomentDoMapper.class 732B
CacheService.class 689B
TargetDataSource.class 687B
CommentDo.class 684B
MomentDo.class 680B
SpringBeanAutoConfig.class 665B
共 351 条
- 1
- 2
- 3
- 4
资源评论
- longun_csdn22020-07-09骗子,里面就只有3个组件能用: gateway eureka 和 ribbon服务提供者。其他全是假的,没用的空工程
COLD11
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Python的PCA人脸识别算法的原理及实现代码详解+源码+详细代码解析+开发文档+数据(毕业设计&课程设计&项目开发)
- Decision tree20240105(1).ipynb
- zuoyezuoyezuoye
- zuoyezuoyezuoye
- 机械设计电机转子装配设备sw22非常好的设计图纸100%好用.zip
- 作业作业作业作业作业作业
- xdotool.c
- RLMD鲁棒性局部均值分解信号分量可视化(Matlab完整源码和数据)
- Screenshot_2024-04-26-17-17-26-36_9d26c6446fd7bb8e41d99b6262b17def.jpg
- 6.0版本超广角文件+教程使用MT管理器打-7.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功