Netflix uses Zuul for the following: Netflix使用Zuul進(jìn)行以下操作:
Authentication 認(rèn)證
Insights洞察
Stress Testing 壓力測試
Canary Testing 金絲雀測試
Dynamic Routing 動態(tài)路由
Service Migration 服務(wù)遷移
Load Shedding 加載脫落
Security 安全
Static Response handling 靜態(tài)響應(yīng)處理
Active/Active traffic management 主動/主動流量管理
1.添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
zuul也需要注冊到eureka
2.修改啟動類
添加注解@EnableZuulProxy
我們來看看@EnableZuulProxy
里面都包含了什么注解
有
@EnableCircuitBreaker
實現(xiàn)斷路器@EnableDiscoveryClient
實現(xiàn)eureka用戶端注冊
3.application.yml
和普通eureka客戶端配置一樣
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka
#加入密碼驗證
security:
basic:
enabled: true
user:
name: laojiao
password: laojiao
spring:
application:
name: gateway-zuul
4.啟動 查看zuul組件是否添加成功
- 啟動eureka
- 啟動user3服務(wù)
-
啟動zuul服務(wù)
訪問user3服務(wù)
通過zuul訪問user3服務(wù)
5. 自定義訪問前綴
zuul:
ignoredPatterns: /**/admin/** #忽略某個服務(wù)的訪問
routes:
provider-user3: /user/**
或者 serviceId模式
zuul:
routes:
users:
path: /user/**
serviceId: provider-user3
或者 url 模式(不能實現(xiàn)ribbon的負(fù)載均衡和hystrix斷路器)
zuul:
routes:
users:
path: /user/**
url: http://127.0.0.1:7904
但是要注意一點:
These simple url-routes don’t get executed as a HystrixCommand nor can you loadbalance multiple URLs with Ribbon.
To achieve this, specify a service-route and configure a Ribbon client for the serviceId (this currently requires disabling Eureka support in Ribbon: see above for more information), e.g.
這些簡單的url路由不會作為HystrixCommand執(zhí)行,也不能使用Ribbon來負(fù)載多個URL陪汽。
為此诡蜓,請指定服務(wù)路由并為serviceId配置功能區(qū)客戶端(目前需要在功能區(qū)中禁用Eureka支持:有關(guān)詳細(xì)信息闸迷,請參閱上文)晤斩。
zuul:
routes:
users:
path: /user/**
serviceId: provider-user3
ribbon:
eureka:
enabled: false
provider-user3:
ribbon:
listOfServers: http://127.0.0.1:7904,http://127.0.0.1:7902
這樣配置后柳沙。我們啟動7902盛险,然后訪問
然后看看兩個user3服務(wù) 日志打印 是否做到負(fù)載均衡:
6. /routes 使用
訪問zuul服務(wù)下的/routes
僧家,可以查看代理的服務(wù)路徑列表雀摘。
7.Strangulation Patterns and Local Forwards(請求轉(zhuǎn)發(fā))
Zuul代理是一個有用的工具,因為您可以使用它來處理來自舊端點的客戶端的所有流量八拱,但將一些請求重定向到新端點阵赠。
application.yml
zuul:
routes:
first:
path: /first/**
url: http://first.example.com
second:
path: /second/**
url: forward:/second
third:
path: /third/**
url: forward:/3rd
legacy:
path: /**
url: http://legacy.example.com
解釋:
在這個例子中,我們正在扼殺映射到不匹配其他模式的所有請求的“遺留”應(yīng)用程序肌稻。
/ first / **中的路徑已被提取到具有外部URL的新服務(wù)中清蚀。
并且/ second / **中的路徑被禁止,所以它們可以在本地處理爹谭,例如枷邪, 與一個正常的Spring @RequestMapping。
在/ third / **中的路徑也被轉(zhuǎn)發(fā)诺凡,但是具有不同的前綴(即/ third / foo被轉(zhuǎn)發(fā)到/ 3rd / foo)东揣。
注意:被忽略的模式并不完全被忽略,它們只是不被代理處理(所以它們也被有效地本地轉(zhuǎn)發(fā))腹泌。
7.Uploading Files through Zuul(文件上傳)
如果您使用@EnableZuulProxy嘶卧,則可以使用代理路徑上傳文件,只要文件很小凉袱,就可以使用芥吟。
對于大文件,在“/ zuul / ”中有一條繞過*Spring DispatcherServlet(避免多部分處理)的替代路徑绑蔫。
例子:我想通過upload服務(wù)上傳文件运沦,我在upload的配置文件設(shè)置允許的最大文件為2500Mb泵额,但是如果通過zuul來上傳http://super-john:8040/file-upload/upload
的話配深,如果文件超過10MB,zuul會直接擋掉(因為用的是zuul的默認(rèn)值);所以我可以在請求前綴上加個zuul即:http://super-john:8040/zuul/file-upload/upload
servlet路徑通過zuul.servletPath進(jìn)行外部化嫁盲。
因為zuul默認(rèn)通過hystrix和ribbon篓叶,所以極大的文件也需要提升超時設(shè)置 烈掠。
- hystrix和ribbon的超時設(shè)置
application.yml
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
8.禁用Zuul過濾器
For example to disable org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter
set zuul.SendResponseFilter.post.disable=true.
9.Providing Hystrix Fallbacks For Routes(提供Hystrix路由回退)
當(dāng)Zuul中給定路由的電路被跳閘時,可以通過創(chuàng)建一個類型為ZuulFallbackProvider的bean來提供回退響應(yīng)缸托。 在這個bean中左敌,你需要指定fallback的路由ID,并提供一個ClientHttpResponse作為回退俐镐。 這是一個非常簡單的ZuulFallbackProvider實現(xiàn)
class MyFallbackProvider implements ZuulFallbackProvider {
@Override
public String getRoute() {
return "customers";
}
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("fallback".getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}
其中
public String getRoute() {
return "customers";
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("fallback".getBytes());
}
customers
改為自己 微服務(wù)的idname provider-user3
new ByteArrayInputStream("fallback".getBytes());
里自定義返回內(nèi)容
- And here is what the route configuration would look like.
zuul:
routes:
provider-user3: /user/**