这里写目录标题
- 1、介绍gateway
- 1.1、概念详解
- 2、配置
- 3、异常:
- 解决方案1:
- 解决方案2:
- Zuul与Gateway路由中的不同点
1、介绍gateway
gateway 网关,内置webflux 依赖,不再使用springMvc
官网配置地址
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RC3/single/spring-cloud-gateway.html
1.1、概念详解
Spring Boot Webflux 是非阻塞式的 I/O ,而 SpringMVC 则是阻塞式的
具体性能对比请看https://www.jianshu.com/p/b2d53667e7e2
tomcat就是针对http层的,所以我建议http还是选择tomcat(或者其他成熟的http-server),并不是说netty不好,而是你的选择问题。
netty是一个网络组件,tcp,udp,http都可以弄,但是官方文档都是些hello wolrd级别的。如果你非常了解http结构,完全可以基于netty搞出一个比tomcat牛的http server。如果做tcp开发,netty不二之选!
现在高并发分布式网站架构一般采用nginx(前端负载均衡)+ Netty/Tomcat(HTTP)
Netty是基于Java NIO开发的,而Tomcat是Apache下的针对HTTP的服务器项目,前者更像一个中间件框架,后者更像一个工具
2、配置
在spring gateway配置加上:
1 2 3 4 5 6 7 8 | server: port: 9000 ssl: enabled: true key-store-password: xxxxxxxxx # 证书密码 key-store: classpath:xxx.pfx # 我自己进行了文件重命名 key-store-type: PKCS12 |
按照上述的配置,可以通过https访问到gateway,但是gateway转发到后台微服务仍然是https请求,所以需要后台微服务也设置成https请求,并且在注册中心注册时就采用域名注册,这样是非常麻烦的。用过Zuul的都知道,Zuul会将https请求转换为http请求给后台微服务。
3、异常:
如果,后台微服务设置为http,那么gateway会抛出如下异常:
1 2 3 4 5 6 | io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 485454502f312e312034303200d0a436f6e74656e742d547970653a20746578742f68746d6c3b636861727365743d7574662d380d0a436f6e74656e742d4c616e67756167653a20656e0d0a436f6e74656e72d4c656e6774683a203830300d0a446174653a204672692c2032322046656220323031392030333a31363a43320474d540d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a3c21646f63747970652068746d6c3e3c68746d6c206c616e673d22656e223e3c686561643e3c7469746c653e48545450205374617475732034303020e280932042616420526571756573743c27469746c653e3c7374796c6520747970653d22746578742f637373223e6831207b666f6e742d66616d696c793a5461686f6d612c417269616c2c73616e732d73657269663b66f6c6f723a77686974653b6261636b67726f756e642d636f6c6f723a233532354437363b666f6e742d73697a653a32327083b7d206832207b666f6e742d66616d696c793a5461686f6d612c417269616c2c73616e732d73657269663b636f6c6f723a7768697465b626163667726f756e642d636f6c6f723a233532354437363b666f6e742d73697a653a313670783b7d20633207b666f6e742d66616d696c793a5461686f6d612c417269616c2c73616e732d7365726663b636f6c6f723a7768697465b6261636b67726f756e642d636f6c6f723a23353235437363b666f6e742d73697a653a313470783b7d20626f6479207b666f6e742d66616d696c793a5461686f6d612c417269616c2c73616e732d73657269663b636f6c6f723a626c61636b3b6261636b67726f756e642d636f6c6f723a77686974653b7d2062207b666f6e742d6661d696c793a5461686f6d612c417269616c2c73616e732d73657269663b636f6c6f723a77686974653b6261636b67726f756e642d636f6c6f723a233532354437363b7d2070207b666f6e742d66616d696c793a5461686f6d612c417269616c2c73616e732d73657269663b6261636b67726f756e643a77686974653b636f6c6f723a626c61636b3b666f6e742d73697a653a31320783b7d2061207b636f66f723a626c61636b3b7d20612e6e616d65207b636f6c6f723a626c61636b3b7d202e6c696e65207b6865696768743a3170783b6261636b67726f756e642d636f6c6f723a233532354437363b626f726465723a6e6f6e653b7d3cf7374796c653e3c2f68651643e3c626f64793e3c68313e48545450205374617475732034303020e280932042616420526571756573743c2f68313e3c2f626f64793e3c2f6846d6c3e at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1178) [netty-handler-4.1.31.Final.jar:4.1.31.Final] at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1243) [netty-handler-4.1.31.Final.jar:4.1.31.Final] at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:502) [netty-codec-4.1.31.Final.jar:4.1.31.Final] at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:441) [netty-codec-4.1.31.Final.jar:4.1.31.Final] at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:278) [ |
解决方案1:
配置nginx (https)->gateway(http)->其他服务
详见:
https://blog.csdn.net/qq_22041375/article/details/106885629
解决方案2:
gatewy需要我们自行添加可过滤器实现转换:
添加过滤器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; import java.net.URI; 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 org.springframework.web.util.UriComponentsBuilder; import reactor.core.publisher.Mono; @Component public class SchemeFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { Object uriObj = exchange.getAttributes().get(GATEWAY_REQUEST_URL_ATTR); if (uriObj != null) { URI uri = (URI) uriObj; uri = this.upgradeConnection(uri, "http"); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri); } return chain.filter(exchange); } private URI upgradeConnection(URI uri, String scheme) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri).scheme(scheme); if (uri.getRawQuery() != null) { // When building the URI, UriComponentsBuilder verify the allowed characters and does not // support the '+' so we replace it for its equivalent '%20'. // See issue https://jira.spring.io/browse/SPR-10172 uriComponentsBuilder.replaceQuery(uri.getRawQuery().replace("+", "%20")); } return uriComponentsBuilder.build(true).toUri(); } @Override public int getOrder() { return 10101; } } |
就可以了
Zuul与Gateway路由中的不同点
Zuul会将路由前缀删除。例如:
1 2 3 4 | spring.application.name=gateway-service-zuul server.port=8888 spring.cloud.client.ipAddress=网关IP地址 zuul.routes.basicService=/basicService/** |
前端请求:http://网关IP地址:8888/basicService/getUserInfo
Zuul路由之后:http://basicService微服务IP地址:basicService端口号/getUserInfo
(删除了basicService)
对于Gateway
Gateway路由后:http://basicService微服务IP地址:basicService端口号/basicService/getUserInfo
(没有删除basicService)
如果要实现与Zuul同样的效果,则需要加一个路径改写过滤器:
1 2 3 4 5 6 7 8 9 10 | spring: cloud: gateway: routes: - id: neo_route uri: lb://basicService predicates: - Path=/basicService/** filters: - RewritePath=/basicService/(?<segment>.*), /$\{segment} |
参考:
https://blog.csdn.net/qq_26932225/article/details/87875336?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase