微服务项目用到gateway网关, 需要配置全局过滤器, 话不多说,直接上代码:
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class MyFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("进入到全局过滤器了........");
return chain.filter(exchange); // 放行
}
/**
* 过滤器排序
* @return 数值越小 越先执行
*/
@Override
public int getOrder() {
return 0;
}
}启动网关后,通过浏览器地址栏查询数据, 总是进不到全局过滤器中.全局过滤器不起作用.
折腾一早上,检查各种配置,都没问题,后来发现自己的查询地址是: 查询地址: http://localhost:8001/checkitem/findById/28 (8001是服务提供者的端口号)
卧槽,直接用服务端的地址查询,肯定就绕过网关了!!
Eureka注册中心的注册实例如下:

通过网关的配置,再去访问服务端: http://localhost/hystrix-provider-ywl/checkitem/findById/28 (此处使用的是Eureka注册中心+网关gateway-server的模式)

用客户端的地址去访问: http://localhost/hystrix-consumer-ywl/getCheckItem/queryByEurekaServer3/28 (此处使用的是Eureka注册中心+网关gateway-server的模式)

版权声明:本文为number_long原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。