spring cloud 两个feignclient 名称相同时的问题

1.报错现象

线上发版发现以前能用的功能,线上报错。

  1. 列表内容 如下:
Caused by: feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [application/x-www-form-urlencoded]
    at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:115)
    at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:351)
    at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:213)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:72)
    at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108)
    at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
    at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
    ... 28 common frames omitted
  1. feign代码:


import com.jaws.core.common.vo.OpResponse;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Map;
@FeignClient(value = "yyfax-message", configuration = MessageClient.FormSupportConfig.class)
public interface MessageClient {
    /**
     * 短信即时发送(无模板)
     */
    @PostMapping(value = "/api/message/sms/send", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
    OpResponse smsSend(Map formParams);

    /**
     * 推送消息发送
     */
    @PostMapping(value = "/api/message/push/send", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
    String pushSend(Map formParams);

    // @Component
    class FormSupportConfig {
        @Autowired
        private ObjectFactory messageConverters;

        // new一个form编码器,实现支持form表单提交
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}

2.分析问题

由于项目中有多个feign的value为yyfax-message,造成configration不起作用。
经过测试,以下情况运行正常。

  1. 项目中不存在其他value为yyfax-message的feign时。
  2. 将configuration打上注解component。
  3. 将feign的名称改为http://yyfax-message。

    大致可以确定为,由于feign的value相同,而造成的bug。已经有人提出该bug,这里写链接内容。


3.解决问题

  1. 方法1,修改feign的value


import com.jaws.core.common.vo.OpResponse;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Map;

@FeignClient(name = "http://yyfax-message", configuration = MessageClient.FormSupportConfig.class)
// @FeignClient(value = "yyfax-message", configuration = MessageClient.FormSupportConfig.class)
public interface MessageClient {
    /**
     * 短信即时发送(无模板)
     */
    @PostMapping(value = "/api/message/sms/send", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
    OpResponse smsSend(Map formParams);

    /**
     * 推送消息发送
     */
    @PostMapping(value = "/api/message/push/send", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
    String pushSend(Map formParams);

    // @Component
    class FormSupportConfig {
        @Autowired
        private ObjectFactory messageConverters;

        // new一个form编码器,实现支持form表单提交
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}
  1. 方法2,打@Component注解


import com.jaws.core.common.vo.OpResponse;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Map;

// @FeignClient(name = "http://yyfax-message", configuration = MessageClient.FormSupportConfig.class)
@FeignClient(value = "yyfax-message", configuration = MessageClient.FormSupportConfig.class)
public interface MessageClient {
    /**
     * 短信即时发送(无模板)
     */
    @PostMapping(value = "/api/message/sms/send", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
    OpResponse smsSend(Map formParams);

    /**
     * 推送消息发送
     */
    @PostMapping(value = "/api/message/push/send", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
    String pushSend(Map formParams);

    @Component
    class FormSupportConfig {
        @Autowired
        private ObjectFactory messageConverters;

        // new一个form编码器,实现支持form表单提交
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}

参考文章

这里写链接内容
这里写链接内容

你可能感兴趣的:(java)