SpringCloud-技术专题-Feign组件基本使用(2)
前提概要
本章主要介绍相关的Fegin的相关配置和更深入一些的使用方式,如果对Fegin的不了解的话,可以先了
解一下,基于上一章之后,本章节属于完全不依赖于前一章,请放心观看本章内容。
用@Configuration注解不能在@ComponentScan or @SpringBootApplication范围内,从其包名上分离注意避免包扫描重叠,最好的方法是明确的指定包名 。
Decoder feignDecoder:ResponseEntityDecoder (which wraps a SpringDecoder)
Encoder feignEncoder:SpringEncoder
Logger feignLogger: Slf4jLogger
Contract feignContract: SpringMvcContract
Feign.Builder feignBuilder: HystrixFeign.Builder
Logger.LevelRetryerErrorDecoderRequest.OptionsCollection
ObjectFactory messageConvertersObjectFactory =
new ObjectFactory() {
@Override
public HttpMessageConverters getObject() throws BeansException {
return httpMessageConverters;}
};
}
apacheHttpclient 配置放在spring root context ,不要在FeignContext ,否则不会起作用。apacheHttpclient 连接池配置合理地连接和其他参数
如果为true,hystrix库必须在classpath中 feign.hystrix.enabled=false
feign.compression.request.enabled=true feign.compression.response.enabled=true
feign.compression.request.enabled=true feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2048
日志支持
logging.level.project.user.UserClient: DEBUG
Logger.Level支持必须为每一个Feign Client配置来告诉Feign如何输出日志,可选:
NONE,No logging (DEFAULT).
BASIC, Log only the request method and URL and the response status code and execution time.
HEADERS, Log the basic information along with request and response headers.
FULL, Log the headers, body, and metadata for both requests and responses.9.
FeignClient.fallback 正确的使用方法配置的 fallback class 也必须在 FeignClient Configuration中实例化,否则会报java.lang.IllegalStateException: No fallback instance of type class异常。
例子
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
public interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
public class HystrixClientFallback implements HystrixClient {
@Override
public Hello iFailSometimes() {
return new Hello("fallback");
}
}
@Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
@Bean
public HystrixClientFallback fb(){
return new HystrixClientFallback();
}
}
使用Feign Client 和@RequestMapping时,注意事项当前工程中有和Feign Client中一样的Endpoint时Feign Client的类上不能用
例子
有一个 Controller
@RestController
@RequestMapping("/v1/card")
public class IndexApi {
@PostMapping("balance")
@ResponseBody
public Info index() {
Info.Builder builder = new Info.Builder();
builder.withDetail("x", 2);
builder.withDetail("y", 2);
return builder.build();
}
}
有一个Feign Client
@FeignClient(name = "card",url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class)
@RequestMapping(value = "/v1/card")
public interface CardFeignClient {
@RequestMapping(value = "/balance", method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
Info info();
}
(1)如果 @RequestMapping注解被用在FeignClient类上,如下代码请求/v1/card/balance时,注意有,
Accept header:
Content-Type:application/jsonAccept:application/json
POST http://localhost:7913/v1/card/balance
(2)如果不包含Accept header时请求,则是OK:
Content-Type : application/json
POST http://localhost:7913/v1/card/balance
或者像下面不在Feign Client上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:
@FeignClient(name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class)
public interface CardFeignClient {
@RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
Info info();
}