SpringCloud——Zuul 文件上传

目录

1、上传文件微服务

2、zuul微服务(最简单的配置)

3、利用zuul上传文件

4、如果上传过大文件出现问题如下——超时1

官方文档解决办法——可以传比较大的zip压缩图片啥的。。。。

5、如果上传过大文件出现问题如下——the request was rejected because its size (26246240) exceeds the configured


 

  • 1、上传文件微服务

  1. 1、pom.xml

		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
  1. 2、删除文件的controller.java
@RestController
public class FileUploadController {

    /**
     * 上传文件
     * 测试方法:
     * 测试接口:http://localhost:8050/upload
     * 使用PostMan测试
     *
     * @param file 待上传文件
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public
    @ResponseBody
    String handleFileUpload(@RequestParam(value = "file", required = true) MultipartFile file) {
        File fileToSave = null;
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                fileToSave = new File(file.getOriginalFilename());
                //fileToSave = new File("D:\\测绘管理系统\\文件系统\\文件存储" + file.getOriginalFilename()); 自定义存储路径
                FileCopyUtils.copy(bytes, fileToSave);
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            return "上传成功" + fileToSave.getAbsolutePath();
        } else {
            return "上传失败,因为文件是空的.";
        }
    }

}
  1. 3、application.yml
server:
  port: 8050

eureka:
  client:
    service-url:
      defaultZone: http://user:123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    
spring:
  application:
    name: file-upload
  http:
    multipart:
      max-file-size: 200Mb # Max file size,默认1M
      max-request-size: 2500Mb     # Max request size,默认10M
  1. 4、主启动类
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudFileUploadApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudFileUploadApplication.class, args);
	}

}
  1. 5、测试接口

SpringCloud——Zuul 文件上传_第1张图片

说明上传微服务没有问题!!!!!


  • 2、zuul微服务(最简单的配置)

  1. 1、pom.xml

		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-zuul
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	
  1. 2、主启动类
@SpringBootApplication
@EnableZuulProxy
public class SpringCloudZuul004Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudZuul004Application.class, args);
	}

}
  1. 3、applicaiton.yml
spring:
  application:
    name: gateway-zuul-004
server:
  port: 8040
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000


#actuator  启用所有的监控端点 “*”号代表启用所有的监控端点,可以单独启用,例如,health,info,metrics
#  spring boot 升为 2.0 后,为了安全,默认 Actuator 只暴露了2个端点,heath 和 info
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
      health:
        show-details: ALWAYS


  • 3、利用zuul上传文件

SpringCloud——Zuul 文件上传_第2张图片

  • 4、如果上传过大文件出现问题如下——超时1

SpringCloud——Zuul 文件上传_第3张图片

官方文档解决办法——可以传比较大的zip压缩图片啥的。。。。

SpringCloud——Zuul 文件上传_第4张图片

所以我们在请求的时候加zuul前缀即可

经过zuul的请求都会被hystrix包裹,所以要配置hystrix的处理时间,zuul还使用了ribbon作为负载均衡,所以ribbon的时间也要设置!!

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000

  • 5、如果上传过大文件出现问题如下——the request was rejected because its size (26246240) exceeds the configured

          SpringCloud——Zuul 文件上传_第5张图片

我传了一个271MB的视频,报错

解决办法:

1、主启动类

@Bean
public MultipartConfigElement multipartConfigElement() {
   MultipartConfigFactory factory = new MultipartConfigFactory();
   //文件最大
   factory.setMaxFileSize("1024000KB"); //KB,MB
   /// 设置总上传数据总大小
   factory.setMaxRequestSize("1024000KB");
   return factory.createMultipartConfig();
}

2、配置文件上传这个参数相等SpringCloud——Zuul 文件上传_第6张图片

SpringCloud——Zuul 文件上传_第7张图片

结果

SpringCloud——Zuul 文件上传_第8张图片

你可能感兴趣的:(springcloud)