Spring Cloud 学习 - Gateway新一代网关
程序员文章站
2022-06-13 13:50:38
...
Spring Cloud Gateway 新一代网关
Spring Cloud Gateway 是Spring Cloud的一个全新项目,基于Spring 5.0 + Spring Boot 2.x和Project Reactor等技术开发的网关,旨在为微服务架构提供一种有效的统一的API路由管理方式。
Spring Cloud Gateway作为Spring Cloud生态系统中的网关组件,目标是替代Zuul。由于Zuul2.x的多次跳票,为了提升网关的性能,Spring Cloud官方基于Spring WebFlux开发了非阻塞的网关组件Gateway,WebFlux框架底层使用了高性能的Reactor模式的非阻塞通信框架Netty。
引入依赖
<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>
因为要从注册中心对服务实例进行动态路由,这里加入了eureka-client
的依赖,把gateway
注册到eureka
中。
服务发现
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
编写配置
事前先编写如下2个服务,并提供相应的接口,服务名称分别为cloud-provider
、cloud-consumer
server:
port: 8008
spring:
application:
name: cloud-gateway
devtools:
livereload:
port: 35731 # 设置热部署插件端口,防止端口冲突导致热加载不生效
cloud:
gateway:
discovery:
locator:
enabled: true # 开启动态路由,不配置uri时默认访问路径:/serviceId/url, e.g. http://localhost:8008/cloud-provider/provider/xudc
lower-case-service-id: true # 启用serviceId小写功能,默认是全部大写,e.g. CLOUD-PROVIDER,开启后:cloud-provider
routes:
- id: spring-cloud-provider # 指定路由id,可任意名称,唯一即可
uri: lb://cloud-provider # lb表示负载均衡到<serviceId>
predicates:
- Path=/provider/**
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
自定义全局过滤
@Component
public class CustomFilter implements GlobalFilter, Ordered {
/**
* 模拟用户登录过滤
* 携带token参数则认为是授权的,未携带则未授权,拒绝访问
* @param exchange
* @param chain
* @return
*/
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String token = request.getQueryParams().getFirst("token");
if (StringUtils.isEmpty(token)) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
byte[] bytes = "{\"status\": 401,\"message\": \"Unauthorized Request!请求未授权\"}".getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = response.bufferFactory().wrap(bytes);
// 设置编码utf-8,否则浏览器中文可能会乱码
response.getHeaders().add("content-type", "text/plain;charset=utf-8");
// return exchange.getResponse().setComplete();
return response.writeWith(Flux.just(buffer));
}
// 通过则放行
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
测试
GET http://localhost:8008/provider/cindy
HTTP/1.1 401 Unauthorized
transfer-encoding: chunked
content-type: text/plain;charset=utf-8
{"status": 401,"message": "Unauthorized Request!请求未授权"}
GET http://localhost:8008/provider/cindy?token=qwerasd
HTTP/1.1 200 OK
Content-Type: text/plain;charset=UTF-8
Content-Length: 12
Hello, cindy
以上就是Spring Cloud Gateway的简单使用。
更多功能使用,请参阅官方文档: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/#gateway-starter
因个人能力有限,如果不足或错误之处,欢迎指正~
下一篇: C#设计模式之:装饰模式
推荐阅读
-
spring cloud 使用Zuul 实现API网关服务问题
-
Spring Cloud入门教程之Zuul实现API网关与请求过滤
-
详解spring cloud构建微服务架构的网关(API GateWay)
-
Spring Cloud Gateway入门解读
-
详解Spring Cloud Zuul 服务网关
-
Spring Cloud学习教程之Zuul统一异常处理与回退
-
Spring Cloud学习教程之DiscoveryClient的深入探究
-
spring cloud 使用Zuul 实现API网关服务问题
-
阿里Sentinel支持Spring Cloud Gateway的实现
-
Spring Cloud入门教程之Zuul实现API网关与请求过滤