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 创建工程导入依赖
org.springframework.cloudspring-cloud-starter-netflix-zuul
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的依赖
org.springframework.cloudspring-cloud-starter-netflix-zuulorg.springframework.cloudspring-cloud-starter-netflix-eureka-client
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 可以替换,后续讲解。