簡體   English   中英

在單個應用程序上運行 Spring 引導服務器和 HttpServer?

[英]Run Spring Boot server and HttpServer on single application?

我有使用 controller 啟動HttpServerinit方法:

public void init() {
    
        GatewayServer server = new GatewayServer("some_host", 8080);

        server.registerController(WorkshopOrderEndpoint.class);

        ControllerFactory.createController();

        server.startServer();
}

這是GatewayServer.class

public class GatewayServer {
    private static Logger logger = LogManager.getFormatterLogger(GatewayServer.class);

    private final String serverHost;

    private final String serverPort;

    private URI address;

    private ResourceConfig resourceConfig = null;

    private com.sun.net.httpserver.HttpServer server;

    public GatewayServer(final String host, final Integer port) {
        serverHost = host;
        serverPort = String.valueOf(port);

        try {
            logger.info("HTTP: Create Http-Server.");
            resourceConfig = new ResourceConfig();

            address = new URI(String.format("http://%s:%s/", serverHost, serverPort));

        } catch (URISyntaxException ex) {
            logger.error("HTTP: %s", ex.getMessage());
            LoggingHelper.sendExceptionLog(ex, "STATUS_URI_ERROR", "URI Encoding error.");
        } catch (ProcessingException ex) {
            logger.error("HTTP: %s", ex.getMessage());
            LoggingHelper.sendExceptionLog(ex, "STATUS_HTTP_ERROR", "HTTP-Server start error.");
        }

    }

    public void registerController(Class<?> controller) {
        if (resourceConfig != null) {
            logger.info("HTTP: Register Controller: %s", controller.getName());
            resourceConfig.register(controller);
        }
    }

    public void startServer() {
        server = JdkHttpServerFactory.createHttpServer(address, resourceConfig, false);
        logger.info("HTTP: Start Http-Server. Adress: %s", address);
        server.start();
    }

    public void stopServer(int delay) {
        logger.info("HTTP: Stop Http-Server. Address: %s", address);
        server.stop(delay);
    }
}

這是純粹的 java 應用程序,我想通過將此代碼添加到init()方法來啟動 Spring Server 以運行Eureka Server

 SpringRestApplication springRestApplication = new SpringRestApplication();
    springRestApplication.start();

其中SpringRestApplication.class正在啟動 Spring 引導服務器:

@SpringBootApplication
@EnableEurekaServer
public class SpringRestApplication {

    public void start() {
        
        SpringApplication.run(SpringRestApplication.class, new String[0]);
    }
}

我想在同一主機上運行兩台服務器,但端口不同,是否可以將Spring Boot Tomcat serverHttpServer連接起來?

您可以在不同的端口上運行兩者。

Eugene 展示了幾個更改 Spring 引導應用程序端口的選項: https://www.baeldung.com/spring-boot-change-port

這是最直接的:

public void start() {
    SpringApplication app = new SpringApplication(SpringRestApplication.class);
    app.setDefaultProperties(Collections
      .singletonMap("server.port", "8083"));
    app.run(args);
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM