SpringCloud Gateway网关配置中,路由需要同时满足http(80端口)和https(443端口)方式访问。

SpringBoot版本:2.1.11.RELEASE,SpringCloud版本:Greenwich.SR4

如下配置即可满足:

一、80端口设置:

http:
  port: 80

  配置HttpServer.java

package com.jc.gateway;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @ClassName: HttpServer
 * @Author: jc
 * @Date: 2020/3/9 10:26
 * @Description: 配置http服务,使其即支持http又支持https服务(https通过配置文件配置)
 */

@Configuration
public class HttpServer {

    @Autowired
    private HttpHandler httpHandler;

    private WebServer webServer;

    @Value("${http.port}")
    private Integer httpPort;

    @PostConstruct
    public void start() {
        NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(httpPort);
        WebServer webServer = factory.getWebServer(httpHandler);
        webServer.start();
    }

    @PreDestroy
    public void stop() {
        webServer.stop();
    }

}

二、配置443端口(通过YML文件配置)

#SSL配置
server:
  port: 443
  ssl:
    key-store : classpath:server.jks
    key-store-password : 20200101
    key-password : 20200101
    key-store-type: JKS
    enabled: true

 

至此配置完成。

三、参考资料:

https://www.cnblogs.com/lianggp/p/8136540.html

https://blog.csdn.net/gy132/article/details/103509710

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐