通过Zuul上传文件,禁用Zuul的Filters
生活随笔
收集整理的這篇文章主要介紹了
通过Zuul上传文件,禁用Zuul的Filters
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
microservice-file-uploadspring:application:name: microservice-file-uploadhttp:multipart:max-file-size: 2000Mb (默認(rèn)1M)max-request-size: 2500Mb (默認(rèn)10M)https://www.cnblogs.com/gqymy/p/11195794.htmlhttps://blog.csdn.net/starskyboy/article/details/83217251spring.application.http.multipart.max-file-size=2000Mb
spring.application.http.multipart.max-request-size=2000Mb/*** Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or* kilobytes respectively.*/
private String maxFileSize = "1MB";localhost:8050/index.htmlhttps://blog.csdn.net/shenzhen_zsw/article/details/81009235zuul有一個(gè)端點(diǎn)是/zuul/*https://cloud.spring.io/spring-cloud-netflix/reference/html/#_strangulation_patterns_and_local_forwardsUploading Files through Zuul我們上傳都是用的SpringMVC的,SpringMVC默認(rèn)是/**,所以它使用的是Spring DispatcherServlet,當(dāng)Zuul在你的classpath下面呢,他就會(huì)繞過(guò)這個(gè)東西上傳大文件得將超時(shí)時(shí)間設(shè)置長(zhǎng)一些,否則會(huì)報(bào)超時(shí)異常。以下幾行超時(shí)設(shè)置來(lái)自https://cloud.spring.io/spring-cloud-netflix/reference/html/#_uploading_files_through_zuulhystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:ConnectTimeout: 3000ReadTimeout: 60000hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
ribbon.ConnectTimeout=3000
ribbon.ReadTimeout=6000經(jīng)過(guò)zuul的請(qǐng)求,他都會(huì)用hystrix進(jìn)行包裹,所以我需要配一下hystrix的超時(shí)時(shí)間,然后zuul還是用了ribbon做負(fù)載均衡,所以我也要設(shè)置一下ribbon的ConnectTimeout,和ReadTimeout
microservice-file-uploadspring:application:name: microservice-file-uploadhttp:multipart:max-file-size: 2000Mb (默認(rèn)1M)max-request-size: 2500Mb (默認(rèn)10M)https://www.cnblogs.com/gqymy/p/11195794.htmlhttps://blog.csdn.net/starskyboy/article/details/83217251spring.application.http.multipart.max-file-size=2000Mb
spring.application.http.multipart.max-request-size=2000Mb/*** Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or* kilobytes respectively.*/
private String maxFileSize = "1MB";localhost:8050/index.htmlhttps://blog.csdn.net/shenzhen_zsw/article/details/81009235zuul有一個(gè)端點(diǎn)是/zuul/*https://cloud.spring.io/spring-cloud-netflix/reference/html/#_strangulation_patterns_and_local_forwardsUploading Files through Zuul我們上傳都是用的SpringMVC的,SpringMVC默認(rèn)是/**,所以它使用的是Spring DispatcherServlet,當(dāng)Zuul在你的classpath下面呢,他就會(huì)繞過(guò)這個(gè)東西上傳大文件得將超時(shí)時(shí)間設(shè)置長(zhǎng)一些,否則會(huì)報(bào)超時(shí)異常。以下幾行超時(shí)設(shè)置來(lái)自https://cloud.spring.io/spring-cloud-netflix/reference/html/#_uploading_files_through_zuulhystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:ConnectTimeout: 3000ReadTimeout: 60000hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
ribbon.ConnectTimeout=3000
ribbon.ReadTimeout=6000經(jīng)過(guò)zuul的請(qǐng)求,他都會(huì)用hystrix進(jìn)行包裹,所以我需要配一下hystrix的超時(shí)時(shí)間,然后zuul還是用了ribbon做負(fù)載均衡,所以我也要設(shè)置一下ribbon的ConnectTimeout,和ReadTimeout,使用Zuul上傳文件是可以上傳文件的,上傳大文件可以加/zuul/前綴,上傳大文件要設(shè)置一個(gè)超時(shí),這邊給了一個(gè)Demo,也是基于curl的$ curl -v -H "Transfer-Encoding: chunked" \-F "file=@mylarge.iso" localhost:9999/zuul/simple/file我們發(fā)現(xiàn)這里有一個(gè)chunked看一下chunked是什么意思HTTP響應(yīng)chunked格式分析https://blog.csdn.net/zztfj/article/details/5906168Plain Embedded Zuulhttps://cloud.spring.io/spring-cloud-netflix/reference/html/#_plain_embedded_zuul我們知道@EnableZuulProx,整合了Eureka,做了反向代理,我們有的時(shí)候并不想讓Zuul做太多的事情,用@EnableZuulServer,這個(gè)是更輕量的Zuul,它是一個(gè)空的Zuulorg.springframework.cloud.netflix.zuul.EnableZuulServer/*** Set up the application to act as a generic Zuul server without any built-in reverse* proxy features. The routes into the Zuul server can be configured through* {@link ZuulProperties} (by default there are none).** @see EnableZuulProxy to see how to get reverse proxy out of the box** @author Spencer Gibb*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ZuulConfiguration.class)
public @interface EnableZuulServer {}構(gòu)建了一個(gè)Zuul,但是不包含任何內(nèi)建的反向代理的特性org.springframework.cloud.netflix.zuul.filters.ZuulProperties你可以使用這個(gè)自己去配https://github.com/spring-cloud/spring-cloud-netflix/issues/1447I cannot figure outhttps://github.com/spring-cloud/spring-cloud-netflix/issues/1477When should I use @EnableZuulServer? #1477@spencergibb I know it, but I cannot figure out when to use a blank zuul.It is blank zuul with no filters installed.Disable Zuul Filtershttps://cloud.spring.io/spring-cloud-netflix/reference/html/#_disable_zuul_filters你可以指定某個(gè)filter,Zuul大部分功能都是基于filter的,這個(gè)filter不是servlet的filter,public class DebugFilter extends ZuulFilter {filter type有pre,post,root
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn.cloud</groupId><artifactId>microservice-file-upload</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties> <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><!-- 這個(gè)插件,可以將應(yīng)用打包成一個(gè)可執(zhí)行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server.port=8050
eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka
eureka.instance.prefer-ip-address=true
spring.application.name=microservice-file-upload
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
spring.application.http.multipart.max-file-size=2000Mb
spring.application.http.multipart.max-request-size=2000Mbmanagement.security.enabled=falselogging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n
package com.learn.cloud.controller;import java.io.File;
import java.io.IOException;import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@RestController
public class FileUploadController {/*** 上傳文件* 測(cè)試方法:* 有界面的測(cè)試:http://localhost:8024/index.html* 使用命令:curl -F "file=@文件全名" localhost:8050/upload* ps.該示例比較簡(jiǎn)單,沒(méi)有做IO異常、文件大小、文件非空等處理** @param file 待上傳的文件* @return 文件在服務(wù)器上的絕對(duì)路徑* @throws IOException IO異常*/@PostMapping("/upload")String handleFileUpload(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {byte[] bytes = file.getBytes();File fileToSave = new File(file.getOriginalFilename());FileCopyUtils.copy(bytes, fileToSave);return fileToSave.getAbsolutePath();}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><!--<form method="POST" enctype="multipart/form-data" action="http://localhost:8040/microservice-file-upload/upload">--><form method="POST" enctype="multipart/form-data" action="/upload">File to upload:<input type="file" name="file"><input type="submit" value="Upload"></form>
</body>
</html>
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient
@SpringBootApplication
public class RegisterFileUploadZuulApplication {public static void main(String[] args) {SpringApplication.run(RegisterFileUploadZuulApplication.class, args);}
}
?
總結(jié)
以上是生活随笔為你收集整理的通过Zuul上传文件,禁用Zuul的Filters的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。