当前位置:网站首页>微服务实战|微服务网关Zuul入门与实战
微服务实战|微服务网关Zuul入门与实战
2022-07-02 06:33:00 【_时光煮雨】
网关的作用
微服务架构中,服务实例的地址可能经常会发生变化,所以我们不能直接将服务的地址暴露出来。如果每一个微服务都直接暴露接口,会导致一系列的问题,比如调用过于复杂,涉及到账户、权限不能统一处理等。另外基于高内聚低耦合的设计准则来讲,我们也应该将内部系统和外部系统做切割。
因此,这时就需要有一个独立的组件来处理外部的请求,这个组件就是服务网关。服务网关就是为了简化前端的调用逻辑,通常情况下也会实现相关的认证逻辑,根据外部不同的请求响应不同的数据,从而简化内外部系统之间调用的复杂度。
服务网关负责服务请求路由、组合及协议转换。客户端的所有请求都首先经过服务网关,然后由它将请求路由到合适的微服务。服务网关经常会通过调用多个微服务并合并结果来处理一个请求,它可以在系统外部与内部响应之间友好的转换。
本文将先着重介绍Zuul组件。
Spring Cloud 网关组件Zuul介绍
Zuul 是 Netflix 公司开源的产品,被称为第一代网关,也是 Spring Cloud 前几个版本默认使用的一款提供动态路由微服务网关组件。Zuul 接收所有外来请求,并将请求转发到对应的后端服务。作为一个前置服务,Zuul 旨在实现动态路由,监控,弹性和安全性等功能。
Zuul 提供了不同类型的 filter 用于处理请求,这些 filter 可以让我们实现以下功能:
- 权限控制和安全性:可以识别认证需要的信息和拒绝不满足条件的请求。
- 监控:与边缘位置追踪有意义的数据和统计结果,从而带来精确的生产视图。
- 动态路由:根据需要动态地路由请求到后台的不同集群。
- 压力测试:逐渐增加指向集群的流量,以了解性能。
- 负载均衡:为每一种负载类型分配对应容量,并弃用超出限定值的请求
- 静态资源处理:直接在 zuul 处理静态资源的响应,从而避免其转发到内部集群。
Zuul网关实战
之前的介绍中,我们启动了注册中心registry,dms服务,和app服务,请求时,直接调用了app服务的接口,app服务又调用了dms服务:
加上网关服务之后,就会变成如下:
接下来,我们创建zuul服务:
1、创建服务
创建子模块zuul,pom.xml引入eureka-client 和zuul的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- zuul路由 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
2、创建配置文件
server:
port: 8004
spring:
application:
name: zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
feign:
hystrix:
enabled: true
zuul:
routes:
app: #活动
path: /app/** #配置请求URL的请求规则
serviceId: app #指定Eureka注册中心中的服务id
dms: #活动
path: /dms/** #配置请求URL的请求规则
serviceId: dms #指定Eureka注册中心中的服务id
logging:
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
3、创建Zuul过滤器
/** * @Author:公众号:程序员965 * @create 2022-06-30 **/
@Component
public class MyZuulFilter extends ZuulFilter {
// pre :可以在请求被路由之前调用
// route :在路由请求时候被调用
// post :在route和error过滤器之后被调用
// error :处理请求时发生错误时被调用
@Override
public String filterType() {
return "pre";// 前置过滤器
}
//通过int值来定义过滤器的执行顺序,优先级为0,数字越大,优先级越低
@Override
public int filterOrder() {
return 0;
}
//返回一个boolean类型来判断该过滤器是否要执行,所以通过此函数可实现过滤器的开关
@Override
public boolean shouldFilter() {
return true;//此处为true,说明需要过滤
}
//过滤器的具体逻辑。
@Override
public Object run() throws ZuulException {
// 获取上下文
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
String accessToken = request.getParameter("accessToken");
if (StringUtils.isEmpty(accessToken)) {
//setSendZuulResponse(false)令zuul过滤该请求,不进行路由
currentContext.setSendZuulResponse(false);
//设置返回的错误码
currentContext.setResponseStatusCode(401);
currentContext.setResponseBody("AccessToken is null");
return null;
}
System.out.println("获取到AccessToken为:"+accessToken);
// 否则正常执行业务逻辑.....
return null;
}
}
4、编写启动类
注意增加@EnableZuulProxy注解
/** * @Author:公众号:程序员965 * @create 2022-06-30 **/
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
5、启动验证
这个时候,我们将直接请求app的接口地址,改成网关zuul的地址和端口:http://localhost:8004/app/index
可以看到,请求中没有token访问授权认证失败!
总结
总结下网关的作用:
- 简化客户端调用的复杂度,统一处理外部请求。
- 数据的裁剪和聚合,根据不同的接口需求,对数据加工后对外提供接口
- 多平台的支持,对不同的客户端提供不同的网关支持。
- 遗留系统的微服务化改造,可以作为新老系统的中转组件。
- 统一处理调用过程中的安全、权限问题。
边栏推荐
- Multi version concurrency control mvcc of MySQL
- Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
- [go practical basis] how to bind and use URL parameters in gin
- Minecraft plug-in service opening
- Programmers with ten years of development experience tell you, what core competitiveness do you lack?
- 长篇总结(代码有注释)数构(C语言)——第四章、串(上)
- Gocv split color channel
- 盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
- gocv opencv exit status 3221225785
- C4D quick start tutorial - Chamfer
猜你喜欢
Hengyuan cloud_ Can aiphacode replace programmers?
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
Minecraft module service opening
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
QT -- how to set shadow effect in QWidget
Minecraft plug-in service opening
Cloud computing in my eyes - PAAS (platform as a service)
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
随机推荐
Solution and analysis of Hanoi Tower problem
一、Qt的核心类QObject
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
Finishing the interview essentials of secsha system!!!
C#钉钉开发:取得所有员工通讯录和发送工作通知
将一串数字顺序后移
WSL安装、美化、网络代理和远程开发
C# 将网页保存为图片(利用WebBrowser)
小米电视不能访问电脑共享文件的解决方案
整理秒杀系统的面试必备!!!
Right click menu of QT
【Go实战基础】如何安装和使用 gin
Select sort and insert sort
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
【Go实战基础】gin 如何验证请求参数
There is a problem with MySQL installation (the service already exists)
选择排序和插入排序
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
cmd窗口中中文呈现乱码解决方法