-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jetty10 and Jetty11 modules (#4845)
Motivations: It would be nice if we support Jetty 10 and Jetty 11 Modifications: - Added Jetty10 and Jetty11 module - There're quite a few breaking changes when updating to the version 10. - `eventListeners` and `lifeCycleListeners` are both `EventListener`. https://github.com/line/armeria/blob/fbab0e3e5f3ba576a5db9bfc8ca1980d60b41006/jetty/jetty9/src/main/java/com/linecorp/armeria/server/jetty/JettyServiceBuilder.java#L51-L52 - Setting a request content has changed. - Creating HttpFields(Http headers) is changed. - `HttpChannel` is created from `ArmeriaHttpConnection`. - There aren't many breaking changes between 10 and 11 except that 11 uses Jakarta while 10 uses Javax - Both requires slf4j 2.x - We cannot exclude slf4j 1.x completely at the moment because the core module added it with the api configuration. - Will revisit this later if this becomes a problem. - Updated Jetty 9 version from 9.4.50.v20221201 to 9.4.51.v20230217 - Added Spring Boot Jetty example Result: - You can now use Armeria with Jetty 10 and Jetty 11
- Loading branch information
Showing
46 changed files
with
2,457 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
plugins { | ||
alias libs.plugins.spring.boot | ||
} | ||
|
||
dependencies { | ||
implementation project(':core') | ||
implementation project(':spring:boot3-starter') | ||
implementation project(':jetty11') | ||
|
||
implementation libs.slf4j2.api | ||
implementation libs.spring.boot3.starter.jetty | ||
|
||
implementation(libs.spring.boot3.starter.web) { | ||
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' | ||
} | ||
annotationProcessor libs.spring.boot3.configuration.processor | ||
|
||
runtimeOnly project(':spring:boot3-actuator-starter') | ||
|
||
testImplementation libs.assertj | ||
testImplementation libs.junit5.jupiter.api | ||
testImplementation libs.logback14 | ||
testImplementation libs.spring.boot3.starter.test | ||
} |
82 changes: 82 additions & 0 deletions
82
...pring-boot-jetty/src/main/java/example/springframework/boot/jetty/HelloConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package example.springframework.boot.jetty; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.util.Loader; | ||
import org.eclipse.jetty.webapp.WebAppContext; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer; | ||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; | ||
import org.springframework.boot.web.embedded.jetty.JettyWebServer; | ||
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.ServerBuilder; | ||
import com.linecorp.armeria.server.healthcheck.HealthChecker; | ||
import com.linecorp.armeria.server.jetty.JettyService; | ||
import com.linecorp.armeria.spring.ArmeriaServerConfigurator; | ||
|
||
import jakarta.servlet.Servlet; | ||
|
||
/** | ||
* Configures an Armeria server to redirect the incoming requests to the Jetty 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 { | ||
|
||
/** | ||
* Returns a new {@link HealthChecker} that marks the server as unhealthy when Tomcat becomes unavailable. | ||
*/ | ||
@Bean | ||
public HealthChecker jettyHealthChecker(ServletWebServerApplicationContext applicationContext) { | ||
final Server server = jettyServer(applicationContext).getServer(); | ||
return server::isRunning; | ||
} | ||
|
||
/** | ||
* Returns a new {@link JettyService} that redirects the incoming requests to the Jetty instance | ||
* provided by Spring Boot. | ||
*/ | ||
@Bean | ||
public JettyService jettyService(ServletWebServerApplicationContext applicationContext) { | ||
final JettyWebServer jettyWebServer = jettyServer(applicationContext); | ||
return JettyService.of(jettyWebServer.getServer(), null, false); | ||
} | ||
|
||
/** | ||
* Returns a new {@link ArmeriaServerConfigurator} that is responsible for configuring a {@link Server} | ||
* using the given {@link ServerBuilder}. | ||
*/ | ||
@Bean | ||
public ArmeriaServerConfigurator armeriaServiceInitializer(JettyService jettyService) { | ||
return sb -> sb.serviceUnder("/", jettyService); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class, WebAppContext.class }) | ||
static class EmbeddedJetty { | ||
|
||
@Bean | ||
JettyServletWebServerFactory jettyServletWebServerFactory( | ||
ObjectProvider<JettyServerCustomizer> serverCustomizers) { | ||
final JettyServletWebServerFactory factory = new JettyServletWebServerFactory() { | ||
|
||
@Override | ||
protected JettyWebServer getJettyWebServer(Server server) { | ||
return new JettyWebServer(server, true); | ||
} | ||
}; | ||
factory.getServerCustomizers().addAll(serverCustomizers.orderedStream().toList()); | ||
return factory; | ||
} | ||
} | ||
|
||
/** | ||
* Extracts a Jetty {@link Server} from Spring webapp context. | ||
*/ | ||
private static JettyWebServer jettyServer(ServletWebServerApplicationContext applicationContext) { | ||
return (JettyWebServer) applicationContext.getWebServer(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...s/spring-boot-jetty/src/main/java/example/springframework/boot/jetty/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package example.springframework.boot.jetty; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class HelloController { | ||
@RequestMapping(method = RequestMethod.GET, path = "/") | ||
String index() { | ||
return "index"; | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, path = "/hello") | ||
String hello() { | ||
return "Hello, World"; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/spring-boot-jetty/src/main/java/example/springframework/boot/jetty/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package example.springframework.boot.jetty; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Main { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Main.class, args); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/spring-boot-jetty/src/main/resources/config/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
spring.profiles.active: local | ||
# Prevent the embedded Tomcat from opening a TCP/IP port. | ||
server.port: -1 | ||
--- | ||
spring.config.activate.on-profile: local | ||
armeria: | ||
ports: | ||
- port: 8080 | ||
protocols: HTTP |
Empty file.
Empty file.
33 changes: 33 additions & 0 deletions
33
...ring-boot-jetty/src/test/java/example/springframework/boot/jetty/HelloControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package example.springframework.boot.jetty; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
@ActiveProfiles("testbed") | ||
@WebMvcTest(HelloController.class) | ||
class HelloControllerTest { | ||
@Inject | ||
private MockMvc mvc; | ||
|
||
@Test | ||
void index() throws Exception { | ||
mvc.perform(MockMvcRequestBuilders.get("/")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("index")); | ||
} | ||
|
||
@Test | ||
void hello() throws Exception { | ||
mvc.perform(MockMvcRequestBuilders.get("/hello")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("Hello, World")); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ing-boot-jetty/src/test/java/example/springframework/boot/jetty/HelloIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package example.springframework.boot.jetty; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import com.linecorp.armeria.client.WebClient; | ||
import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.server.Server; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
@ActiveProfiles("testbed") | ||
@SpringBootTest( | ||
classes = { | ||
HelloConfiguration.class, | ||
HelloController.class | ||
}, | ||
webEnvironment = WebEnvironment.DEFINED_PORT) | ||
@EnableAutoConfiguration | ||
class HelloIntegrationTest { | ||
|
||
@Inject | ||
private Server server; | ||
private WebClient client; | ||
|
||
@BeforeEach | ||
void initClient() { | ||
if (client == null) { | ||
client = WebClient.of("http://127.0.0.1:" + server.activeLocalPort()); | ||
} | ||
} | ||
|
||
@Test | ||
void index() { | ||
final AggregatedHttpResponse res = client.get("/").aggregate().join(); | ||
assertThat(res.status()).isEqualTo(HttpStatus.OK); | ||
assertThat(res.contentUtf8()).isEqualTo("index"); | ||
} | ||
|
||
@Test | ||
void hello() throws Exception { | ||
final AggregatedHttpResponse res = client.get("/hello").aggregate().join(); | ||
assertThat(res.status()).isEqualTo(HttpStatus.OK); | ||
assertThat(res.contentUtf8()).isEqualTo("Hello, World"); | ||
} | ||
|
||
@Test | ||
void healthCheck() throws Exception { | ||
final AggregatedHttpResponse res = client.get("/internal/healthcheck").aggregate().join(); | ||
assertThat(res.status()).isEqualTo(HttpStatus.OK); | ||
assertThat(res.contentUtf8()).isEqualTo("{\"healthy\":true}"); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/spring-boot-jetty/src/test/resources/application-testbed.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Prevent the embedded Tomcat from opening a TCP/IP port. | ||
server.port: -1 | ||
--- | ||
armeria: | ||
ports: | ||
- port: 0 | ||
protocols: HTTP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.