使用spring cloud gateway为微服务网关
> gateway 相对比原始zuul 性能更强,且基于netty与zuul(这里指1.0版本,2.0版本已使用netty) 的阻塞IO上性能更强,且支持长连接并且gateway由spring cloud公司自主开发
##### 需要引用的包
spring boot使用的2.x spring cloud 使用Finchley版本
```xml
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-webflux
```
> 注意:因为gateway使用webflux实现的,不要使用webmvc方式,否则会报错
---
与zuul方式不太一样的是,gateway不需要在启动类注入任何注解,使用yaml文件在配置文件集成即可
application.yml配置如下
```yaml
spring:
application:
name: gatewayDemo
cloud:
gateway:
routes:
- id: user
uri: lb://user //使用服务发现中获取 这里的lb为负载均衡的意思 这里也可以使用http://baidu.com方式 转发路径
predicates:
- Path=/user/**
filters:
- StripPrefix=1 //如果使用服务发现模式,这里必须添加,去掉请求中的user字符
- id: user
uri: lb:ws://user //这里设置webSocket转发路径
predicates:
- Path=/user/**
filters:
- StripPrefix=1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8889
logging: //设置gateway日志格式
level:
org.springframework.cloud.gateway: TRACE
org.springframework.http.server.reactive: DEBUG
org.springframework.web.reactive: DEBUG
```
下一篇文章将继续讲解gateway中如何验证token与过滤器设置
回复 刷新
登录 后可发表评论