SpringCloud通过Feign方式调用上传文件服务

发现问题

平时已经在生产环境跑了很久的上传服务,今天突然爆出个如下图的狗屎错误(具体原因未知,可能是版本升级或是服务器迁移到AWS等)
在这里插入图片描述
并没有发现是啥问题,去百度,论坛找了一圈也没有找到好的解决方案

@Component
@FeignClient(name = "instrument-service", configuration = {FeignMultipartSupportConfig.class})
public interface InstrumentClient {
    @RequestMapping(method = RequestMethod.POST, value = {"/instrument/uploadFile"}, produces = {"application/json;charset=UTF-8"}, consumes = {"multipart/form-data"})
    String fileUpload(@RequestPart("file") MultipartFile file);
}

上图configuration的feignConfig配置如下(顺便带上import的包)

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.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignMultipartSupportConfig{
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

pom的配置

<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
			<groupId>io.github.openfeigngroupId>
			<artifactId>feign-coreartifactId>
		dependency>
		<dependency>
			<groupId>io.github.openfeigngroupId>
			<artifactId>feign-ribbonartifactId>
		dependency>

以上的配置之前在生产上很久都没有问题
不知道因为可能是因为SpringCloud版本的问题

解决方案

在排除了一些代码的问题情况下,参考了一些文章加上了如下两个包

<dependency>
			<groupId>io.github.openfeign.formgroupId>
			<artifactId>feign-formartifactId>
			<version>3.2.2version>
		dependency>
		<dependency>
			<groupId>io.github.openfeign.formgroupId>
			<artifactId>feign-form-springartifactId>
			<version>3.2.2version>
		dependency>

上面的版本不是最新的,但是亲测是好用的版本!
下面配上入口方法的代码和注解

@EnableCaching
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@MapperScan(PkgConstants.MapperPackage)
public class OperationServiceApplication {
	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(OperationServiceApplication.class);
		springApplication.run(args);
	}
}

问题解决啦!

2019-05-15 17:56:19.938  INFO 16032 --- [io-18010-exec-1] s.c.a.AnnotationConfigApplicationContext : Refreshing SpringClientFactory-instrument-service: startup date [Wed May 15 17:56:19 CST 2019]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6492fab5
2019-05-15 17:56:20.133  INFO 16032 --- [io-18010-exec-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-05-15 17:56:20.546  INFO 16032 --- [io-18010-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: instrument-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-05-15 17:56:20.572  INFO 16032 --- [io-18010-exec-1] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-instrument-service
2019-05-15 17:56:20.580  INFO 16032 --- [io-18010-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: instrument-service instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=instrument-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-05-15 17:56:20.590  INFO 16032 --- [io-18010-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2019-05-15 17:56:20.625  INFO 16032 --- [io-18010-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: instrument-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-05-15 17:56:20.629  INFO 16032 --- [io-18010-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client instrument-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=instrument-service,current list of Servers=[localhost:18012],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:1;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
},Server stats: [[Server:localhost:18012;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@1a34fd1
2019-05-15 17:56:21.597  INFO 16032 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: instrument-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

你可能感兴趣的:(SpringCloud问题)