务虚的小刚 2019-06-22 13:27 采纳率: 0%
浏览 371

springcloud网关服务和message微服务都配置了ssl,结果网关服务路由不到message服务,怎么处理?

springcloud+eureka+网关服务(zuul构建)+ message微服务(微信小程序用到了websocket调用此服务中的接口,需配置ssl)

message微服务中相关代码如下:

application.properties 配置文件代码

server.port=9007  
spring.application.name=message-service 
// https配置
 https.port=443  
server.ssl.key-store=classpath:aliyun.pfx  
server.ssl.key-store-password=8vwAq72s  
server.ssl.key-store-type=PKCS12
// 启动入口代码  
public class MessageServiceApplication {

    public static ConfigurableApplicationContext context = null;

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

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {  

                if (container instanceof TomcatEmbeddedServletContainerFactory) {  
                   TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;  

                    Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                    connector.setPort(httpPort);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                }
            }
        };
    }

    public static void main(String[] args) {
            // ...
        }

}

我目前在message微服务里添加了同时支持http和https代码,想法是:网关走http,websocket走https

以上代码:打印信息

2019-06-22 13:42:56.009 [DiscoveryClient-InstanceInfoReplicator-0] INFO  [com.netflix.discovery.DiscoveryClient:804] [,] - DiscoveryClient_MESSAGE-SERVICE/message-service:192.168.60.92:9007 - registrati
on status: 204
2019-06-22 13:42:56.694 [main] INFO  [org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration$HystrixMetricsPollerConfiguration:138] [,] - Starting poller
2019-06-22 13:42:57.101 [SimpleAsyncTaskExecutor-1] INFO  [org.springframework.amqp.rabbit.connection.CachingConnectionFactory:360] [,] - Created new connection: rabbitConnectionFactory#73f79b74:0/Simpl
eConnection@790ff004 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 49865]
2019-06-22 13:42:57.320 [main] INFO  [org.apache.coyote.http11.Http11NioProtocol:179] [,] - Initializing ProtocolHandler ["https-jsse-nio-9007"]
2019-06-22 13:42:57.380 [main] INFO  [org.apache.coyote.http11.Http11NioProtocol:179] [,] - Starting ProtocolHandler ["https-jsse-nio-9007"]
2019-06-22 13:42:58.714 [main] INFO  [org.apache.tomcat.util.net.NioSelectorPool:179] [,] - Using a shared selector for servlet write/read
2019-06-22 13:42:58.749 [main] INFO  [org.apache.coyote.http11.Http11NioProtocol:179] [,] - Initializing ProtocolHandler ["http-nio-9007"]
2019-06-22 13:42:58.752 [main] ERROR [org.apache.coyote.http11.Http11NioProtocol:181] [,] - Failed to initialize end point associated with ProtocolHandler ["http-nio-9007"]
java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_111]
        at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_111]

请教各位大佬,怎么更优雅、简洁地解决小弟难题呢?请赐教,不胜感激!

  • 写回答

1条回答 默认 最新

  • java奋斗者 2024-05-10 10:06
    关注

    你的message-service服务配置了同时支持HTTP和HTTPS,但看起来在试图启动时两种协议都在尝试使用相同的端口(9007)。从你提供的日志中,错误java.net.BindException: Address already in use: bind表明端口9007已经被占用,因此无法再次绑定。

    一般而言,HTTP和HTTPS需要绑定在不同的端口上。在你的配置中,HTTPS配置了端口443,但你的HTTP配置(httpPort)也似乎在尝试使用端口9007,这可能是问题所在。

    解决这个问题,你需要为HTTP配置另一个端口,确保没有端口冲突。下面是如何在Spring Boot应用中同时启用HTTP和HTTPS端口,你可以在你的application.properties中定义:

    # 通用配置
    spring.application.name=message-service
    # HTTPS配置
    server.port=443  
    server.ssl.key-store=classpath:aliyun.pfx  
    server.ssl.key-store-password=8vwAq72s  
    server.ssl.key-store-type=PKCS12
    # HTTP配置(需要另外一个未使用的端口)
    http.port=8080 
    

    然后在你的启动入口代码中使用这个额外的HTTP端口:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
    import org.springframework.boot.web.server.WebServerFactoryCustomizer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class ServerConfig {
    
        @Value("${http.port}")
        private int httpPort;
    
        @Bean
        public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
            return factory -> {
                factory.addAdditionalTomcatConnectors(createStandardConnector());
            };
        }
    
        private Connector createStandardConnector() {
            Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
            connector.setPort(httpPort);
            return connector;
        }
    }
    

    以上代码使用了一个新属性http.port用于配置HTTP连接器。

    至于你的Zuul网关服务,确保你在Zuul的路由配置中正确地指向了message-service的HTTPS端口(如果你的Zuul网关也启用了SSL)。另外,如果你使用的是Eureka服务注册和发现,那么确保message-service将正确的端口信息注册到了Eureka。

    在Zuul配置中,你可能需要类似以下的配置(在application.ymlapplication.properties):

    zuul:
      routes:
        message-service:
          path: /message/**
          serviceId: message-service
          url: https://message-service-host:443
    

    确保message-service-host是你message-service服务的主机名或IP地址。

    如果你的服务都注册到了同一个Eureka服务器,那么理论上不需要在Zuul配置中指定服务的具体URL,因为Zuul将会通过服务ID自动从Eureka发现服务。只需要确保服务注册包含正确的端口和安全配置。

    最后,WebSocket配置可能需要额外的注意,因为与HTTP/HTTPS不完全相同。如果你的WebSocket连接直接指向message-service,确保WebSocket也配置了SSL,以便通过wss协议工作。

    评论

报告相同问题?