三大核心概念
Route(路由)
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
Predicate(断言)
参考的是Java8的java.util.function.Predicate
开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
Filter(过滤)
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
核心逻辑
路由转发+执行过滤器链
新建Module
cloud-gateway-gateway9527
pom核心jar
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
yml配置
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
# id 唯一的
- id: A
#动态路由 用的注册中心的Application下的集群名称
uri: lb://CLOUD-PAYMENT-SERVICE
#单机
# uri: http://localhost:8001
predicates:
# 表示放行所有请求
- Path=/**
eureka:
instance:
hostname: cloud-gateway-service
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone:
#你搭建的注册中心的名称
http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka