赞
踩
该博客仅为作者记录springcloud学习过程,作者水平有限,如有错误敬请提出
SpringCloud Gateway 是 Spring Cloud 的一个全新项目,基于 Spring 5.0+Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。
网关是所有微服务的门户,路由转发仅仅是最基本的功能,除此之外还有其他的一些功能,比如:认证、鉴权、熔断、限流、日志监控等等…
gateway主要由三个部分组成,分别是:
Gateway的生命周期:
PRE:这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择 请求的微服务、记录调试信息等。
POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。
1.添加gateway依赖
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2.yml配置文件
server: port: 9527 spring: application: name: cloud-gateway cloud: gateway: routes: - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: http://localhost:8001 #匹配后提供服务的路由地址 predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: http://localhost:8001 #匹配后提供服务的路由地址 predicates: - Path=/payment/lb/** # 断言,路径相匹配的进行路由 eureka: instance: hostname: cloud-gateway-service client: #服务提供者provider注册进eureka服务列表内 service-url: register-with-eureka: true fetch-registry: true defaultZone: http://localhost:7001/eureka
3.启动类
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527
{
public static void main(String[] args)
{
SpringApplication.run(GateWayMain9527.class,args);
}
}
4.测试说明,箭头所指的就是所匹配的url;启动7001,8001,9527端口服务
输入http://localhost:9527/payment/lb,即可看到结果
5.实现动态路由
默认情况下Gateway会根据注册中心注册的服务列表,
以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能
需要修改yml配置文件
server: port: 9527 spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由 routes: - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8001 #匹配后提供服务的路由地址 uri: lb://cloud-payment-service #匹配后提供服务的路由地址 predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8001 #匹配后提供服务的路由地址 uri: lb://cloud-payment-service #匹配后提供服务的路由地址 predicates: - Path=/payment/lb/** # 断言,路径相匹配的进行路由 eureka: instance: hostname: cloud-gateway-service client: #服务提供者provider注册进eureka服务列表内 service-url: register-with-eureka: true fetch-registry: true defaultZone: http://eureka7001.com:7001/eureka
这里我们访问url:http://localhost:9527/payment/lb,会在8001端口和8002端口服务中切换(负载均衡)
6.设置Predicate(断言)
Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理。
例如:我们在上面设置的断言,是指在路径匹配的结果下,去找到对应路由
具体断言设置可以看:
Spring Cloud Gateway
实战Spring Cloud Gateway
7.设置filter
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。
Spring Cloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生
Gateway 的Filter从作用范围可分为两种:
自定义过滤器
自定义过滤器需要实现两个主要接口:
implements GlobalFilter Ordered
其中Order接口的返回值是指优先级
过滤器执行流程如下,order 越大,优先级越低,如图所示。
上面的是全局过滤器,对所有路由生效
下面展示局部过滤器的配置
@Component public class UserIdCheckGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> { @Override public GatewayFilter apply(Object config) { return new UserIdCheckGateWayFilter(); } @Slf4j static class UserIdCheckGateWayFilter implements GatewayFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { System.out.println("time:"+new Date()+"\t 执行了自定义的局部过滤器: "+"UserIdCheckGateWayFilter"+"hello"); String uname = exchange.getRequest().getQueryParams().getFirst("id"); if (uname == null) { System.out.println("****id为null,无法登录"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } // 值越小,优先级越高 @Override public int getOrder() { return HIGHEST_PRECEDENCE; } } }
结果:
路径为/get的必须要有id,因为设置了filter
而/lb不需要
关于自定义过滤器具体可以看这两篇博客
spring cloud gateway自定义过滤器
Spring-Cloud-Gateway实现自定义过滤器
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。