配置gateway做网关
新建model,叫service-gateway8588,添加依赖
<dependencies>
<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-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
注意:gateway里面不能有web的依赖,不然会报错的
配置文件
server:
port: 8588
spring:
application:
name: service-gateway8588
cloud:
gateway:
##复数,可以写多组routes
routes:
- id: gateway001
uri: http://localhost:8101
##复数,可以写多组predicates
predicates:
- Path=/provider
eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/
然后启动service-eureka001,service-provider(8101),service-gateway8588
首先http://localhost:8101/provider,结果显示“这里是provider,port:8101,f3017559-32ec-4aa8-a94f-87fcfa421e2e”
在通过网关调用一下http://localhost:8588/provider,结果显示“这里是provider,port:8101,f3017559-32ec-4aa8-a94f-87fcfa421e2e”。调用成功
还可以通过配置类的方式来添加路由,个人感觉比较繁琐,这里不做介绍
如果服务提供做成了集群,那么在路由时通过http://localhost:8101这样的方式是不能满足的,那么我们通过服务名直接去注册中心获取ip然后在调用,这样就能形成负载均衡了
修改配置文件
spring:
application:
name: service-gateway8588
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
# id不可重复
- id: gateway001
# uri: http://localhost:8101
uri: lb://SERVICE-PROVIDER
predicates:
- Path=/provider
启动service-eureka001,service-provider(8101,8102),service-gateway8588
然后调用http://localhost:8588/provider,多刷新几次,会发现出现的端口号是8101和8102交替出现的,实现了负载均衡
predicates断言
简单来说,就是一个请求进来,然后进行规则匹配,最后转发到匹配到的对应的路由
spring:
application:
name: service-gateway8588
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: gateway001
# uri: http://localhost:8101
uri: lb://SERVICE-PROVIDER
predicates:
# ZonedDateTime time=ZonedDateTime.now();可以得到下面格式的时间
# 在什么时间之后
- After=2020-09-15T15:11:53.878+08:00[Asia/Shanghai]
# 在什么时间之前
- Before=2020-09-15T15:11:53.878+08:00[Asia/Shanghai]
# 在什么时间之间
- Between=2020-09-15T15:11:53.878+08:00[Asia/Shanghai]
# 带cookie的请求,而且名字必须匹配正确
- Cookie=chocolate, ch.p
# 路径
- Path=/provider
# 更多可去https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#gateway-request-predicates-factories官网查看
自定义全局过滤器
添加一个类,实现GlobalFilter, Ordered接口
@Component
@Slf4j
public class CustomGlobalFilter implements GlobalFilter, Ordered {
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
String id = exchange.getRequest().getQueryParams().getFirst("id");//带名称为id的参数
if (id==null){
log.info("请求失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);//本次过滤通过
}
@Override public int getOrder() {
return 0;
}
}
测试http://localhost:8588/provider,结果调用不通
测试http://localhost:8588/provider?id=22,结果能调用,配置成功