Code Monkey home page Code Monkey logo

reactive-grpc-spring-boot-starter's Introduction

Spring Boot Starter Reactive gRPC

Spring Boot 2.0 starter for Reactive gRPC (https://github.com/salesforce/reactive-grpc) to bring reactive style for grpc-java.

How to use?

  • Please clone the repository and install reactive-grpc-spring-boot-starter artifact to your local Maven repository.
$ git clone [email protected]:linux-china/reactive-grpc-spring-boot-starter.git
$ cd reactive-grpc-spring-boot-starter
$ mvn -P release -DskipTests clean install
  • Add dependency and repository in your pom.xml. Now reactive-grpc-spring-boot-starter hosted by jitpack
<project>
 ...
 <dependencies>
     <dependency>
        <groupId>com.github.linux-china</groupId>
        <artifactId>reactive-grpc-spring-boot-starter</artifactId>
        <version>1.0.0-SNAPSHOT</version>
     </dependency>
 </dependencies>
 
</project>
  • Add protobuf-maven-plugin with Reactor-gRPC protocPlugin included. Attention: proto files should be in "src/main/proto" directory
<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <parameters>true</parameters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protobuf-java.version}:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
                    </pluginArtifact>
                    <protocPlugins>
                        <protocPlugin>
                            <id>reactor-grpc</id>
                            <groupId>com.salesforce.servicelibs</groupId>
                            <artifactId>reactor-grpc</artifactId>
                            <version>${reactive-grpc.version}</version>
                            <mainClass>com.salesforce.reactorgrpc.ReactorGrpcGenerator</mainClass>
                        </protocPlugin>
                    </protocPlugins>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>
  • Implement Reactive Service with @ReactiveGrpcService annotation.
@Service
@ReactiveGrpcService
public class ReactiveGreeterImpl extends ReactorGreeterGrpc.GreeterImplBase {
    @Override
    public Mono<HelloReply> sayHello(Mono<HelloRequest> request) {
        return request.map(helloRequest -> HelloReply.newBuilder().setMessage("Hello " + helloRequest.getName()).build());
    }

    @Override
    public Mono<HelloReply> sayHelloAgain(Mono<HelloRequest> request) {
        return request.map(helloRequest -> HelloReply.newBuilder().setMessage("Hello Again " + helloRequest.getName()).build());
    }
}
  • Start you Spring application and you will get following output on your console.
09:40:17.366 [INFO ] o.m.s.b.r.g.ReactiveGrpcAutoConfiguration - gRPC server started on: 50051
09:40:17.379 [INFO ] o.m.s.b.r.g.ReactiveGrpcAutoConfiguration - Reactive gRPC service exposed: org.mvnsearch.spring.boot.reactive.grpc.demo.ReactiveGreeterImpl

  • Use Evans to test gRPC service
$ evans src/main/proto/greeter.proto

Reactive gRPC configuration

You don't need to setup anything for Reactive gRPC service. If you want to change listen port(default is 50051), please change as following in application.properties:

grpc.reactive.port=50051

FAQ

What's difference with gRPC Spring Boot Starter

gRPC has different starters to integration with Spring Boot, for example:

The main difference is code style in these starters. If you like Reactive style, please choose Reactive gRPC. If you think native style fine, please use starters above.

  • gRPC native style:
//server side
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
  HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
  responseObserver.onNext(reply);
  responseObserver.onCompleted();
}
//client side
 asyncStub.sayHello(HelloRequest.newBuilder().setName(name).build(), new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply reply) {
                
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        });
  • gRPC reactive style:
//server side
public Mono<HelloReply> sayHello(Mono<HelloRequest> request) {
  return request.map(helloRequest -> HelloReply.newBuilder().setMessage("Hello " + helloRequest.getName()).build());
}
//client side
 Mono<HelloRequest> request = Mono.just(HelloRequest.newBuilder().setName("Jack").build());
 request.as(greeter::sayHello)
         .map(HelloReply::getMessage)
         .subscribe(System.out::println);

Convert between DDD model(entity,value object) and Protobuf message

Please refer MapStruct to map DDD model and Protobuf message, and MapStruct 1.3 with Protobuf builder support.

Quick test

Please execute "mvn -DskipTests clean package" and run ReactiveGrpcDemoApplication

Todo

server.ssl.enabled=true
server.ssl.key-store=${user.home}/domain_certs/localhost.com/localhost.com.p12
server.ssl.key-store-type=PKCS12
server.ssl.key-store-password=changeit

References

reactive-grpc-spring-boot-starter's People

Contributors

linux-china avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.