提交 47963078 编写于 作者: ibizdev's avatar ibizdev

ibizdev提交

上级 997f6d3b
package cn.ibizlab.util.service; package cn.ibizlab.util.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.springfox.SwaggerJsonSerializer;
import feign.*; import feign.*;
import feign.codec.Decoder; import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignContext; import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -94,9 +91,10 @@ public class RemoteService { ...@@ -94,9 +91,10 @@ public class RemoteService {
object = Feign.builder() object = Feign.builder()
//decoder指定对象解码方式 //decoder指定对象解码方式
.decoder(this.feignDecoder()) .decoder(this.feignDecoder())
.encoder(this.feignEncoder())
.client(feignClient) .client(feignClient)
//options方法指定连接超时时长及响应超时时长 //options方法指定连接超时时长及响应超时时长
.options(new Request.Options(5000, 5000)) .options(new Request.Options(5000, 50000))
//retryer方法指定重试策略 //retryer方法指定重试策略
//.retryer(new Retryer.Default(5000, 5000, 3)) //.retryer(new Retryer.Default(5000, 5000, 3))
.contract(feignContext.getInstance(serviceId, Contract.class)) .contract(feignContext.getInstance(serviceId, Contract.class))
...@@ -112,31 +110,18 @@ public class RemoteService { ...@@ -112,31 +110,18 @@ public class RemoteService {
return new SpringDecoder(feignHttpMessageConverter()); return new SpringDecoder(feignHttpMessageConverter());
} }
/** private Encoder feignEncoder() {
* 设置解码器为fastjson return new SpringEncoder(feignHttpMessageConverter());
*
* @return
*/
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(this.getFastJsonConverter());
return () -> httpMessageConverters;
} }
private FastJsonHttpMessageConverter getFastJsonConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<>();
MediaType mediaTypeJson = MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE);
supportedMediaTypes.add(mediaTypeJson);
converter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfig config = new FastJsonConfig();
config.getSerializeConfig().put(JSON.class, new SwaggerJsonSerializer());
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
return converter; private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(new ObjectMapper());
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jsonConverter);
return () -> httpMessageConverters;
} }
@Configuration @Configuration
public class FeignTokenInterceptor implements RequestInterceptor { public class FeignTokenInterceptor implements RequestInterceptor {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册