java - 在单个应用程序上运行 Spring Boot 服务器和 HttpServer?
问题描述
我有以控制器init
开头的方法:HttpServer
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);
}
}
Eureka Server
这是纯 java 应用程序,我想通过将此代码添加到方法中来启动 Spring Server 以便运行init()
:
SpringRestApplication springRestApplication = new SpringRestApplication();
springRestApplication.start();
SpringRestApplication.class在哪里启动 Spring Boot 服务器:
@SpringBootApplication
@EnableEurekaServer
public class SpringRestApplication {
public void start() {
SpringApplication.run(SpringRestApplication.class, new String[0]);
}
}
我想在同一主机上运行两台服务器,但可以连接不同的端口Spring Boot Tomcat server
吗HttpServer
?
解决方案
您可以在不同的端口上运行这两者。
Eugene 展示了几个更改 Spring Boot 应用程序端口的选项: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);
}
推荐阅读
- function - 在另一个函数中使用相同的类对象
- firebase - Firebase 应用程序的 Google OAuth 同意屏幕
- apache-kafka - 消费者死后,消费者群体还能活多久?
- javascript - 反应 JS。NavLink activeClassName ICON, IMAGE
- java - 从 Java 中的泛型约束接口继承
- networking - 如何收集 k8s 集群 pod 的所有 igress 和 egress 流量?
- netlogo - 为什么在 IF 语句中使用 map 会出现问题?
- vue.js - 在 vuejs 中将 Dashbord 和 Forum 组件与 App 组件分开
- android - ListView 的自定义适配器忽略设置值
- angular - Angular 材料中的中心对齐内容不起作用