Nic*_*aly 4 java jetty spring-boot
使用spring-boot
and jetty
,我希望能够将我的应用程序配置为侦听其他端口,这些端口在运行时以编程方式添加(+ 删除?)。
我试过的:
我遵循了本教程,它允许我侦听多个端口。这很完美,但不幸的是仅在启动时有效。
我已经尝试@Autowiring
了org.eclipse.jetty.server.Server
一个服务类,以便我可以添加连接器 - 我收到了错误No qualifying bean of type [org.eclipse.jetty.server.Server] found ...
build.gradle(依赖项
buildscript {
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
apply plugin: 'spring-boot'
...
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.eclipse.jetty:jetty-proxy:9.2.17.v20160517"
...
Run Code Online (Sandbox Code Playgroud)
不知道从这里尝试什么......
您可以Server
从 Boot's获取 Jetty ,JettyEmbeddedServletContainer
可从EmbeddedWebApplicationContext
. 一旦掌握了 ,Server
就可以使用 Jetty 的 API 向其添加新的连接器。
这是一个添加新连接器以响应ApplicationReadyEvent
发布的示例:
@Bean
public JettyCustomizer jettyCustomizer(EmbeddedWebApplicationContext context) {
return new JettyCustomizer(
(JettyEmbeddedServletContainer) context.getEmbeddedServletContainer());
}
static class JettyCustomizer implements ApplicationListener<ApplicationReadyEvent> {
private final JettyEmbeddedServletContainer container;
JettyCustomizer(JettyEmbeddedServletContainer container) {
this.container = container;
}
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
Server server = this.container.getServer();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8081);
server.addConnector(connector);
try {
connector.start();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to start connector", ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该在日志中看到从端口 8080 开始的默认连接器,然后是从 8081 开始的第二个连接器:
2016-08-16 10:28:57.476 INFO 71330 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@64bc21ac{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2016-08-16 10:28:57.478 INFO 71330 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2016-08-16 10:28:57.482 INFO 71330 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@664a9613{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2016-08-16 10:28:57.483 INFO 71330 --- [ main] sample.jetty.SampleJettyApplication : Started SampleJettyApplication in 1.838 seconds (JVM running for 2.132)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1948 次 |
最近记录: |