在微服務架構中,一個系統會有多個微服務,例如service-user, service-shop,一個功能每每須要調用多個微服務來完成,zuul實現的網關相似於設計模式中的門面模式Facade,一般在網關層實現功能:java
- 認證 來源的合法性校驗
- 動態路由 隱藏內部接口,經過調用所配置路徑轉發至各微服務接口
- 安全 權限校驗
- 服務遷移 簡單化因業務變化而須要拆分/整合服務的工做git
zuul 像其餘微服務同樣做爲一個微服務向eureka server註冊,而且可以經過註冊列表獲取全部的可用服務,其內部默認實現了ribbon,可達到負載均衡的目的,此時的微服務架構圖以下:github
不管是pc仍是移動端調用首先通過zuul網關層,在這裏會有一系列的filter(例如權限控制,接口認證等),驗證合法後zuul放行並調用相關的微服務接口,在此架構中,須要說明一般的部署方法:zuul網關在系統的最外層暴露映射後的地址供外部客戶端調用,而使用內部私有地址調用各微服務spring
zuul的配置仍是放到配置中心統一管理,所以須要加入配置客戶端、eureka客戶端和zuul 依賴bootstrap
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
spring: application: name: service-zuul cloud: config: discovery: enabled: true service-id: service-config label: master profile: dev
server: port: 1100 zuul: ignoredServices: '*' #忽略全部未配置的service host: connect-timeout-millis: 20000 socket-timeout-millis: 20000 routes: user-service: #自定義名稱 path: /user/** serviceId: service-user #/user/開頭的路徑轉發至service-user微服務 hystrix: #hystrix配置 command: default: execution: isolation: thread: timeoutInMilliseconds: 2500 ribbon: #ribbon負載均衡參數配置 ReadTimeout: 5000 ConnectTimeout: 5000 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
在啓動類CommonserviceZuulApplication添加註解@EnableZuulProxy以開啓zuul特性:設計模式
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class CommonserviceZuulApplication { public static void main(String[] args) { SpringApplication.run(CommonserviceZuulApplication.class, args); } }
建立一個熔斷類:UserFallbackProvider, 代碼取自官方文檔最簡單實現安全
package com.mayi.springcloud.fallback; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; @Component public class UserFallbackProvider implements FallbackProvider{ @Override public String getRoute() { return "service-user"; } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { return new ClientHttpResponse() { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("fallback".getBytes()); } @Override public String getStatusText() throws IOException { return "OK"; } @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return 200; } @Override public void close() { } }; } }
依次啓動eureka、config、user、zuul ,user啓動兩個節點微信
訪問:http://localhost:1100/user/listUsers 架構
發現映射到了service-user微服務的listUsers接口,並實現了負載均衡。app
此時,斷掉其中一個service-user節點,繼續訪問以上地址
數秒後,故障節點被剔除
源碼下載:https://github.com/tianyana/s...
歡迎加入JAVA架構師QQ羣(初建):618578034
關注 微信公衆號:java架構師修行
按照JAVA高級軟件架構師實戰培訓的路線發佈一期完整的架構文章,難度由淺入深,適合有必定開發基礎想轉架構和正在作初級架構開發的人員學習(springcloud更新完畢即開始)