Coder Social home page Coder Social logo

quarkus-grpc-client-extension's Introduction

โš ๏ธ No longer supported

This extension is no longer supported. Please use the official gRPC extension which is expected to be released as part of Quarkus 1.5.0.Final: https://github.com/quarkusio/quarkus/tree/master/extensions/grpc


Quarkus gRPC Client Extension

Extension to call gRPC services in your Quarkus application.

@ApplicationScoped
public class RouteGuideClient {

    private RouteGuideBlockingStub blockingStub;
    
    @Inject
    public RouteGuideClient(@Channel("route") ManagedChannel channel) {
        blockingStub = RouteGuideGrpc.newBlockingStub(channel);    
    }

    public Feature getFeature(int lat, int lon) {
        Point request = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
        return blockingStub.getFeature(request);
    }
}

You can inject named channels into your beans. Each channel has a name and is configured in your application.properties:

quarkus.grpc-client.route.host=localhost
quarkus.grpc-client.route.port=5050

The property names are made up of quarkus.grpc-client plus the name of the channel. Right now you can configure host and port. Channels are created with usePlaintext() at the moment, but TLS and support for additional settings (like timeouts) is in the works. Injected channels are created as singletons and are automatically shutdown when Quarkus is shutdown.

Getting Started

The gRPC client extension is not available in Maven Central. For now you have to clone the repository and install the extension in your local maven repository. Then follow these steps to write and deploy a simple hello world gRPC service:

Setup Project

Create a new project using the Quarkus archetype:

mvn io.quarkus:quarkus-maven-plugin:0.11.0:create \
    -DprojectGroupId=io.grpc.helloworld \
    -DprojectArtifactId=helloworld

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-grpc-client</artifactId>
    <version>${quarkus.grpc.client.version}</version>
</dependency>

Setup gRPC

To setup the gRPC code generation, add the following settings to your pom.xml:

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.6.2</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}
                </protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.19.0:exe:${os.detected.classifier}
                </pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    <plugins>
</build>

Define gRPC Client

You need a running gRPC service you can talk to. See the Quarkus gRPC extension on how to get your gRPC services up and running. Then in your client code, inject a managed channel and create a blocking and/or asynchronous stub:

@ApplicationScoped
public class RouteGuideClient {

    private RouteGuideStub asyncStub;
    private RouteGuideBlockingStub blockingStub;
    
    @Inject
    public RouteGuideClient(@Channel("route") ManagedChannel channel) {
        asyncStub = RouteGuideGrpc.newStub(channel);
        blockingStub = RouteGuideGrpc.newBlockingStub(channel);    
    }

    public Feature getFeature(int lat, int lon) {
        Point request = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
        return blockingStub.getFeature(request);
    }
}

Finally specify the host and port in your application.properties:

quarkus.grpc-client.route.host=localhost
quarkus.grpc-client.route.port=5050

Quickstart

If you want to see a more complex example, checkout the gRPC quickstart. It uses both the gRPC and the gRPC client extension to implement the route guide example provided by gRPC Java.

What's Missing

  • TLS
  • Better devmode support
  • More configuration options

See also quarkusio/quarkus#820

Have fun!

quarkus-grpc-client-extension's People

Contributors

hpehl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

smatijasevic

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.