八:Spring Cloud 之服务路由网关-zuul
1. 简介2. 代码实现2.1涉及的模块及关系图谱2.1.1 涉及的模块2.1.2 模块关系图谱2.2 源代码2.2.1 Github地址2.2.2 切换2.3 eureka-server-singleton2.4 eureka-service2.5 eureka-service-feign2.5 eureka-service-ribbon2.6 eureka-...
1. 简介
Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.
Zuul 是在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。
2. 代码实现
2.1涉及的模块及关系图谱
2.1.1 涉及的模块
- eureka-server-singleton:服务注册中心,端口8761
- eureka-service: 服务提供者,通过profile指定不同端口模拟一组微服务,端口8762、8763
- eureka-service-feign:通过Feign调用服务提供者提供的服务
- eureka-service-ribbon:通过Feign调用服务提供者提供的服务
- eureka-service-zuul:服务路由功能,实现请求路由,网关功能
2.1.2 模块关系图谱
2.2 源代码
2.2.1 Github地址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 切换
通过tag切换git tag -d v1.0,若想修改,可根据此tag创建新的分支。
2.3 eureka-server-singleton
与Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别
2.4 eureka-service
2.5 eureka-service-feign
与五:Spring Cloud 之服务发现与调用-Feign#2.5 eureka-service-feign没有任何区别
2.6 eureka-service-ribbon
与四:Spring Cloud 之服务发现与调用-Ribbon#2.5 eureka-service-ribbon 没有任何区别
2.7 eureka-service-zuul
2.7.1 整体实现
- pom.xml中引入
spring-cloud-starter-netflix-eureka-client
和spring-cloud-starter-netflix-zuul
依赖 - application.yml中指定配置项
- 端口:8768
- 应用名称:eureka-serivce-zuul
- 指定路由规则:
- 将
/api-ribbon/**
请求转发到eureka-service-ribbon
处理 - 将
/api-feign/**
请求转发到eureka-serivce-feign
处理
- 将
- SpringBoot 启动类添加启用Zuul的注解
@EnableZuulProxy
- 添加继承
ZuulFilter
的Filter,并指定filterType实现请求过滤 - 根据路由规则访问访问API网关,观察请求是否正确传递到真正服务提供者处理并返回
2.7.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-service-zuul</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
</project>
2.7.3 application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8768
spring:
application:
name: eureka-serivce-zuul
zuul:
routes:
api-ribbon:
path: /api-ribbon/**
serviceId: eureka-service-ribbon
api-feign:
path: /api-feign/**
serviceId: eureka-serivce-feign
2.7.4 EurekaServiceZuulApplication
package org.oscar.scd.eureka.service.ribbon.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceZuulApplication.class, args);
}
}
2.7.5 PreFilter
使用了lombok插件,IDEA需要安装一下lombok插件。见补充部分
package org.oscar.scd.eureka.service.ribbon.zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@Service
public class PreFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
return "yes".equals(request.getParameter("shouldFilter"));
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String token = request.getParameter("token");
log.info("Token:{}", token);
return null;
}
}
3. 验证
3.1 创建SpringBoot启动类
3.1.1 EurekaServerSingletonApplication
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.1 EurekaServerSingletonApplication 完全一致
3.1.2 EurekaServiceApplication-8762
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.2 EurekaServiceApplication-8762 完全一致
3.1.3 EurekaServiceApplication-8763
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.3 EurekaServiceApplication-8763完全一致
3.1.4 EurekaServiceFeignApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
3.1.5 EurekaServiceRibbonApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
###3.1.6 EurekaServiceZuulApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
3.2 启动
EurekaServerSingletonApplication
EurekaServiceApplication-8762
EurekaServiceApplication-8763
EurekaServiceFeignApplication
EurekaServiceRibbonApplication
EurekaServiceZuulApplication
启动完成后Run Dashboard界面:
3.3 访问服务信息界面
3.4 调用服务
3.4.1 访问 /api-ribbon/
- 多次访问 http://localhost:8768/api-ribbon/ribbon/print?name=oscar ,见到如下两图返回结果切换,表示路由正常工作,且负载均衡功能起作用
3.4.2 访问 /api-feign/
- 多次访问 http://localhost:8768/api-ribbon/ribbon/print?name=oscar ,见到如下两图返回结果切换,表示路由正常工作,且负载均衡功能起作用
3.4.3 验证过滤器生效
3.4.3.1 不生效
访问:http://localhost:8768/api-ribbon/ribbon/print?name=oscar&shouldFilter=no
3.4.3.2 生效
访问:http://localhost:8768/api-ribbon/ribbon/print?name=oscar&shouldFilter=yes
4. 思考
- zuul过滤器使用场景
- 如何做分流
- 能不能动态指定路由规则
- 处理请求的整体流程
- 如何做高可用
5. 补充
5.1 资料
5.2 Zuul的Filter类型
- pre:可以在请求被路由之前调用
- route:在路由请求时候被调用
- post:在route和error过滤器之后被调用
- error:处理请求时发生错误时被调用
5.2 lombok插件安装
打开IDEA的settings,按下图所示搜索并安装Lombok Plugin:
更多推荐
所有评论(0)