SpringCloud(第 024 篇)簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現文件上傳
一、大致介紹
1、本章節主要將文件上傳微服務加入到 zuul 服務中去,然后利用 zuul 微服務的地址上傳文件;
二、實現步驟
2.1 添加 maven 引用包
<?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>
<artifactId>springms-gateway-zuul-file-upload</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.springms.cloud</groupId>
<artifactId>springms-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- API網關模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 客戶端發現模塊,由于文檔說 Zuul 的依賴里面不包括 eureka 客戶端發現模塊,所以這里還得單獨添加進來 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
2.2 添加應用配置文件(springms-gateway-zuul-file-upload\src\main\resources\application.yml)
spring:
application:
name: springms-gateway-zuul-file-upload
server:
port: 8195
eureka:
datacenter: SpringCloud # 修改 http://localhost:8761 地址 Eureka 首頁上面 System Status 的 Data center 顯示信息
environment: Test # 修改 http://localhost:8761 地址 Eureka 首頁上面 System Status 的 Environment 顯示信息
client:
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka
healthcheck: # 健康檢查
enabled: true
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
#####################################################################################################
# 打印日志
logging:
level:
root: INFO
com.springms: DEBUG
com.netflix: debug
#####################################################################################################
#####################################################################################################
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
#####################################################################################################
#####################################################################################################
# 解決第一次請求報超時異常的方案,因為 hystrix 的默認超時時間是 1 秒,因此請求超過該時間后,就會出現頁面超時顯示 :
#
# 這里就介紹大概三種方式來解決超時的問題,解決方案如下:
#
# 第一種方式:將 hystrix 的超時時間設置成 60000 毫秒,因為文件上傳需要的超時時間稍微長一點點
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
#
# 或者:
# 第二種方式:將 hystrix 的超時時間直接禁用掉,這樣就沒有超時的一說了,因為永遠也不會超時了
# hystrix.command.default.execution.timeout.enabled: false
#
# 或者:
# 第三種方式:索性禁用feign的hystrix支持
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持
# 超時的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超時的解決方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
#####################################################################################################
2.3 添加zuul服務啟動類(springms-gateway-zuul-file-upload\src\main\java\com\springms\cloud\MsGatewayZuulFileUploadApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* 簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現文件上傳。
*
* 提供給 springms-file-upload 文件上傳微服務用的。
*
* 注意 EnableZuulProxy 注解能注冊到 eureka 服務上,是因為該注解包含了 eureka 客戶端的注解,該 EnableZuulProxy 是一個復合注解。
*
* @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客戶端注解,同時也包含了 Hystrix 斷路器模塊注解。
*
* http://localhost:8195/routes 地址可以查看該zuul微服務網關代理了多少微服務的serviceId。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/26
*
*/
@SpringBootApplication
@EnableZuulProxy
public class MsGatewayZuulFileUploadApplication {
public static void main(String[] args) {
SpringApplication.run(MsGatewayZuulFileUploadApplication.class, args);
System.out.println("【【【【【【 GatewayZuulFileUpload微服務 】】】】】】已啟動.");
}
}
三、測試
/****************************************************************************************
一、簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現文件上傳(加入 zuul 微服務,頁面上傳文件):
1、啟動 springms-discovery-eureka 模塊服務,啟動1個端口;
2、啟動 springms-file-upload 模塊服務;
3、啟動 springms-gateway-zuul-file-upload 模塊服務;
4、新起網頁頁簽,輸入 http://localhost:8190/index.html 正常情況下是能看到選擇文件上傳的界面;
5、新起網頁頁簽,輸入 http://localhost:8195/springms-file-upload/index.html 正常情況下是能看到選擇文件上傳的界面;
6、選擇文件,然后點擊 upload 上傳文件,上傳成功后,直接返回上傳成功文件所在的磁盤全路徑;
****************************************************************************************/
/****************************************************************************************
二、簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現文件上傳(加入 zuul 微服務,然后采用命令上傳文件):
1、啟動 springms-discovery-eureka 模塊服務,啟動1個端口;
2、啟動 springms-file-upload 模塊服務;
3、啟動 springms-gateway-zuul-file-upload 模塊服務;
4、進入 curl.exe 所在的目錄,嘗試 curl.exe www.baidu.com 看看是否正常,正常情況下會打印百度首頁的一堆信息;
5、執行命令:curl.exe -F "file=@文件名稱" localhost:8195/springms-file-upload/upload
6、正常情況下,第6步驟執行后,直接返回上傳成功文件所在的磁盤全路徑;
****************************************************************************************/
/****************************************************************************************
三、簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現文件上傳文件上傳微服務(加入 zuul 微服務,然后采用命令上傳文件,傳送大文件,文件大小大于 max-file-size 這個配置的屬性值):
1、啟動 springms-discovery-eureka 模塊服務,啟動1個端口;
2、啟動 springms-file-upload 模塊服務;
3、啟動 springms-gateway-zuul-file-upload 模塊服務;
4、進入 curl.exe 所在的目錄,嘗試 curl.exe www.baidu.com 看看是否正常,正常情況下會打印百度首頁的一堆信息;
5、執行命令:curl.exe -F "file=@文件名稱大于50M" localhost:8195/springms-file-upload/upload
6、結果第6步驟執行后,報錯提示文件太大,報錯信息為:the request was rejected because its size (36611267) exceeds the configured maximum (10485760)
7、然而在zuul的集群中,可以饒過SpringMvc的檢測,執行命令:curl.exe -F "file=@文件名稱大于50M" localhost:8195/zuul/springms-file-upload/upload
8、結果第7步驟執行后,報錯提示timeout超時,報錯信息為:{"timestamp":1503932607944,"status":500,"error":"Internal Server Error","exception":"com.netflix.zuul.exception.ZuulException","message":"GENERAL"};
9、發現采用了各種辦法好像還是沒有解決,難道是最近版本 SpringCloud 解決了這樣的一個饒過SpringMvc請求訪問的bug了么???
****************************************************************************************/
四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關注,您的肯定是對我最大的支持!!!