BAEL-2715: Added code samples for Netty server configuration
This commit is contained in:
parent
65cbf46e6b
commit
b7cc946a32
1
pom.xml
1
pom.xml
@ -575,6 +575,7 @@
|
|||||||
<module>spring-5-reactive-client</module>
|
<module>spring-5-reactive-client</module>
|
||||||
<module>spring-5-reactive-oauth</module>
|
<module>spring-5-reactive-oauth</module>
|
||||||
<module>spring-5-reactive-security</module>
|
<module>spring-5-reactive-security</module>
|
||||||
|
<module>spring-5-reactive-netty</module>
|
||||||
<module>spring-5-security</module>
|
<module>spring-5-security</module>
|
||||||
<module>spring-5-security-oauth</module>
|
<module>spring-5-security-oauth</module>
|
||||||
|
|
||||||
|
11
spring-5-reactive-netty/.gitignore
vendored
Normal file
11
spring-5-reactive-netty/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Folders #
|
||||||
|
**/.idea
|
||||||
|
**/target
|
||||||
|
|
||||||
|
# Files #
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
3
spring-5-reactive-netty/README.md
Normal file
3
spring-5-reactive-netty/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
## Spring 5 Reactive Project With Netty Server
|
||||||
|
|
||||||
|
Includes configuration options for Netty server.
|
51
spring-5-reactive-netty/pom.xml
Normal file
51
spring-5-reactive-netty/pom.xml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-5-reactive-netty</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-5-reactive-netty</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<description>Spring 5 sample project about reactive web with Netty server</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.serverconfig;
|
||||||
|
|
||||||
|
import com.baeldung.serverconfig.client.GreetingWebClient;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ServerConfigApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ServerConfigApplication.class, args);
|
||||||
|
|
||||||
|
GreetingWebClient webClient = new GreetingWebClient();
|
||||||
|
webClient.getResult();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.serverconfig.client;
|
||||||
|
|
||||||
|
import io.netty.handler.ssl.SslContext;
|
||||||
|
import io.netty.handler.ssl.SslContextBuilder;
|
||||||
|
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.netty.http.client.HttpClient;
|
||||||
|
|
||||||
|
public class GreetingWebClient {
|
||||||
|
|
||||||
|
private WebClient client = getWebClient();
|
||||||
|
|
||||||
|
public void getResult() {
|
||||||
|
System.out.println("Mono");
|
||||||
|
Mono<String> greetingMono = client.get()
|
||||||
|
.uri("/greet/{name}", "baeldung")
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(String.class);
|
||||||
|
|
||||||
|
greetingMono.subscribe(System.out::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private WebClient getWebClient() {
|
||||||
|
SslContext sslContext = SslContextBuilder
|
||||||
|
.forClient()
|
||||||
|
.trustManager(InsecureTrustManagerFactory.INSTANCE)
|
||||||
|
.build();
|
||||||
|
HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
|
||||||
|
return WebClient.builder()
|
||||||
|
.baseUrl("https://localhost:8443")
|
||||||
|
.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.serverconfig.server;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/greet")
|
||||||
|
public class GreetingController {
|
||||||
|
|
||||||
|
private final GreetingService greetingService;
|
||||||
|
|
||||||
|
public GreetingController(GreetingService greetingService) {
|
||||||
|
this.greetingService = greetingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{name}")
|
||||||
|
private Mono<String> greet(@PathVariable String name) {
|
||||||
|
return greetingService.greet(name);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.serverconfig.server;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GreetingService {
|
||||||
|
|
||||||
|
public Mono<String> greet(String name) {
|
||||||
|
return Mono.just("Greeting " + name);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.serverconfig.server;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||||
|
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
|
||||||
|
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.netty.http.server.HttpServer;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class NettyWebServerFactoryPortCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(NettyReactiveWebServerFactory serverFactory) {
|
||||||
|
serverFactory.addServerCustomizers(new PortCustomizer(8443));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class PortCustomizer implements NettyServerCustomizer {
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
private PortCustomizer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpServer apply(HttpServer httpServer) {
|
||||||
|
return httpServer.port(port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.serverconfig.server;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||||
|
import org.springframework.boot.web.server.Ssl;
|
||||||
|
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class NettyWebServerFactorySslCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(NettyReactiveWebServerFactory serverFactory) {
|
||||||
|
Ssl ssl = new Ssl();
|
||||||
|
ssl.setEnabled(true);
|
||||||
|
ssl.setKeyStore("classpath:sample.jks");
|
||||||
|
ssl.setKeyAlias("alias");
|
||||||
|
ssl.setKeyPassword("password");
|
||||||
|
ssl.setKeyStorePassword("secret");
|
||||||
|
serverFactory.setSsl(ssl);
|
||||||
|
serverFactory.setPort(8443);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
31
spring-5-reactive-netty/src/main/resources/logback.xml
Normal file
31
spring-5-reactive-netty/src/main/resources/logback.xml
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="Console"
|
||||||
|
class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||||
|
<Pattern>
|
||||||
|
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
|
||||||
|
</Pattern>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="AccessLog" class="ch.qos.logback.core.FileAppender">
|
||||||
|
<file>netty-access.log</file>
|
||||||
|
<encoder>
|
||||||
|
<pattern>%msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="Async" class="ch.qos.logback.classic.AsyncAppender">
|
||||||
|
<appender-ref ref="AccessLog"/>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="reactor.netty.http.server.AccessLog" level="INFO" additivity="false">
|
||||||
|
<appender-ref ref="Console"/>
|
||||||
|
<appender-ref ref="Async"/>
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="Console"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
BIN
spring-5-reactive-netty/src/main/resources/sample.jks
Normal file
BIN
spring-5-reactive-netty/src/main/resources/sample.jks
Normal file
Binary file not shown.
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.serverconfig.server;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@WebFluxTest
|
||||||
|
public class GreetingControllerIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebTestClient webClient;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private GreetingService greetingService;
|
||||||
|
|
||||||
|
private final String name = "baeldung";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
when(greetingService.greet(name)).thenReturn(Mono.just("Hello baeldung"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldGreet() {
|
||||||
|
webClient.get().uri("/greet/{name}", name)
|
||||||
|
.exchange()
|
||||||
|
.expectStatus()
|
||||||
|
.isOk()
|
||||||
|
.expectBody(String.class)
|
||||||
|
.isEqualTo("Hello baeldung");
|
||||||
|
}
|
||||||
|
}
|
13
spring-5-reactive-netty/src/test/resources/logback-test.xml
Normal file
13
spring-5-reactive-netty/src/test/resources/logback-test.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||||
|
<Pattern>
|
||||||
|
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
|
||||||
|
</Pattern>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="Console"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user