-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
378 additions
and
42 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
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
61 changes: 61 additions & 0 deletions
61
...tarter-demo/src/main/java/org/lognet/springboot/grpc/demo/ReactiveGreeterGrpcService.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,61 @@ | ||
package org.lognet.springboot.grpc.demo; | ||
|
||
import io.grpc.Status; | ||
import io.grpc.examples.reactor.ReactiveHelloRequest; | ||
import io.grpc.examples.reactor.ReactiveHelloResponse; | ||
import io.grpc.examples.reactor.ReactorReactiveGreeterGrpc; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.lognet.springboot.grpc.GRpcService; | ||
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler; | ||
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
|
||
@GRpcService | ||
@Slf4j | ||
public class ReactiveGreeterGrpcService extends ReactorReactiveGreeterGrpc.ReactiveGreeterImplBase { | ||
|
||
private ReactiveGreeterService reactiveGreeterService; | ||
|
||
public ReactiveGreeterGrpcService(ReactiveGreeterService reactiveGreeterService) { | ||
this.reactiveGreeterService = reactiveGreeterService; | ||
} | ||
|
||
@Override | ||
public Mono<ReactiveHelloResponse> greet(Mono<ReactiveHelloRequest> request) { | ||
return reactiveGreeterService.greet(request); | ||
|
||
} | ||
|
||
@Override | ||
public Flux<ReactiveHelloResponse> multiGreet(Mono<ReactiveHelloRequest> request) { | ||
return request.flatMapIterable(r -> | ||
IntStream.range(0, r.getName().length()) | ||
.mapToObj(i -> ReactiveHelloResponse.newBuilder() | ||
.setMessage(String.format("Hello %d,%s ", i, r.getName())) | ||
.build()) | ||
.toList() | ||
); | ||
} | ||
|
||
@Override | ||
public Flux<ReactiveHelloResponse> streamGreet(Flux<ReactiveHelloRequest> request) { | ||
return request.flatMap(r -> Mono.just( | ||
ReactiveHelloResponse.newBuilder() | ||
.setMessage(String.format("Hello ,%s ", r.getName())) | ||
.build() | ||
) | ||
); | ||
} | ||
|
||
@GRpcExceptionHandler | ||
public Status handle(Exception ex, GRpcExceptionScope scope) { | ||
var status = Status.INVALID_ARGUMENT.withDescription(ex.getLocalizedMessage()).withCause(ex); | ||
log.error("(GrpcExceptionAdvice) : ", ex); | ||
return status; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/ReactiveGreeterService.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,22 @@ | ||
package org.lognet.springboot.grpc.demo; | ||
|
||
import io.grpc.examples.reactor.ReactiveHelloRequest; | ||
import io.grpc.examples.reactor.ReactiveHelloResponse; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Service | ||
public class ReactiveGreeterService { | ||
public Mono<ReactiveHelloResponse> greet(Mono<ReactiveHelloRequest> request) { | ||
return Mono | ||
.from(request) | ||
.flatMap(r -> { | ||
if ("wolf".equalsIgnoreCase(r.getName())) { | ||
return Mono.error(new Exception("Wolf is not welcome!")); | ||
} | ||
return Mono.just(ReactiveHelloResponse.newBuilder() | ||
.setMessage("Hello " + r.getName()) | ||
.build()); | ||
}); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
grpc-spring-boot-starter-demo/src/main/proto/reactiveHello.proto
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,26 @@ | ||
syntax = "proto3"; | ||
|
||
|
||
option java_multiple_files = true; | ||
option java_package = "io.grpc.examples.reactor"; | ||
option java_outer_classname = "ReactiveHelloWorldProto"; | ||
|
||
/* | ||
* Define the service's operations | ||
*/ | ||
service ReactiveGreeter { | ||
rpc Greet (ReactiveHelloRequest) returns (ReactiveHelloResponse) {} | ||
rpc MultiGreet (ReactiveHelloRequest) returns (stream ReactiveHelloResponse) {} | ||
rpc StreamGreet (stream ReactiveHelloRequest) returns (stream ReactiveHelloResponse) {} | ||
} | ||
|
||
/* | ||
* Define the service's data structures | ||
*/ | ||
message ReactiveHelloRequest { | ||
string name = 1; | ||
} | ||
|
||
message ReactiveHelloResponse { | ||
string message = 1; | ||
} |
125 changes: 125 additions & 0 deletions
125
...boot-starter-demo/src/test/java/org/lognet/springboot/grpc/reactive/ReactiveDemoTest.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,125 @@ | ||
package org.lognet.springboot.grpc.reactive; | ||
|
||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.examples.reactor.ReactiveGreeterGrpc; | ||
import io.grpc.examples.reactor.ReactiveHelloRequest; | ||
import io.grpc.examples.reactor.ReactiveHelloResponse; | ||
import io.grpc.examples.reactor.ReactorReactiveGreeterGrpc; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.collection.IsIn; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.lognet.springboot.grpc.GrpcServerTestBase; | ||
import org.lognet.springboot.grpc.demo.DemoApp; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.hamcrest.Matchers; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; | ||
|
||
|
||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = DemoApp.class, webEnvironment = NONE) | ||
@ActiveProfiles("disable-security") | ||
public class ReactiveDemoTest extends GrpcServerTestBase { | ||
@Test | ||
public void grpcGreetTest() { | ||
String shrek = "Shrek"; | ||
String message = ReactiveGreeterGrpc.newBlockingStub(channel) | ||
.greet(ReactiveHelloRequest.newBuilder().setName(shrek).build()) | ||
.getMessage(); | ||
assertThat(message, containsString(shrek)); | ||
|
||
} | ||
|
||
@Test | ||
public void reactorGreetTest() { | ||
String shrek = "Shrek"; | ||
ReactiveHelloResponse helloResponse = ReactorReactiveGreeterGrpc.newReactorStub(channel) | ||
.greet(simpleRequest(shrek)) | ||
.block(Duration.ofSeconds(10)); | ||
assertThat(helloResponse, notNullValue()); | ||
|
||
assertThat(helloResponse.getMessage(), containsString(shrek)); | ||
|
||
} | ||
|
||
@Test | ||
public void reactorGreetFailureTest() { | ||
String shrek = "Wolf"; | ||
StatusRuntimeException e = assertThrows(StatusRuntimeException.class, () -> { | ||
|
||
ReactorReactiveGreeterGrpc.newReactorStub(channel) | ||
.greet(simpleRequest(shrek)) | ||
.block(Duration.ofSeconds(10)); | ||
}); | ||
assertThat(e.getMessage(), containsStringIgnoringCase("not welcome")); | ||
assertThat(e.getStatus().getCode(), is(Status.INVALID_ARGUMENT.getCode())); | ||
|
||
|
||
} | ||
|
||
@Test | ||
public void reactorMultiGreerTest() { | ||
String shrek = "Shrek"; | ||
List<ReactiveHelloResponse> greets = ReactorReactiveGreeterGrpc.newReactorStub(channel) | ||
.multiGreet(simpleRequest(shrek)) | ||
.collectList() | ||
.block(Duration.ofSeconds(10)); | ||
|
||
assertThat(greets, notNullValue()); | ||
assertThat(greets, hasSize(shrek.length())); | ||
|
||
assertThat(greets.stream().map(ReactiveHelloResponse::getMessage).toList(), everyItem(containsString(shrek))); | ||
|
||
} | ||
|
||
@Test | ||
public void reactorBidiGreerTest() { | ||
String[] names = new String[]{ | ||
"Shrek", | ||
"Fiona", | ||
"Robin", | ||
"Christopher" | ||
}; | ||
List<ReactiveHelloResponse> greets = ReactorReactiveGreeterGrpc.newReactorStub(channel) | ||
.streamGreet( | ||
Flux.fromStream(Arrays.stream(names).map(this::simpleRequest)) | ||
) | ||
.collectList() | ||
.block(Duration.ofSeconds(10)); | ||
|
||
assertThat(greets, notNullValue()); | ||
assertThat(greets, hasSize(name.length())); | ||
|
||
assertThat(greets.stream().map(ReactiveHelloResponse::getMessage).toList(), | ||
Matchers.everyItem(new IsIn<>(names) { | ||
@Override | ||
public boolean matches(Object actual) { | ||
return Arrays.stream(names) | ||
.anyMatch(a -> actual.toString().contains(a)); | ||
} | ||
}) | ||
); | ||
|
||
} | ||
|
||
private ReactiveHelloRequest simpleRequest(String name) { | ||
return ReactiveHelloRequest.newBuilder().setName(name).build(); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
...tarter-gradle-plugin/src/main/java/org/lognet/springboot/grpc/gradle/ReactiveFeature.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,10 @@ | ||
package org.lognet.springboot.grpc.gradle; | ||
|
||
public enum ReactiveFeature { | ||
|
||
|
||
OFF, | ||
REACTOR, | ||
RX | ||
|
||
} |
Oops, something went wrong.