gateway定制网站三大核心概念
Route(路由)
定制网站路由是构建网关的基本模块,它由ID,目标URI,定制网站一系列的断言和过滤器组成,定制网站如果断言为true定制网站则匹配该路由
Predicate(断言)
参考的是Java8的java.util.function.Predicate定制网站开发人员可以匹配HTTP定制网站请求中的所有内容(定制网站例如请求头或请求参数),定制网站如果请求与断言相匹配定制网站则进行路由
Filter(过滤)
指的是Spring框架中GatewayFilter的实例,定制网站使用过滤器,定制网站可以在请求被路由前或定制网站者之后对请求进行修改。
web请求,通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。
predicate就是我们的匹配条件;
而filter,就可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了
Gateway工作流程
客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。
Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。
Filter在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,
在“post”类型的过滤器中可以做响应内容、响应头的修改,日志的输出,流量监控等有着非常重要的作用。
核心逻辑 :路由转发+执行过滤器链
代码讲解1
需求:
现在有一个请求,我们目前不想暴露8001端口,希望在8001外面套一层9527
pom文件依赖:
(版本由父工程控制)
- <!--gateway-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
- <!--eureka-client-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
- <dependency>
- <groupId>com.atguigu.springcloud</groupId>
- <artifactId>cloud-api-commons</artifactId>
- <version>${project.version}</version>
- </dependency>
- <!--一般基础配置类-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
-
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://eureka7001.com:7001/eureka
主启动类:
- @SpringBootApplication
- @EnableEurekaClient
- public class GateWayMain9527
- {
- public static void main(String[] args)
- {
- SpringApplication.run(GateWayMain9527.class,args);
- }
- }
代码中注入RouteLocator的Bean
上面的是通过配置文件去设置的网关,而除了这种方法以外还有另一种方法去设置通过代码中注入RouteLocator的Bean
代码讲解2
需求:
通过9527网关访问到外网的百度新闻网址,当访问地址 http://localhost:9527/guonei时会自动转发到地址:http://news.baidu.com/guonei
设置代码:
- @Configuration
- public class GateWayConfig
- {
- /**
- * 配置了一个id为route-name的路由规则,
- * 当访问地址 http://localhost:9527/guonei时会自动转发到地址:http://news.baidu.com/guonei
- * @param builder
- * @return
- */
- @Bean
- public RouteLocator customRouteLocator(RouteLocatorBuilder builder)
- {
- RouteLocatorBuilder.Builder routes = builder.routes();
-
- routes.route("path_route_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
-
- return routes.build();
-
- }
-
- }
-
通过微服务名实现动态路由
默认情况下Gateway会根据注册中心注册的服务列表,
以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能
代码讲解3
需求:
从服务注册中心上面去调用服务,即假设现在有俩个服务8001 8002(已经注册进服务中心 7001, 服务名称:cloud-payment-service ),需要通过去 调用 cloud-payment-service上面的服务,即8001 8002上的服务
pom依赖引入:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
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
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri