网站首页 > 技术文章 正文
前言
不同的微服务一般有不同的网络地址,而外部的客户端可能需要调用多个服务的接口才能完成一个业务需求。比如一个电影购票的手机 APP,可能会调用电影分类微服务,用户微服务,支付微服务等。如果客户端直接和微服务进行通信,会存在以下问题:
- 客户端会多次请求不同微服务,增加客户端的复杂性
- 存在跨域请求,在一定场景下处理相对复杂
- 认证复杂,每一个服务都需要独立认证
- 难以重构,随着项目的迭代,可能需要重新划分微服务,如果客户端直接和微服务通信,那么重构会难以实施
- 某些微服务可能使用了其他协议,直接访问有一定困难
Zuul
Zuul 包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
Zuul 和 Eureka 进行整合,将 Zuul 自身注册为 Eureka 服务治理下的应用,同时从 Eureka
中获得其他微服务的信息,也即以后的访问微服务都是通过 Zuul 跳转后获得。
总体来说,Zuul 提供了代理、路由和过滤的功能。
搭建
建立一个模块,springcloud-zuul
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
添加配置文件application.yml
server:
port: 9000
spring:
application:
name: zuul-gateway
eureka:
client:
serviceUrl:
defaultZone: http://localhost:5000/eureka/
添加启动类
@SpringBootApplication
//启用 Zuul 代理功能
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
启动eureka,zuul,provider,feign-consumer项目
浏览器输入:
http://localhost:9000/springcloud-feign-consumer/feign/consumer/get/emp
http://localhost:9000:访问zuul网关
springcloud-feign-consumer:目标微服务名称
/feign/consumer/get/emp:目标微服务具体功能地址
访问成功
使用指定的名称代替微服务名称,zuul模块添加配置
zuul:
routes:
employee: # 自定义路由规则的名称,在底层的数据结构中是 Map 的键
serviceId: springcloud-feign-consumer # 目标微服务名称,ZuulRoute 类型的一个属性
path: /zuul-emp/** # 用来代替目标微服务名称的路径,
/**表示匹配多层路径,如果没有加/**则不能匹配后续的多层路径了
效果:使用微服务名称和新配置的地址都可以访问
http://localhost:9000/springcloud-feign-consumer/feign/consumer/get/emp
http://localhost:9000/zuul-emp/feign/consumer/get/emp
让用户不能通过微服务名称访问
zuul:
ignored-services: # 忽略指定微服务名称,让用户不能通过微服务名称访问
- springcloud-feign-consumer
routes:
employee: # 自定义路由规则的名称,在底层的数据结构中是 Map 的键
serviceId: springcloud-feign-consumer # 目标微服务名称,ZuulRoute 类型的一个属性
path: /zuul-emp/** #用来代替目标微服务名称的路径
ignored-services: # 忽略指定微服务名称,让用户不能通过微服务名称访问
忽略所有,用:'*',配置完成下面地址将不能访问
http://localhost:9000/springcloud-feign-consumer/feign/consumer/get/emp
给访问路径添加统一前缀
prefix: /ssss # 给访问路径添加统一前缀
http://localhost:9000/ssss/zuul-emp/feign/consumer/get/emp,访问成功
如果配置context-path,还需加上context-path
server:
port: 9000 servlet:
context-path: /context
http://localhost:9000/context/ssss/zuul-emp/feign/consumer/get/emp
但是我们还可以不通过zuul,直接访问其他的微服务,这时需要配置ZuulFilter进行统一拦截。
ZuulFilter
添加配置类MyZuulFilter
@Component
public class MyZuulFilter extends ZuulFilter {
@Override
public boolean shouldFilter() {
// 1.获取当前 RequestContext 对象
RequestContext context = RequestContext.getCurrentContext();
// 2.获取当前请求对象
HttpServletRequest request = context.getRequest();
// 3.获取当前请求要访问的目标地址
String servletPath = request.getServletPath();
// 4.打印
System.err.println("servletPath="+servletPath);
// 5.当前方法返回值
// true:表示应该过滤,下面继续执行 run()方法
// false:表示不过滤,直接放行
return true;
}
@Override
public Object run() throws ZuulException {
// 执行具体过滤逻辑
System.err.println("run() ...");
// 官方文档说:当前实现会忽略这个返回值,所以返回 null 即可
return null;
}
@Override
public String filterType() {
// 返回当前过滤器类型
// 可选类型包括:pre、route、post、static
// 如果需要在目标微服务前面执行过滤操作,选用 pre 类型
return "pre";
}
@Override
public int filterOrder() {
// 过滤器执行顺序
return 0;
}
}
这样可以自定义拦截规则
猜你喜欢
- 2024-11-22 SpringCloud——zuul权限校验、接口限流
- 2024-11-22 Spring Cloud源码分析(四)Zuul:核心过滤器
- 2024-11-22 Spring Cloud Zuul 学习+实践
- 2024-11-22 SpringCloud网关及Zuul项目搭建,你确定你了解?
- 2024-11-22 springcloud-zuul微服务网关高可用的场景概述
- 2024-11-22 24岁秃头程序员带你深入学习微服务网关Zuul源码解析,不来别后悔
- 2024-11-22 Zuul 路由网关
- 2024-11-22 SpringCloud微服务架构篇6:Zuul路由规则
- 2024-11-22 springcloud(十一):服务网关Zuul高级篇
- 2024-11-22 SpringCloud系列之Zuul网关和Zuul过滤器
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)