内容简介:Hello大家好,我是初晨,本章我们学习SpringCloud 服务网关Zuul的使用。大家有问题和意见可以发邮箱mr_beany@163.comZuul是Spring Cloud服务系列中的微服务API网关。Zuul的核心是一系列的
Hello大家好,我是初晨,本章我们学习SpringCloud 服务网关Zuul的使用。大家有问题和意见可以发邮箱mr_beany@163.com
一:什么是Zuul
Zuul是Spring Cloud服务系列中的微服务API网关。
Zuul的核心是一系列的 filters , 其作用可以类比Servlet框架的Filter,或者AOP。
所有的请求都会经过Zuul的验证之后到达其他各个服务。作为一个边界性质的应用程序,Zuul提供了动态路由、监控、弹性负载和安全功能。Zuul底层利用各种filter实现如下功能:
- 认证和安全 识别每个需要认证的资源,拒绝不符合要求的请求。
- 性能监测 在服务边界追踪并统计数据,提供精确的生产视图。
- 动态路由: 以动态方式根据需要将请求路由至不同后端集群处。
- 压力测试: 逐渐增加指向集群的负载流量,从而计算性能水平。
- 负载卸载 预先为每种类型的请求分配容量,当请求超过容量时自动丢弃。
- 静态资源处理 直接在边界返回某些响应。
二:创建服务网关
1:创建过程与 SpringCloud 实战二:Client的创建和高可用 一样
2:打开pom文件,添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>复制代码
3:修改启动类,添加 @EnableZuulProxy
4:修改配置文件名称为bootstrap.yml
spring:
application:
name: api-gateway
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: dev复制代码
5:打开git仓库,创建配置文件
6:启动服务,访问 http://localhost:8761/
可以看到网关服务已经注册成功。
7:通过网关服务的路由来访问其他服务接口
我们来访问前面为创建组件间通信创建的 http://localhost:8081/getServerResult
可以访问,再通过网关访问 http://localhost:8085/client/getServerResult
其中8085是服务网关的ip,client代表着client服务,getServerResult代表访问路径
8:自定义路由
通过Zuul每次访问client服务时都需要带上client,那么怎么才能不使用client而使用自定义的名称呢?
修改api-gateway服务配置文件,添加如下配置
zuul:
routes:
myClient:
path: /myClient/**
serviceId: client复制代码
这时地址栏中输入 http://localhost:8085/myClient/getServerResult
仍然可以获取到返回结果。
那么我们怎么查看所有路由的规则呢?
地址栏中输入 http://localhost:8085/actuator/routes
9:禁用路由
修改api-gateway配置文件,添加如下
zuul:
ignored-patterns:
- /**/getServerResult复制代码
此时我们git上的配置文件应该为
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: '*'
zuul:
routes:
myClient:
path: /myClient/**
serviceId: client
# 设置可以传递请求头
sensitiveHeaders:
ignored-patterns:
- /**/getServerResult复制代码
这里我们把这个url给禁用掉,再次访问该地址
10:动态配置路由
利用我们上篇文章讲的统一配置中心,来实现动态配置路由功能,大家可以先回想一下配置的步骤
- 在api-gateway服务上,添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>复制代码 - 修改git上api-gateway服务的配置文件,添加rabbitmq配置信息
spring: rabbitmq: host: 192.168.99.100 username: user password: password eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: '*' zuul: routes: myClient: path: /myClient/** serviceId: client # 设置可以传递请求头 sensitiveHeaders: ignored-patterns: - /**/getServerResult复制代码 - 创建ZuulConfig.java
package com.example.apigateway; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; @Component public class ZuulConfig { @ConfigurationProperties("zuul") @RefreshScope public ZuulProperties zuulProperties(){ return new ZuulProperties(); } }复制代码
注意这时我们访问 http://localhost:8085/myClient/getServerResult 是404的,因为我们已经在配置文件中把url禁用了
修改git上api-gateway的配置文件,把禁用 http://localhost:8085/myClient/getServerResult
的配置注释掉
然后postman访问 http://localhost:8084/actuator/bus-refresh 来刷新配置文件
再次访问
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 微服务网关实战——Spring Cloud Gateway
- 微服务网关Spring Cloud Gateway的应用实战
- istio服务网格生产环境ingress网关部署SSL实战
- Microsoft Teams Voice语音落地系列-5 实战: Sonus语音网关配置
- Soul 网关发布 2.2.0,让高性能网关变得如此简单!
- 远行API网关(200428)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Coding the Matrix
Philip N. Klein / Newtonian Press / 2013-7-26 / $35.00
An engaging introduction to vectors and matrices and the algorithms that operate on them, intended for the student who knows how to program. Mathematical concepts and computational problems are motiva......一起来看看 《Coding the Matrix》 这本书的介绍吧!