简介
SpringCloud Gateway是spingcloud家族的产品,使用netty实现的高性能服务网关,用于替换netflix公司的zuul网关实现。
参考地址: https://spring.io/projects/spring-cloud
-
术语
-
工作原理
-
Route Predicate Factories
- GatewayFilter
示例
- 添加依赖配置
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
server:port: 9000spring:application:name: svc-gatewaycloud:nacos:discovery:server-addr: 175.178.191.230:8848gateway:routes:- id: svc-hotel-consumeuri: lb://svc-hotel-consumerpredicates:- Path=/hotel/**
- 自定义异常返回处理:
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public WebExceptionHandler exceptionHandler() {return new WebExceptionHandler() {@Overridepublic Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {exchange.getResponse().getHeaders().set("Content-Type","application/json");Map map = new HashMap();map.put("status",400);map.put("msg",ex.getMessage());return exchange.getResponse().writeWith(Mono.just(exchange.getResponse().bufferFactory().wrap(JSON.toJSONString(map).getBytes())));}};
}