-
Notifications
You must be signed in to change notification settings - Fork 54
/
HelloConfiguration.java
59 lines (51 loc) · 2.36 KB
/
HelloConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package example.springframework.boot.tomcat;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.healthcheck.HealthChecker;
import com.linecorp.armeria.server.tomcat.TomcatService;
import com.linecorp.armeria.spring.ArmeriaServerConfigurator;
/**
* Configures an Armeria {@link Server} to redirect the incoming requests to the Tomcat instance provided by
* Spring Boot. It also sets up a {@link HealthChecker} so that it works well with a load balancer.
*/
@Configuration
public class HelloConfiguration {
/**
* Extracts a Tomcat {@link Connector} from Spring webapp context.
*/
public static Connector getConnector(ServletWebServerApplicationContext applicationContext) {
final TomcatWebServer container = (TomcatWebServer) applicationContext.getWebServer();
// Start the container to make sure all connectors are available.
container.start();
return container.getTomcat().getConnector();
}
/**
* Returns a new {@link HealthChecker} that marks the server as unhealthy when Tomcat becomes unavailable.
*/
@Bean
public HealthChecker tomcatConnectorHealthChecker(ServletWebServerApplicationContext applicationContext) {
final Connector connector = getConnector(applicationContext);
return () -> connector.getState().isAvailable();
}
/**
* Returns a new {@link TomcatService} that redirects the incoming requests to the Tomcat instance
* provided by Spring Boot.
*/
@Bean
public TomcatService tomcatService(ServletWebServerApplicationContext applicationContext) {
return TomcatService.forConnector(getConnector(applicationContext));
}
/**
* Returns a new {@link ArmeriaServerConfigurator} that is responsible for configuring a {@link Server}
* using the given {@link ServerBuilder}.
*/
@Bean
public ArmeriaServerConfigurator armeriaServiceInitializer(TomcatService tomcatService) {
return sb -> sb.service("prefix:/", tomcatService);
}
}