赞
踩
目录
Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2和 Project Reactor等技术。
Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等。
反向代理、鉴权、流量控制、熔断、日志监控……
微服务架构中网关的位置(相当于给微服务看大门的):
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由。
参考的是Java8的java.util.function.Predicate
开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由。
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
web请求通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。
predicate就是我们的匹配条件;
filter可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了。
核心逻辑是:路由转发+执行过滤器链
新建普通maven模块cloud-gateway-gateway9527
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>mscloud03</artifactId>
- <groupId>com.atguigu.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cloud-gateway-gateway9527</artifactId>
-
- <dependencies>
- <!--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.shang.cloud</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>
-
-
- </project>

- server:
- port: 9527
-
- spring:
- application:
- name: cloud-gateway
-
- eureka:
- instance:
- hostname: cloud-gateway-service
- client: #服务提供者provider注册进eureka服务列表内
- service-url:
- register-with-eureka: true
- fetch-registry: true
- defaultZone: http://eureka7001.com:7001/eureka
使用eureka、zookeeper或者consul都可以。
- @SpringBootApplication
- @EnableEurekaClient
- public class GateWayMain9527 {
- public static void main(String[] args) {
- SpringApplication.run(GateWayMain9527.class, args);
- }
- }
通过以上的配置之后,基本配置已经完成。那么现在需要思考9527如何做路由映射。
以cloud-provider-payment8001为例
我们不想直接暴露8001,想在8001外面套一层9527,让9527为8001看家护院。
于是,我们需要在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

对应关系如下:
9527已经注册进了eureka
http://localhost:8001/payment/get/1
http://localhost:9527/payment/get/1
9527也能访问到。
我们现在服务模块是写死的,只能通过8001来提供服务,假如我们后面有8002、8003……改该怎么办呢?
这就需要配置动态路由, 以服务名为路径。通过网关来实现负载均衡。
- 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

http://localhost:9527/payment/lb
可以看到8001和8002轮询,负载均衡实现了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。