发布时间:2023-10-01 11:30
什么是gateway
熔断
、限流
、重试
等。安全
、监控
、日志
和限流
。**作用:**反向代理、鉴权、流量监控、熔断、日志监控等等。
架构图示:
gateway的三大核心:
**动态路由:**地址映射
**Predicate 【断言】:**是否符合条件
**Filter 【过滤器】:**转发前后的过滤
gateway的工作流程:
为了方便包管理创建一个父工程
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.6.5version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.0.1.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
子工程用作gateway网关
<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>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<version>2.6.5version>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
server:
port: 9527 #向外暴露的网关端口号,不需暴露微服务的地址,向外统一一个网关地址,通过网关来转发
spring:
application:
name: cloud-gateway
cloud:
gateway: #网关配置
discovery:
locator:
enabled: true #开启负载均衡
routes:
- id: payment-routh #微服务id,自定义保证唯一就行
uri: http://localhost:8003 #微服务地址
predicates:
- Path=/payment/circuit/** #微服务中的请求,**代表参数
- id: payment-routh2
uri: http://localhost:8003
predicates:
- Path=/payment/hystrix/timeout/**
#用于负载均衡测试
- id: payment-routh3
uri: lb://CLOUD-PROVIDE-PAYMENT #负载均衡
predicates:
- Path=/payment/query/**
- id: payment-routh3
uri: lb://CLOUD-PROVIDE-PAYMENT #负载均衡
predicates:
- Path=/payment/query/**
eureka: #注册中心
client:
register-with-eureka: true
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: gateway9527
prefer-ip-address: true
lease-renewal-interval-in-seconds: 2
lease-expiration-duration-in-seconds: 4
package com.qiumin.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.ZonedDateTime;
@Configuration
//路由映射配置代码方式配置
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_com_qiumin",r->r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
return routes.build();
}
}
我们也可以在配置文件在通过filter来配置,当一般我们用我们自己的自定义路由配置,实现GlobalFilter, Ordered该两个接口,并实现方法
package com.qiumin.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Date;
@Component
@Slf4j
//我的全局过滤器配置类
public class MyLogGateWayGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("come in MyLogGateWayGlobalFilter:"+new Date());
String username = exchange.getRequest().getQueryParams().getFirst("username");
if(username==null){
log.info("用户名为null,非法用户!!!");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
设置该过滤器后,我们的请求是处理必须要写的外还要带上username参数且不能为空,否则就会被挡
gateway过滤器生命周期两种:pre 【之前】
、post 【之后】
种类两种:GatewayFilter
、GlobalFilter
package com.qiumin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //服务注册中心
public class gatewayApplication9527 {
public static void main(String[] args) {
SpringApplication.run(gatewayApplication9527.class,args);
}
}
qiumin