Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/doublemine/grpc-springboot-starter
Spring Boot starter module for gRPC framework.
https://github.com/doublemine/grpc-springboot-starter
grpc
Last synced: 13 days ago
JSON representation
Spring Boot starter module for gRPC framework.
- Host: GitHub
- URL: https://github.com/doublemine/grpc-springboot-starter
- Owner: Doublemine
- License: apache-2.0
- Created: 2018-01-10T14:28:37.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-22T07:34:12.000Z (almost 7 years ago)
- Last Synced: 2024-10-25T03:56:18.562Z (2 months ago)
- Topics: grpc
- Language: Kotlin
- Homepage:
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### [gRPC](https://github.com/grpc/grpc-java) Spring Boot Starter [![](https://jitpack.io/v/Doublemine/grpc-springboot-starter.svg)](https://jitpack.io/#Doublemine/grpc-springboot-starter) [![Build Status](https://travis-ci.org/Doublemine/grpc-springboot-starter.svg?branch=master)](https://travis-ci.org/Doublemine/grpc-springboot-starter)
### TODO
- [x] add client interceptor
### Usage
#### dependencies
##### Gradle
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```Step 2. Add the dependency
```gradle
dependencies {
compile 'com.github.Doublemine.grpc-springboot-starter:starter:1.0'
}
```##### Maven
Step 1. Add the JitPack repository to your build file
```xml
jitpack.io
https://jitpack.io
```
Step 2. Add the dependency
```xml
com.github.Doublemine.grpc-springboot-starter
starter
1.0
```#### Grpc Server:
##### Enable Server
- Use annotation `@EnableGrpcServer` or `grpc.server.enable=true` in application.properties to Enable Grpc Server.
- The default server port is `9494`.
- You can change the host and port in application.properties:
```properties
grpc.server.enable=true
```##### GrpcService
Annotate your server interface implementation(s) with ``@GrpcService``
```java
@GrpcService
public class HelloService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
```##### Interceptor
As private Intercept Or Annotate your ServerInterceptor interface implementation(s) with ``@GlobalInterceptor``
```java
@GlobalInterceptor // As Global interceptor
public class ServerLogInterceptor implements ServerInterceptor {private static final Logger logger = LoggerFactory.getLogger(LogInterceptor.class.getName());
static final Metadata.Key CUSTOM_HEADER_KEY =
Metadata.Key.of("custom_server_header_key", Metadata.ASCII_STRING_MARSHALLER);@Override
public ServerCall.Listener interceptCall(ServerCall call,
Metadata headers, ServerCallHandler next) {
logger.info("header received from client:" + headers);return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
responseHeaders.put(CUSTOM_HEADER_KEY, "customRespondValue");
super.sendHeaders(responseHeaders);
}
}, headers);
}
}
```
Annotate your server interface implementation(s) with ``@GrpcService`````java
@GrpcService(applyGlobalInterceptor = true)/*or you can use private interceptor or both*/
//@GrpcService(interceptors = ServerLogInterceptor.class)
public class HelloService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
```#### Grpc Client:
##### Enable Client
- Use annotation `@EnableGrpcClient` or `grpc.client.enable=true` in application.properties to Enable Grpc Server.
##### Property
- The Annotation `@GrpcClientChannel` default value is `defaultClient` if not present to inject gRPC channel.
the default host is `localhost` and default port is `9494`.
```properties
grpc.client.(gRPC server name).host=127.0.0.1
grpc.client.(gRPC server name).port=9494
# optional
grpc.client.(gRPC server name).plaintext=true
grpc.client.(gRPC server name).enable-keep-alive=false
grpc.client.(gRPC server name).keep-alive-without-calls=false
grpc.client.(gRPC server name).keep-alive-time=180
grpc.client.(gRPC server name).keep-alive-timeout=40
```
Then, you can inject channel:```java
public class GrpcClientService {@GrpcClientChannel("")
private Channel channel;public String sendMessage(String name) {
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
}
}
```### Sample
see the [sample module](https://github.com/Doublemine/grpc-springboot-starter/tree/master/sample/src/main/java/work/wanghao/kotlin/boot/grpc/sample)