• Stars
    star
    3,518
  • Rank 12,636 (Top 0.3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated 27 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Spring Boot starter module for gRPC framework.

gRPC Spring Boot Starter

Build master branch Maven Central with version prefix filter MIT License Crowdin

Client-Javadoc Server-Javadoc Common-Javadoc

README: English | 中文

Documentation: English | 中文

Features

  • Automatically configures and runs the gRPC server with your @GrpcService implementations

  • Automatically creates and manages your grpc channels and stubs with @GrpcClient

  • Supports other grpc-java flavors (e.g. Reactive gRPC (RxJava), grpc-kotlin, ...)

    • Server-side: Should work for all grpc-java flavors (io.grpc.BindableService based)
    • Client-side: Requires custom StubFactorys
      Currently build-in support:
      • grpc-java
      • (Please report missing ones, so we can add support for them)
  • Supports Spring-Security

  • Supports Spring Cloud

    • Server-side: Adds grpc-port information to the service registration details
      Currently natively supported:
    • Client-side: Reads the service's target addresses from spring's DiscoveryClient (all flavors)
  • Supports Spring Sleuth as distributed tracing solution
    (If brave-instrumentation-grpc is present)

  • Supports global and custom gRPC server/client interceptors

  • Automatic metric support (micrometer/actuator based)

  • Also works with (non-shaded) grpc-netty

Versions

The latest version is 2.14.0.RELEASE it was compiled with spring-boot 2.6.13 and spring-cloud 2021.0.5 but it is also compatible with a large variety of other versions. An overview of all versions and their respective library versions can be found in our documentation.

Note: This project can also be used without Spring-Boot, however that requires some manual bean configuration.

Usage

gRPC Server + Client

To add a dependency using Maven, use the following:

<dependency>
  <groupId>net.devh</groupId>
  <artifactId>grpc-spring-boot-starter</artifactId>
  <version>2.14.0.RELEASE</version>
</dependency>

To add a dependency using Gradle:

dependencies {
  implementation 'net.devh:grpc-spring-boot-starter:2.14.0.RELEASE'
}

gRPC Server

To add a dependency using Maven, use the following:

<dependency>
  <groupId>net.devh</groupId>
  <artifactId>grpc-server-spring-boot-starter</artifactId>
  <version>2.14.0.RELEASE</version>
</dependency>

To add a dependency using Gradle:

dependencies {
  implementation 'net.devh:grpc-server-spring-boot-starter:2.14.0.RELEASE'
}

Annotate your server interface implementation(s) with @GrpcService

@GrpcService
public class GrpcServerService extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

}

By default, the grpc server will listen to port 9090. These and other settings can be changed via Spring's property mechanism. The server uses the grpc.server. prefix.

Refer to our documentation for more details.

gRPC Client

To add a dependency using Maven, use the following:

<dependency>
  <groupId>net.devh</groupId>
  <artifactId>grpc-client-spring-boot-starter</artifactId>
  <version>2.14.0.RELEASE</version>
</dependency>

To add a dependency using Gradle:

dependencies {
  compile 'net.devh:grpc-client-spring-boot-starter:2.14.0.RELEASE'
}

Annotate a field of your grpc client stub with @GrpcClient(serverName)

  • Do not use in conjunction with @Autowired or @Inject

    @GrpcClient("gRPC server name")
    private GreeterGrpc.GreeterBlockingStub greeterStub;

Note: You can use the same grpc server name for multiple channels and also different stubs (even with different interceptors).

Then you can send queries to your server just like this:

HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());

It is possible to configure the target address for each client individually. However in some cases, you can just rely on the default configuration. You can customize the default url mapping via NameResolver.Factory beans. If you don't configure that bean, then the default uri will be guessed using the default scheme and the name (e.g.: dns:/<name>):

These and other settings can be changed via Spring's property mechanism. The clients use the grpc.client.(serverName). prefix.

Refer to our documentation for more details.

Running with (non-shaded) grpc-netty

This library supports both grpc-netty and grpc-netty-shaded. The later one might prevent conflicts with incompatible grpc-versions or conflicts between libraries that require different versions of netty.

Note: If the shaded netty is present on the classpath, then this library will always favor it over the non-shaded grpc-netty one.

You can use it with Maven like this:

<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty</artifactId>
    <version>${grpcVersion}</version>
</dependency>

<!-- For both -->
<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-spring-boot-starter</artifactId>
    <version>...</version>
    <exclusions>
        <exclusion>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- For the server (only) -->
<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-server-spring-boot-starter</artifactId>
    <version>...</version>
    <exclusions>
        <exclusion>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- For the client (only) -->
<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-client-spring-boot-starter</artifactId>
    <version>...</version>
    <exclusions>
        <exclusion>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
        </exclusion>
    </exclusions>
</dependency>

and like this when using Gradle:

implementation "io.grpc:grpc-netty:${grpcVersion}"

implementation 'net.devh:grpc-spring-boot-starter:...' exclude group: 'io.grpc', module: 'grpc-netty-shaded' // For both
implementation 'net.devh:grpc-client-spring-boot-starter:...' exclude group: 'io.grpc', module: 'grpc-netty-shaded' // For the client (only)
implementation 'net.devh:grpc-server-spring-boot-starter:...' exclude group: 'io.grpc', module: 'grpc-netty-shaded' // For the server (only)

Example-Projects

Read more about our example projects here.

Troubleshooting

Refer to our documentation for help.

Contributing

Contributions are always welcomed! Please see CONTRIBUTING.md for detailed guidelines.

More Repositories

1

grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
Go
17,873
star
2

awesome-grpc

A curated list of useful resources for gRPC
7,459
star
3

go-grpc-middleware

Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.
Go
6,275
star
4

grpc-health-probe

A command-line tool to perform health-checks for gRPC applications in Kubernetes and elsewhere
Go
1,414
star
5

go-grpc-prometheus

Prometheus monitoring for your gRPC Go servers.
Go
1,334
star
6

polyglot

A universal grpc command line client
Java
530
star
7

grpc-opentracing

OpenTracing is a set of consistent, expressive, vendor-neutral APIs for distributed tracing and context propagation
Python
470
star
8

java-grpc-prometheus

Java interceptors which can be used to monitor Grpc services using Prometheus.
Java
224
star
9

grpc-httpjson-transcoding

Transcoding to provide HTTP/JSON interface for gRPC Service
C++
161
star
10

grpcdebug

grpcdebug is a command line interface focusing on simplifying the debugging process of gRPC applications.
Go
144
star
11

protoc-gen-grpc-gateway-ts

protoc-gen-grpc-gateway-ts is a Typescript client generator for the grpc-gateway project. It generates idiomatic Typescript clients that connect the web frontend and golang backend fronted by grpc-gateway.
Go
141
star
12

grpc-simon-says

Multiplayer Simon Says game using bidirectional gRPC streaming
Go
133
star
13

grpc-cloud-run-example

Go
120
star
14

meetup-kit

gRPC meet up kit in a box
37
star
15

grift

This repository hosts gRPC's support for Thrift IDL and protocol
Java
10
star
16

grpc-exchange-o-gram

Exchange-o-gram demo
C#
7
star
17

proto-converter

C++
4
star
18

grpcz-stackdriver

grpcz-monitoring - description: standalone agent for monitoring statistics from grpc library and publishing to various sinks (like stackdriver, serial port etc).
4
star
19

proto-field-extraction

C++
3
star
20

grpc-codelabs

Completed codelabs from the gRPC project.
Java
3
star
21

proto_processing_lib

C++
1
star