Update 64.13. Enable Multiple Listeners with Undertow.md
parent
10ec84b5f9
commit
e5f896258b
|
@ -1,22 +1,18 @@
|
|||
### 64.13. 启用Undertow的多监听器(Multiple Listeners)
|
||||
|
||||
### 64.13. 发现运行时的HTTP端口
|
||||
|
||||
你可以通过日志输出或它的EmbeddedServletContainer的EmbeddedWebApplicationContext获取服务器正在运行的端口。获取和确认服务器已经初始化的最好方式是添加一个`ApplicationListener<EmbeddedServletContainerInitializedEvent>`类型的`@Bean`,然后当事件发布时将容器pull出来。
|
||||
|
||||
使用`@WebIntegrationTests`的一个有用实践是设置`server.port=0`,然后使用`@Value`注入实际的('local')端口。例如:
|
||||
往`UndertowEmbeddedServletContainerFactory`添加一个`UndertowBuilderCustomizer`,然后添加一个监听者到`Builder`:
|
||||
```java
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
|
||||
@WebIntegrationTest("server.port:0")
|
||||
public class CityRepositoryIntegrationTests {
|
||||
@Bean
|
||||
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
|
||||
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
|
||||
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
|
||||
|
||||
@Autowired
|
||||
EmbeddedWebApplicationContext server;
|
||||
|
||||
@Value("${local.server.port}")
|
||||
int port;
|
||||
|
||||
// ...
|
||||
@Override
|
||||
public void customize(Builder builder) {
|
||||
builder.addHttpListener(8080, "0.0.0.0");
|
||||
}
|
||||
|
||||
});
|
||||
return factory;
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue