1、搭建一個eureka-server註冊中心工程 該工程比較簡潔,沒有太多配置,不在描述,單節點,服務埠:8888 2、創建zuul-gateway網關工程 2.1、工程pom依賴 2.2、工程配置文件:zuul-gateway\src\main\resources\bootstrap.yml ...
1、搭建一個eureka-server註冊中心工程
該工程比較簡潔,沒有太多配置,不在描述,單節點,服務埠:8888
2、創建zuul-gateway網關工程
2.1、工程pom依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
2.2、工程配置文件:zuul-gateway\src\main\resources\bootstrap.yml
spring: application: name: zuul-gateway servlet: #spring boot2.0之前是http multipart: enabled: true # 使用http multipart上傳處理 max-file-size: 100MB # 設置單個文件的最大長度,預設1M,如不限制配置為-1 max-request-size: 100MB # 設置最大的請求文件的大小,預設10M,如不限制配置為-1 file-size-threshold: 10MB # 當上傳文件達到10MB的時候進行磁碟寫入 location: / # 上傳的臨時目錄 server: port: 5555 eureka: client: serviceUrl: defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/ instance: prefer-ip-address: true ##### Hystrix預設超時時間為1秒,如果要上傳大文件,為避免超時,稍微設大一點 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 30000 ribbon: ConnectTimeout: 3000 ReadTimeout: 30000
註意:
SpringBoot版本之間有很大的變化,1.4.x 版本前:
multipart: enabled: true max-file-size: 100MB
1.5.x - 2.x 之間:
spring: http: multipart: enabled: true max-file-size: 100MB
2.x 之後:
spring: servlet: multipart: enabled: true max-file-size: 100MB
2.3、網關工程啟動類:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class ZuulServerApplication { public static void main(String[] args) { SpringApplication.run(ZuulServerApplication.class, args); } }
2.4、上傳代碼:
import java.io.File; import java.io.IOException; import org.springframework.stereotype.Controller; 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.ResponseBody; import org.springframework.web.multipart.MultipartFile; @Controller public class ZuulUploadController { @PostMapping("/upload") @ResponseBody public String uploadFile(@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(); } }
3、測試,上傳成功返迴文件絕對路徑: