Merge pull request #9170 from mdabrowski-eu/BAEL-3863

BAEL-3863 Introduction to Finagle
This commit is contained in:
rpvilao 2020-05-13 10:53:51 +02:00 committed by GitHub
commit 856b818a2f
6 changed files with 129 additions and 0 deletions

35
libraries-rpc/pom.xml Normal file
View File

@ -0,0 +1,35 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-rpc</artifactId>
<name>libraries-rpc</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>finagle-core_2.13</artifactId>
<version>${finagle.core.version}</version>
</dependency>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>finagle-http_2.13</artifactId>
<version>${finagle.http.version}</version>
</dependency>
</dependencies>
<properties>
<finagle.core.version>20.4.0</finagle.core.version>
<finagle.http.version>20.4.0</finagle.http.version>
</properties>
</project>

View File

@ -0,0 +1,18 @@
package com.baeldung.rpc.finagle;
import com.twitter.finagle.Service;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.twitter.finagle.http.Status;
import com.twitter.io.Buf;
import com.twitter.io.Reader;
import com.twitter.util.Future;
public class GreetingService extends Service<Request, Response> {
@Override
public Future<Response> apply(Request request) {
String greeting = "Hello " + request.getParam("name");
Reader<Buf> reader = Reader.fromBuf(new Buf.ByteArray(greeting.getBytes(), 0, greeting.length()));
return Future.value(Response.apply(request.version(), Status.Ok(), reader));
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.rpc.finagle;
import com.twitter.finagle.Service;
import com.twitter.finagle.SimpleFilter;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.twitter.util.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogFilter extends SimpleFilter<Request, Response> {
private static final Logger logger = LoggerFactory.getLogger(LogFilter.class);
@Override
public Future<Response> apply(Request request, Service<Request, Response> service) {
logger.info("Request host:" + request.host().getOrElse(() -> ""));
logger.info("Request params:");
request.getParams().forEach(entry -> logger.info("\t" + entry.getKey() + " : " + entry.getValue()));
return service.apply(request);
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,40 @@
package com.baeldung.rpc.finagle;
import com.twitter.finagle.Http;
import com.twitter.finagle.Service;
import com.twitter.finagle.http.Method;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.twitter.util.Await;
import com.twitter.util.Future;
import org.junit.Test;
import scala.runtime.BoxedUnit;
import static org.junit.Assert.assertEquals;
public class FinagleIntegrationTest {
@Test
public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception {
// given
Service serverService = new LogFilter().andThen(new GreetingService());
Http.serve(":8080", serverService);
Service<Request, Response> clientService = new LogFilter().andThen(Http.newService(":8080"));
// when
Request request = Request.apply(Method.Get(), "/?name=John");
request.host("localhost");
Future<Response> response = clientService.apply(request);
// then
Await.result(response
.onSuccess(r -> {
assertEquals("Hello John", r.getContentString());
return BoxedUnit.UNIT;
})
.onFailure(r -> {
throw new RuntimeException(r);
})
);
}
}

View File

@ -509,6 +509,7 @@
<module>libraries-http-2</module>
<module>libraries-io</module>
<module>libraries-primitive</module>
<module>libraries-rpc</module>
<module>libraries-security</module>
<module>libraries-server</module>
<module>libraries-testing</module>