文档
特性
- Java 8
- Spring Framework 5
- Spring Boot 2
- 动态路由
- 内置到Spring Handler映射中的路由匹配
- 基于HTTP请求的路由匹配 (Path, Method, Header, Host, etc…)
- 过滤器作用于匹配的路由
- 支持Spring Cloud DiscoveryClient配置路由,与服务发现与注册配合使用
应用
本文springcloud基于Finchley.RELEASE版本
- pom.xml引入gateway依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- 新建自定义filter,继承AbstractGatewayFilterFactory
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
/**
* 过滤器
* @author 爱飘de小子
*/
@Slf4j
@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory<CustomGatewayFilterFactory.Config> {
public CustomGatewayFilterFactory() {
super(Config.class);
log.info("加载 自定义拦截器 [Custom]...");
}
@Override
public Config newConfig() {
return new Config();
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("enabled");
}
/**
* @param config
* @return
*/
@Override
public GatewayFilter apply(CustomGatewayFilterFactory.Config config) {
return (exchange, chain) -> {
//Custom=false 跳过验证
if (!config.isEnabled()) {
return chain.filter(exchange);
}
//获取请求信息
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
//获取请求参数
//具体业务略....
String token = headers.getFirst("token");
System.out.println("====token===="+token);
return chain.filter(exchange);
};
}
public static class Config {
/**
* 控制是否开启拦截器
*/
private boolean enabled;
public Config() {}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
- bootstrap.yml配置gateway
spring:
application:
name: gateway-demo
cloud:
gateway:
discovery:
locator:
enabled: true #是否开启
lower-case-service-id: true
default-filters: #全局过滤器配置
routes:
- id: web
uri: lb://epw-web
predicates:
- Path=/web/**
filters:
- StripPrefix=1
# 配置自定义CustomGatewayFilterFactory ,Custom是CustomGatewayFilterFactory的前缀,gateway默认省略GatewayFilterFactory
- Custom=true
- id: common
uri: lb://epw-common
predicates:
- Path=/common/**
filters:
- StripPrefix=1
eureka:
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10 #表示eureka client发送心跳给server端的频率,默认为30秒
health-check-url-path: /actuator/health #健康检查的地址(依赖spring-boot-starter-actuator)
client:
registry-fetch-interval-seconds: 10 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒
service-url:
defaultZone: http://localhost:8761/eureka/
说明:
- default-filters: 里面可以定义一些共同的filter,对所有路由都起作用
- routes:具体的路由信息,是一个数组,每一个路由基本包含部分:
- id:这个路由的唯一id,不定义的话为一个uuid
- uri:http请求为lb://前缀 + 服务id;ws请求为lb:ws://前缀 + 服务id;表示将请求负载到哪一个服务上
- predicates:表示这个路由的请求匹配规则,只有符合这个规则的请求才会走这个路由。为一个数组,每个规则为并且的关系。
- filters:请求转发前的filter,为一个数组。
- order:这个路由的执行order
4.项目启动启动类
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
/**
* @author 爱飘de小子
*/
@SpringCloudApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
说明: @SpringCloudApplication是一个组合注解,包含@SpringBootApplication
@EnableDiscoveryClient