Merge pull request #9170 from mdabrowski-eu/BAEL-3863
BAEL-3863 Introduction to Finagle
This commit is contained in:
commit
856b818a2f
|
@ -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>
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue