新建模块-添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
配置文件
spring:
application:
name: cloud-gatesway-service
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_route1
# uri: http://localhost:8001
uri: lb://CLOUD-PAYMENT-SERVECE
predicates:
- Path=/payment/getPaymentById/**
- id: payment_route2
# uri: http://localhost:8001
uri: lb://CLOUD-PAYMENT-SERVECE
predicates:
- Path=/payment/get/**
编写第一个全局过滤器
@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*******************coming in MyGlobalFilter" + new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == null) {
log.info("*******************非法用户,uname为null");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}