源码:https://gitee.com/GXQ205153964/gateway-parent.git

- 客户端多次请求不同的微服务,增加客户端的复杂性
- 认证复杂,每个服务都要进行认证
- http请求不同服务次数增加,性能不高
流程:
http默认端口是80,不写就是80,这样用户访问更加方便。

pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-gatewayartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- dependencies>
启动类
- @SpringBootApplication
- @EnableEurekaClient
- public class ApiGatewayApp {
- public static void main(String[] args) {
- SpringApplication.run(ApiGatewayApp.class,args);
- }
- }
application.yml
- server:
- port: 80
-
- spring:
- application:
- name: api-gateway-server
-
- cloud:
- #网关配置
- gateway:
- #路由配置:转发规则
- routes: #集合
- #id: 唯一标识 默认UUID
- - id: gateway-provider
- #uri: 转发路径
- uri: http://localhost:8000/
- #predicates: 条件,用于请求网关路径的匹配规则
- predicates:
- - Path=/goods/**
- #当web访问 http://localhost:80/goods/finds 时,gateway会自动和上面匹配,
- #predicates里的Path相同时,会把端口后拼接到Path后
-
- - id: gateway-consumer
- uri: http://localhost:9000/
- predicates:
- - Path=/order/**
-
转换规则:
当web访问 http://localhost:80/goods/finds 时,gateway会自动和上面匹配,predicates里的Path相同时,会把端口后拼接到Path后
当路由发生变化后,网关里的配置就需要修改,相当麻烦。
动态路由使用到Eureka里注册的url地址,直接拉取,不用再自己配置了。
流程:
application.yml
- server:
- port: 80
-
- spring:
- application:
- name: api-gateway-server
-
- cloud:
- #网关配置
- gateway:
- #路由配置:转发规则
- routes: #集合
- #id: 唯一标识 默认UUID
- - id: gateway-provider
- #uri: 转发路径(静态路由)
- #uri: http://localhost:8000/
- #动态路由
- uri: lb://gateway-provider
- #predicates: 条件,用于请求网关路径的匹配规则
- predicates:
- - Path=/goods/**
- #当web访问 http://localhost:80/goods/finds 时,gateway会自动和上面匹配,
- #predicates里的Path相同时,会把端口后拼接到Path后
-
- - id: gateway-consumer
- #uri: http://localhost:9000/
- uri: lb://gateway-consumer
- predicates:
- - Path=/order/**
-
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka
结果

为了防止服务多了后uri地址重名,所以加入微服务名称,经行区分。
application.yml
- server:
- port: 80
-
- spring:
- application:
- name: api-gateway-server
-
- cloud:
- # 网关配置
- gateway:
- # 路由配置:转发规则
- routes: #集合。
- # id: 唯一标识。默认是一个UUID
- # uri: 转发路径
- # predicates: 条件,用于请求网关路径的匹配规则
- # filters:配置局部过滤器的
-
- - id: gateway-provider
- # 静态路由
- # uri: http://localhost:8001/
- # 动态路由
- uri: lb://GATEWAY-PROVIDER
- predicates:
- - Path=/goods/**
- filters:
- - AddRequestParameter=username,zhangsan
-
- - id: gateway-consumer
- # uri: http://localhost:9000
- uri: lb://GATEWAY-CONSUMER
- predicates:
- - Path=/order/**
- # 微服务名称配置
- discovery:
- locator:
- enabled: true # 设置为true 请求路径前可以添加微服务名称
- lower-case-service-id: true # 允许为小写
-
-
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka


api-gateway-server 添加配置文件
- - id: gateway-provider
- # 静态路由
- # uri: http://localhost:8001/
- # 动态路由
- uri: lb://GATEWAY-PROVIDER
- predicates:
- - Path=/goods/**
- filters:
- - AddRequestParameter=username,zhangsan
provider的GoodsController findOne函数加参数String username
加入输出语句输出,下面的函数 findOne_fallback 需要和finidOne参数保持一直,发生降级时被
Hystrix的fallbackMethod调用



创建Myfilter类
Myfilter
- package com.gao.gateway.filter;
-
- import org.springframework.cloud.gateway.filter.GatewayFilterChain;
- import org.springframework.cloud.gateway.filter.GlobalFilter;
- import org.springframework.core.Ordered;
- import org.springframework.core.annotation.Order;
- import org.springframework.stereotype.Component;
- import org.springframework.web.server.ServerWebExchange;
- import reactor.core.publisher.Mono;
-
- @Component
- public class MyFilter implements GlobalFilter, Ordered {
- @Override
- public Mono
filter(ServerWebExchange exchange, GatewayFilterChain chain) { - System.out.println("自定义全局过滤器执行~~~~");
- return chain.filter(exchange); //执行放行
- }
-
- /**
- * 过滤排序
- * @return 数值越小越先执行
- */
- @Override
- public int getOrder() {
- return 0;
- }
- }



在源码中下载md文档可以看到内置的过滤器工厂多有配置的使用方法和详情使用步骤
