097:新一代服务网关Gateway
1 新一代服务网关Gateway课程介绍
课程内容:
- Zuul与Gageway有哪些区别
- Gateway整合nacos实现服务转发
- 基于Gateway拦截请求参数
- Gateway集群原理分析
2 为什么需要使用微服务网关
什么是微服务网关
微服务网关是整个微服务API请求的入口,可以实现过滤Api接口。
作用:可以实现用户的登录验证、解决跨域问题、日志拦截、权限控制、限流、熔断、负载均衡、黑名单与白名单拦截等。
微服务中架构模式:前后端分离,前端调用接口地址都能够被抓包分析到。
传统的方式可以使用过滤器拦截用户会话信息,这个过程所有的服务都必须要写入该验证会话登录的代码。
3 过滤器与微服务网关的区别
过滤器与网关有哪些区别?
过滤器适合于单个服务实现过滤请求;局部拦截。
网关拦截整个微服务实现过滤请求,能够解决微服务中冗余代码;全局拦截。
4 Zuul网关与Gateway之间的区别
Zuul与Gateway有哪些区别
Zuul网关属于netflix公司开源的产品,属于第一代微服务网关;
Gateway属于SpringCloud自研发的网关框架,属于第二代微服务网关;
相比来说SpringCloud Gateway性能比Zuul性能要好。
注意事项:
Zuul基于Servlet实现的,阻塞式的Api,不支持长连接,依赖spring-boot-starter-web。
SpringCloudGateway基于Spring5构建,能够实现响应式非阻塞式的Api,支持长连接,能够更好的整合Spring体系的产品,依赖spring-webflux。
5 构建SpringCloudGateway
引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 80
####服务网关名称
spring:
application:
name: mayikt-gateway
cloud:
gateway:
discovery:
locator:
####开启以服务id去注册中心上获取转发地址
enabled: true
###路由策略
routes:
###路由id
- id: mayikt
####转发http://www.mayikt.com/
uri: http://www.mayikt.com/
###匹配规则
predicates:
- Path=/mayikt/**
网关的服务端口号:80或者443(https)
启动类
@SpringBootApplication
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class);
}
}
测试效果
6 GateWay实现转发真实微服务接口
application.yml
server:
port: 80
####服务网关名称
spring:
application:
name: mayikt-gateway
cloud:
gateway:
###路由策略
routes:
###根据我们的服务名称查找地址实现调用
- id: member
####转发http://www.mayikt.com/
uri: lb://mayikt-member/
filters:
- StripPrefix=1
###匹配规则
predicates:
- Path=/member/**
discovery:
locator:
###允许通过注册中心获取地址调用
enabled: true
nacos:
discovery:
server-addr: 127.0.0.1:8848
测试结果:
7 Nginx与网关之间存在哪些区别
网关与Nginx的区别
微服务网关能够做的事情,Nginx也可以实现。
相同点:
都是可以实现api的拦截、负载均衡、反向代理、请求过滤,nginx完全可以实现和网关一样的效果。
不同点:
Nginx采用C语言编写,Gateway采用java语言编写,能够对java构建的微服务项目更好的实现扩展功能;相比nginx扩展功能,需要学习lua或者c语言,学习成本更高。
8 Gateway全局过滤使用方法
spring cloud gateway官网:https://spring.io/projects/spring-cloud-gateway
实现案例:没有传递token参数,不会转发到真实服务地址。
自定义TokenFilter实现参数拦截
@Component
public class TokenGlobalFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 如何获取参数
String token = exchange.getRequest().getQueryParams().getFirst("token");
if (StringUtils.isEmpty(token)) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
String msg = "token not is null ";
DataBuffer buffer = response.bufferFactory().wrap(msg.getBytes());
return response.writeWith(Mono.just(buffer));
}
// 直接转发到真实服务
return chain.filter(exchange);
}
}
测试效果: