如何使用Feignchrome 构造post请求多参数的请求

推荐这篇日记的豆列
······Spring Cloud中,Feign常见问题总结 - 推酷
Spring Cloud中,Feign常见问题总结
Spring Cloud中,Feign常见问题的总结。
FeignClient接口,不能使用
@GettingMapping
之类的组合注解
代码示例:
@FeignClient("microservice-provider-user")
publicinterfaceUserFeignClient{
@RequestMapping(value ="/simple/{id}", method = RequestMethod.GET)
publicUserfindById(@PathVariable("id")Long id);
@RequestMapping(value = &/simple/{id}&, method = RequestMethod.GET)
@GetMapping(&/simple/{id}&)
FeignClient接口中,如果使用到
@PathVariable
,必须指定其value
代码示例:
@FeignClient("microservice-provider-user")
publicinterfaceUserFeignClient{
@RequestMapping(value ="/simple/{id}", method = RequestMethod.GET)
publicUserfindById(@PathVariable("id")Long id);
@PathVariable(&id&)
中的”id”,不能省略,必须指定。
FeignClient方法中的参数,暂不支持GET复杂对象
@FeignClient("microservice-provider-user")
publicinterfaceUserFeignClient{
@RequestMapping(value ="/get-user", method = RequestMethod.GET)
publicUsergetUser(User user);
该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。
Feign如果想要使用Hystrix Stream,需要做一些额外操作
我们知道Feign本身就是支持Hystrix的,可以直接使用
@FeignClient(value = &microservice-provider-user&, fallback = XXX.class)
来指定fallback的类,这个fallback类集成@FeignClient所标注的接口即可。
但是假设我们需要使用Hystrix Stream进行监控,默认情况下,访问
是个404。如何为Feign增加Hystrix Stream支持呢?
需要以下两步:
第一步:添加依赖,示例:
&!-- 整合hystrix,其实feign中自带了hystrix,引入该依赖主要是为了使用其中的hystrix-metrics-event-stream,用于dashboard --&
&dependency&
&groupId&org.springframework.cloud&/groupId&
&artifactId&spring-cloud-starter-hystrix&/artifactId&
&/dependency&
第二步:在启动类上添加
@EnableCircuitBreaker
注解,示例:
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
publicclassMovieFeignHystrixApplication{
publicstaticvoidmain(String[] args){
SpringApplication.run(MovieFeignHystrixApplication.class, args);
这样修改以后,访问任意的API后,再访问
如果需要自定义单个Feign配置,Feign的
@Configuration
注解的类不能与
@ComponentScan
如果包重叠,将会导致所有的Feign Client都会使用该配置。
首次请求失败
@FeignClient
的属性注意点
(1) serviceId属性已经失效,尽量使用name属性。例如:
@FeignClient(serviceId = "microservice-provider-user")
这么写是不推荐的,应写为:
@FeignClient(name = "microservice-provider-user")
(2) 在使用url属性时,在老版本的Spring Cloud中,不需要提供name属性,但是在新版本(例如Brixton、Camden)@FeignClient必须提供name属性,并且name、url属性支持占位符。例如:
@FeignClient(name = "${feign.name}", url = "${feign.url}")
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致SpringCloud(15)
java.util.concurrent.TimeoutException: null
at com.netflix.hystrix.AbstractCommand.handleTimeoutViaFallback(AbstractCommand.java:980) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.AbstractCommand.access$500(AbstractCommand.java:59) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.AbstractCommand$12.call(AbstractCommand.java:595) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.AbstractCommand$12.call(AbstractCommand.java:587) ~[hystrix-core-1.5.6.jar:1.5.6]
at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:140) ~[rxjava-1.1.10.jar:1.1.10]
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87) ~[rxjava-1.1.10.jar:1.1.10]
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87) ~[rxjava-1.1.10.jar:1.1.10]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1.run(AbstractCommand.java:1121) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:41) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:37) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$2.tick(AbstractCommand.java:1138) ~[hystrix-core-1.5.6.jar:1.5.6]
at com.netflix.hystrix.util.HystrixTimer$1.run(HystrixTimer.java:99) ~[hystrix-core-1.5.6.jar:1.5.6]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_101]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[na:1.8.0_101]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_101]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[na:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_101]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
2017-05-15 20:40:21.407
INFO 5108 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty
: Flipping property: springcloud-userservcie.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit =
#索性禁用Feign的Hystrix
feign.hystrix.enabled=false
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:52492次
积分:1680
积分:1680
排名:千里之外
原创:116篇
(7)(11)(16)(19)(7)(15)(20)(21)(3)(1)Feign正确的使用姿势和性能优化注意事项 - 简书
Feign正确的使用姿势和性能优化注意事项
1. feign自定义Configuration和root 容器有效隔离。
用@Configuration注解
不能在主@ComponentScan (or @SpringBootApplication)范围内,从其包名上分离
注意避免包扫描重叠,最好的方法是明确的指定包名
2. Spring Cloud Netflix 提供了默认的Bean类型:
Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
Encoder feignEncoder: SpringEncoder
Logger feignLogger: Slf4jLogger
Contract feignContract: SpringMvcContract
Feign.Builder feignBuilder: HystrixFeign.Builder
3. Spring Cloud Netflix没有提供默认值,但仍然可以在feign上下文配置中创建:
Logger.Level
ErrorDecoder
Request.Options
Collection&RequestInterceptor&
4. 自定义feign的消息编码解码器:
不要在如下代码中getObject方法内new 对象,外部会频繁调用getObject方法。
ObjectFactory&HttpMessageConverters& messageConvertersObjectFactory = new ObjectFactory&HttpMessageConverters&() {
public HttpMessageConverters getObject() throws BeansException {
return httpMessageC
5. 注意测试环境和生产环境,注意正确使用feign日志级别。
6. apacheHttpclient或者其他client的正确配置:
apacheHttpclient自定义配置放在spring root context,不要在FeignContext,否则不会起作用。
apacheHttpclient 连接池配置合理地连接和其他参数
7. Feign配置
#Hystrix支持,如果为true,hystrix库必须在classpath中
feign.hystrix.enabled=false
#请求和响应GZIP压缩支持
pression.request.enabled=pression.response.enabled=true
#支持压缩的mime pression.request.enabled=pression.request.mime-types=text/xml,application/xml,application/pression.request.min-request-size=2048
# 日志支持
logging.level.project.user.UserClient: DEBUG
8. Logger.Level支持
必须为每一个Feign Client配置来告诉Feign如何输出日志,可选:
NONE, No logging (DEFAULT).
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 {
public Hello iFailSometimes() {
return new Hello("fallback");
@Configuration
public class FooConfiguration {
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
public HystrixClientFallback fb(){
return new HystrixClientFallback();
10. 使用Feign Client 和@RequestMapping时,注意事项
当前工程中有和Feign Client中一样的Endpoint时,Feign Client的类上不能用@RequestMapping注解否则,当前工程该endpoint http请求且使用accpet时会报404.
下面的例子:
有一个 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();
if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :
如果 @RequestMapping注解被用在FeignClient类上,当像如下代码请求/v1/card/balance时,注意有Accept header:
Content-Type:application/json
Accept:application/json
POST http://localhost:7913/v1/card/balance
那么会返回 404。
如果不包含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();
http://tietang.wang

我要回帖

更多关于 如何构造post请求 的文章

 

随机推荐