API Gateway封裝內部系統架構,提供API供客戶端使用。涵蓋功能點,如受權,監控,負載均衡,緩存,請求分片和管理,靜態響應處理,請求聚合等。java
很好的封裝了內部應用結構,交互更簡單,減小了客戶端和服務端通訊次數。git
須要是一個高可用的組件,必需要開發,部署和管理,可能成爲開發瓶頸。API Gateway的更新必須越輕量級越好。github
傳統架構:spring
引入eureka:緩存
最終:安全
經過Ribbon進行負載均衡;springboot
經過繼承ZuulFilter實現請求過濾;架構
package com.springbootdemo.eureka.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; /** * Created by Administrator on 2017/4/18. * * 安全過濾器,驗證請求中是否有accessToken參數 */ public class AccessFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(AccessFilter.class); /** * 表明不一樣的過濾類型: * pre:請求以前調用; * routing:在路由請求時調用; * post:在routing和error過濾器以後被調用; * error:在處理錯誤是調用; * @return */ @Override public String filterType() { return "pre"; } /** * 經過int值定義過濾器執行順序; * @return */ @Override public int filterOrder() { return 0; } /** * 返回boolean類型判斷過濾器是否執行; * @return */ @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString())); Object accessToken = request.getParameter("accessToken"); if(accessToken == null) { log.warn("access token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); return null; } log.info("access token ok"); return null; } }
ZUUL代碼負載均衡