更多Spring与微服务相关的教程请戳这里 Spring与微服务教程合集
路由是网关最基础的部分。路由信息由一个ID、一个目的url、一组断言工厂和一组Filter组成。如果路由断言为真,则说明请求的url和配置的路由匹配。
Java8中的断言函数。spring cloud gateway中的断言函数输入类型是spring5.0框架中的ServerWebExchange。spring cloud gateway中的断言函数允许开发者去定义来自于Http Request中的任何信息,比如请求头和参数等。
一个标准的spring webfilter。spring cloud gateway中的filter分为两种类型:分别是gateway filter和global filter。
注意:
路由断言:
路由过滤:
spring cloud gateway内置了很多的路由过滤工厂,也可以根据需要自定义路由过滤工厂
路由过滤器允许以某种方式修改request或response
spring:cloud:gateway:routes:- id: findAlluri: http://localhost:8130predicates:- Path=/findAll- id: findOneuri: http://localhost:8130predicates:- Path=/findOne
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.GatewayFilterSpec;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.route.builder.UriSpec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.function.Function;@Configuration
public class GatewayRouteConfig {@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder builder){ZonedDateTime dateTime = ZonedDateTime.of(2020, 10, 17, 22, 40, 0, 0, ZoneId.systemDefault());return builder.routes()//普通path断言.route(r->r.path("/getStudentName").filters(new Function
}
3、gateway集成eureka服务注册中心
注意springboot和springcloud的版本,如果是其它版本,启动项目的时候可能会报错
spring:cloud:gateway:discovery:locator:#与服务注册中心集成enabled: true#因为eureka的serviceId默认是大写,设为true之后,在gateway中可以用小写的serviceId进行路由转发lowerCaseServiceId: true
eureka:client:service-url:defaultZone: http://localhost:8000/eureka/register-with-eureka: true
gateway与eureka集成后,即使不配置路由,也会进行自动路由匹配
比如在eureka上注册了一个服务提供者service-a,service-a的上下文是/service-a,端口是8080,且暴露了一个接口,路径为/student/getName
gateway的端口是9000,则访问http://localhost:9000/service-a/service-a/student/getName,可自动匹配到service-a
通过gateway的日志可以看到,自动路由的信息如下:
Route{id='ReactiveCompositeDiscoveryClient_SERVICE-A', uri=lb://SERVICE-A, order=0, predicate=Paths: [/service-a/**], \
其中,lb表示该uri从注册中心查找
spring cloud gateway的Filter实际上分两种:
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.util.List;
import java.util.Map;
import java.util.Set;public class PrintHeaderGatewayFilter implements GatewayFilter {@Overridepublic Mono
}
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.util.List;@Component
public class TokenGlobalFilter implements GlobalFilter {@Overridepublic Mono
}
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.util.List;@Component
public class PrintParamGatewayFilterFactory extends AbstractGatewayFilterFactory
spring:cloud:gateway:routes:- id: methodGetorder: -100uri: http://localhost:8130predicates:- Method=GETfilters:- name: PrintParam # 如果类名为 ???GatewayFilterFactory,则过滤器名为???args:ignoreUrl:- /findAll- /findOne