11.1 服務網關的概念
11.1.1 什麼是微服務網關
11.1.2作用和應用場景
11.2 常見的API網關實現方式
11.3 基於Nginx的網關實現
IP地址映射到路徑,統一管理
11.4 Zuul網關
11.4.1 Zuul簡介
11.4.2 搭建Zuul微服務網關
1 創建工程導入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
2 配置啓動類,開啓網關服務器功能
package xx.study.sc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
//開啓zuul網關功能
@EnableZuulProxy
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class);
}
}
3 配置文件
server.port=8080
spring.application.name=api-zuul-server
11.4.3 路由
路由:根據請求的URl匹配到不同的微服務
1 基礎路由配置
#路由配置
#product-service路由id 隨便寫;/product-service/** 映射路徑 localhost:8080/product-service/xxxx
zuul.routes.product-service.path=/product-service/**
#映射路徑對應的實際微服務地址 http://localhost:8080/product-service/product/buy?name=apple
zuul.routes.product-service.url=http://127.0.0.1:9001
2 面向服務的路由
a 添加eureka的依賴
<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>
b 開啓eureka的客戶端發現
package xx.study.sc;
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
//開啓zuul網關功能
@EnableZuulProxy
@EnableEurekaClient
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class);
}
}
c 在zuul網關服務中配置eureka的註冊中心相關信息
#註冊中心訪問地址
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
d 修改路由中的映射路徑
#product-service路由id 隨便寫;/product-service/** 映射路徑 localhost:8080/product-service/xxxx
zuul.routes.product-service.path=/product-service/**
#映射路徑對應的實際微服務地址
#zuul.routes.product-service.url=http://127.0.0.1:9001
#配置轉發的微服務名稱
zuul.routes.product-service.service-id=service-product
3 簡化配置
#簡化配置
# 如果路由id和服務名稱一致的話可進行如下配置
zuul.routes.service-product=/product-service/**
#zuul的默認路由 不用配置可直接訪問註冊中心的服務 如果當前服務名稱service-product 默認的請求映射路徑 /service-product/**
11.4.4 Zuul加入後的架構
11.5 Zuul 過濾器
11.5.1 ZuulFilter 簡介
11.5.2 過濾器訪問流程
11.5.3 過濾器的實現
package xx.study.sc.fitter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
@Component
public class LoginFilter extends ZuulFilter {
/**
* 定義過濾器類型
* pre 轉發到微服務之前執行的過濾器
* routing
* post
* error
* @return
*/
public String filterType() {
return "pre";
}
/**
* 指定過濾器的執行順序 返回值越小 執行順序越高
* @return
*/
public int filterOrder() {
return 1;
}
/**
* 當前過濾器是否生肖
* true :生效
* @return
*/
public boolean shouldFilter() {
return true;
}
/**
* 指定過濾器的業務邏輯
* @return
* @throws ZuulException
*/
public Object run() throws ZuulException {
System.out.println("進入過濾器了!!!");
return null;
}
}
11.5.4 案例用戶登錄
簡單代碼實現
package xx.study.sc.fitter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@Component
public class LoginFilter extends ZuulFilter {
/**
* 定義過濾器類型
* pre 轉發到微服務之前執行的過濾器
* routing
* post
* error
* @return
*/
public String filterType() {
return "pre";
}
/**
* 指定過濾器的執行順序 返回值越小 執行順序越高
* @return
*/
public int filterOrder() {
return 1;
}
/**
* 當前過濾器是否生肖
* true :生效
* @return
*/
public boolean shouldFilter() {
return true;
}
/**
* 指定過濾器的業務邏輯
* 身份驗證
* 1 所有的請求需要攜帶一個參數 access-token
* 2 獲取request請求
* 3 通過request 獲取參數access-token
* 4 判斷token是否爲空
* 4.1 token==null 身份驗證失敗
* 4.2 token!=null 執行後續操作
* 在Zuul網關中,通過RequestContext的上下文對象,可以獲取對象request對像
*
* @return
* @throws ZuulException
*/
public Object run() throws ZuulException {
//System.out.println("進入過濾器了!!!");
RequestContext ctx=RequestContext.getCurrentContext();
HttpServletRequest requst=ctx.getRequest();
String token=requst.getParameter("access-token");
if (token==null){
ctx.setSendZuulResponse(false);//攔截請求
ctx.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
}
return null;//返回null 繼續運行
}
}
11.6內部源碼
11.7 Zuul網關存在問題
11.8 Zuul替換方案
Zuul2.x版本 SpringCloud不支持;
SpringCloud Gateway 可以替換,後續講解。