SpringCloud Gateway Https配置

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配置加上:

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會拋出如下異常:

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需要我們自行添加可過濾器實現轉換:
添加過濾器:


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會將路由前綴刪除。例如:

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同樣的效果,則需要加一個路徑改寫過濾器:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章