SpringCloud 微服务网关Gateway介绍及简单路由配置 您所在的位置:网站首页 网关的用户名在哪里 SpringCloud 微服务网关Gateway介绍及简单路由配置

SpringCloud 微服务网关Gateway介绍及简单路由配置

2024-07-11 16:43| 来源: 网络整理| 查看: 265

概述:什么是微服务网关?为了解决用户客户端在调用微服务系统中的多个消费者工程接口时,需要维护非常多的消费者应用接口地址等信息,以及可能存在不同应用见的调用跨域等问题,微服务网关组件随即出现。网关作为用户客户端和微服务接口之间的一个统一路由及其他功能的组件,方便了用户客户端请求接口时不用去关注不同接口的地址路径等。只需要统一调用网关的服务即可。微服务网关为一个服务器服务,是系统对外的唯一入口。网关可以提供身份验证、监控、负载均衡、缓存、请求分片与管理、等功能。

一、Gateway 简介

微服务网关比较流行的有Zuul和Gateway。Gateway作为Spring Cloud生态系统中的网关,目标是替代Netfix Zuul,其基于filter链的方式在提供了基本的路由之外,还提供了网关的其他功能,如:安全、监控/埋点,和限流等。他是基于Netty的响应式开发模式。

gateway 与 zuul rps 对比。可以看出 Gateway 的RPS 几乎为Zuul 的 1.6倍。

组件RPS(request per second)Spring Cloud Gateway32212.38Netfix Zuul1x20800.13

 

 

 

 

 

Gateway 核心概念:

1,路由(routes)路由为网关最基础功能,Gateway网关路由信息由一个ID、一个目的URL、一组断言工厂和一组Filter组成,如果断言为真,则会对改请求路由到对应的url上。

2,断言(predicates) Java8中的断言函数,SpringCloud Gateway中的断言函数允许开发者去定义匹配来自用户端的请求request中的任何信息,如请求参数和Header中的信息。

3,过滤器(Filters) 一个标准的Spring webFilter,SpringCloud Gateway中的过滤器分为两种,针对服务的Gateway Filter 和全局的Global Filter。过滤器可以对请求和响应进行处理。

二、简单路由配置

 

准备步骤:

创建maven子工程

pom文件引入Gateway依赖

创建主启动类

编写配置文件

1,父工程pom.xml 文件如下:

4.0.0 com.xiaohui.springCloud SpringCloud 1.0-SNAPSHOT pom org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE product_service order_service cloud-api-common eureka_server eureka_server2 cloud-import-test consul_product_service consul_order_service openfeign_order_service hystrix_order_service_ipconn hystrix_order_service_rest hystrix-turbine sentinel_order_service_rest sentinel_order_service_feign api_gateway_server UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 5.1.47 1.1.16 1.3.0 org.springframework.cloud spring-cloud-dependencies Hoxton.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom import com.alibaba druid ${druid.version} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.spring.boot.version} mysql mysql-connector-java ${mysql.version} junit junit ${junit.version} org.projectlombok lombok ${lombok.version} true org.springframework.boot spring-boot-maven-plugin true true

2,创建子模块maven工程,并引入gateway依赖

SpringCloud com.xiaohui.springCloud 1.0-SNAPSHOT 4.0.0 api_gateway_server org.springframework.cloud spring-cloud-starter-gateway

3,创建主启动类

package com.xiaohui.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class GatewayServerApplication { public static void main(String[] args) { SpringApplication.run(GatewayServerApplication.class,args); } }

4,编写配置文件

server: port: 8080 spring: application: name: api-gateway-server #服务名称 cloud: gateway: routes: #配置路由: 路由id,路由到微服务的uri,断言(判断条件) - id: product-service #保持唯一 uri: http://127.0.0.1:8001 #目标为服务地址 predicates: - Path=/payment/** #路由条件 path 路由匹配条件

在配置文件中,我们断言位置配置了一个当访问网关时以 /payment/开始的请求时都会转发到uri 配置的地址127.0.0.1:8001上。

5,启动测试

我们首先保证我们的8001服务正常启动,以及再启动gateway 网关服务。都启动成功后我们访问 http://127.0.0.1:8080/payment/get/1 则会转发到 http://127.0.0.1:8001/payment/get/1 服务地址上。

至此最简单的gateway 服务我们搭建完成,并实现了最简单的服务路由功能。

三、其他路由配置规则:

在上面第二部分我们使用了比较简单的-Path 路径匹配规则,进行了服务路由。SpringCloud Gateway 处理路径匹配之外还有很多其他的匹配规则,比如请求参数、时间等,如下:

例如如下表示:2028年xxxx 时间之后的可以被路由转发。

spring: cloud: gateway: routes: #配置路由: 路由id,路由到微服务的uri,断言(判断条件) - id: product-service #保持唯一 uri: http://127.0.0.1:8001 #目标为服务地址 predicates: - After=2028-03-18T17:32:58.129+08:00[Asia/Shanghai] #路由条件 After 路由匹配条件

参考:https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/#gateway-request-predicates-factories

 

 

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有