Skip to content

Commit 0a44b6b

Browse files
author
jvmlet
committed
interceptors support
1 parent 3cf7c16 commit 0a44b6b

File tree

12 files changed

+1686
-89
lines changed

12 files changed

+1686
-89
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
4+
import io.grpc.examples.CalculatorGrpc;
5+
import io.grpc.examples.CalculatorOuterClass;
6+
import org.lognet.springboot.grpc.GRpcService;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
import io.grpc.examples.GreeterGrpc;
11+
import io.grpc.examples.GreeterOuterClass;
12+
import io.grpc.stub.StreamObserver;
13+
14+
/**
15+
* Created by alexf on 28-Jan-16.
16+
*/
17+
18+
19+
@SpringBootApplication
20+
public class DemoApp {
21+
22+
@GRpcService(interceptors = { LogInterceptor.class })
23+
public static class GreeterService extends GreeterGrpc.GreeterImplBase {
24+
@Override
25+
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
26+
final GreeterOuterClass.HelloReply.Builder replyBuilder = GreeterOuterClass.HelloReply.newBuilder().setMessage("Hello " + request.getName());
27+
responseObserver.onNext(replyBuilder.build());
28+
responseObserver.onCompleted();
29+
}
30+
}
31+
32+
@GRpcService
33+
public static class CalculatorService extends CalculatorGrpc.CalculatorImplBase{
34+
@Override
35+
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
36+
CalculatorOuterClass.CalculatorResponse.Builder resultBuilder = CalculatorOuterClass.CalculatorResponse.newBuilder();
37+
switch (request.getOperation()){
38+
case ADD:
39+
resultBuilder.setResult(request.getNumber1()+request.getNumber2());
40+
break;
41+
case SUBTRACT:
42+
resultBuilder.setResult(request.getNumber1()-request.getNumber2());
43+
break;
44+
case MULTIPLY:
45+
resultBuilder.setResult(request.getNumber1()*request.getNumber2());
46+
break;
47+
case DIVIDE:
48+
resultBuilder.setResult(request.getNumber1()/request.getNumber2());
49+
break;
50+
case UNRECOGNIZED:
51+
break;
52+
}
53+
responseObserver.onNext(resultBuilder.build());
54+
responseObserver.onCompleted();
55+
56+
57+
}
58+
59+
60+
}
61+
62+
public static void main(String[] args) {
63+
SpringApplication.run(DemoApp.class,args);
64+
}
65+
66+
}

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/GreeterApp.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
import io.grpc.ServerCallHandler;
66
import io.grpc.ServerInterceptor;
77
import lombok.extern.slf4j.Slf4j;
8-
import org.lognet.springboot.grpc.GRpcInterceptor;
8+
import org.lognet.springboot.grpc.GRpcGlobalInterceptor;
9+
import org.springframework.stereotype.Component;
910

1011
/**
1112
* Created by jamessmith on 9/7/16.
1213
*/
1314
@Slf4j
14-
@GRpcInterceptor
15-
public class DemoInterceptor implements ServerInterceptor {
15+
@Component
16+
public class LogInterceptor implements ServerInterceptor {
1617

1718
@Override
1819
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
1920
ServerCallHandler<ReqT, RespT> next) {
20-
log.info("Demo interceptor invoked!");
21+
System.out.println(call.getMethodDescriptor().getFullMethodName());
22+
log.info(call.getMethodDescriptor().getFullMethodName());
2123
return next.startCall(call, headers);
2224
}
2325
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
syntax = "proto3";
2+
3+
option java_package = "io.grpc.examples";
4+
5+
6+
message CalculatorRequest {
7+
double number1 = 1;
8+
double number2 = 2;
9+
OperationType operation = 3;
10+
11+
enum OperationType {
12+
ADD = 0;
13+
SUBTRACT = 1;
14+
MULTIPLY = 2;
15+
DIVIDE = 3;
16+
}
17+
}
18+
19+
message CalculatorResponse {
20+
double result = 1;
21+
}
22+
23+
24+
service Calculator {
25+
rpc Calculate(CalculatorRequest) returns (CalculatorResponse) {}
26+
}
27+

grpc-spring-boot-starter-demo/src/main/proto/greeter.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ service Greeter {
1111

1212
}
1313

14+
1415
// The request message containing the user's name.
1516
message HelloRequest {
1617
string name = 1;
@@ -19,4 +20,6 @@ message HelloRequest {
1920
// The response message containing the greetings
2021
message HelloReply {
2122
string message = 1;
22-
}
23+
}
24+
25+
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package io.grpc.examples;
2+
3+
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
4+
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
5+
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
6+
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
7+
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
8+
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
9+
import static io.grpc.stub.ClientCalls.futureUnaryCall;
10+
import static io.grpc.MethodDescriptor.generateFullMethodName;
11+
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
12+
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
13+
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
14+
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;
15+
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
16+
import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
17+
18+
/**
19+
*/
20+
@javax.annotation.Generated(
21+
value = "by gRPC proto compiler (version 1.0.0)",
22+
comments = "Source: calculator.proto")
23+
public class CalculatorGrpc {
24+
25+
private CalculatorGrpc() {}
26+
27+
public static final String SERVICE_NAME = "Calculator";
28+
29+
// Static method descriptors that strictly reflect the proto.
30+
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
31+
public static final io.grpc.MethodDescriptor<io.grpc.examples.CalculatorOuterClass.CalculatorRequest,
32+
io.grpc.examples.CalculatorOuterClass.CalculatorResponse> METHOD_CALCULATE =
33+
io.grpc.MethodDescriptor.create(
34+
io.grpc.MethodDescriptor.MethodType.UNARY,
35+
generateFullMethodName(
36+
"Calculator", "Calculate"),
37+
io.grpc.protobuf.ProtoUtils.marshaller(io.grpc.examples.CalculatorOuterClass.CalculatorRequest.getDefaultInstance()),
38+
io.grpc.protobuf.ProtoUtils.marshaller(io.grpc.examples.CalculatorOuterClass.CalculatorResponse.getDefaultInstance()));
39+
40+
/**
41+
* Creates a new async stub that supports all call types for the service
42+
*/
43+
public static CalculatorStub newStub(io.grpc.Channel channel) {
44+
return new CalculatorStub(channel);
45+
}
46+
47+
/**
48+
* Creates a new blocking-style stub that supports unary and streaming output calls on the service
49+
*/
50+
public static CalculatorBlockingStub newBlockingStub(
51+
io.grpc.Channel channel) {
52+
return new CalculatorBlockingStub(channel);
53+
}
54+
55+
/**
56+
* Creates a new ListenableFuture-style stub that supports unary and streaming output calls on the service
57+
*/
58+
public static CalculatorFutureStub newFutureStub(
59+
io.grpc.Channel channel) {
60+
return new CalculatorFutureStub(channel);
61+
}
62+
63+
/**
64+
*/
65+
public static abstract class CalculatorImplBase implements io.grpc.BindableService {
66+
67+
/**
68+
*/
69+
public void calculate(io.grpc.examples.CalculatorOuterClass.CalculatorRequest request,
70+
io.grpc.stub.StreamObserver<io.grpc.examples.CalculatorOuterClass.CalculatorResponse> responseObserver) {
71+
asyncUnimplementedUnaryCall(METHOD_CALCULATE, responseObserver);
72+
}
73+
74+
@java.lang.Override public io.grpc.ServerServiceDefinition bindService() {
75+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
76+
.addMethod(
77+
METHOD_CALCULATE,
78+
asyncUnaryCall(
79+
new MethodHandlers<
80+
io.grpc.examples.CalculatorOuterClass.CalculatorRequest,
81+
io.grpc.examples.CalculatorOuterClass.CalculatorResponse>(
82+
this, METHODID_CALCULATE)))
83+
.build();
84+
}
85+
}
86+
87+
/**
88+
*/
89+
public static final class CalculatorStub extends io.grpc.stub.AbstractStub<CalculatorStub> {
90+
private CalculatorStub(io.grpc.Channel channel) {
91+
super(channel);
92+
}
93+
94+
private CalculatorStub(io.grpc.Channel channel,
95+
io.grpc.CallOptions callOptions) {
96+
super(channel, callOptions);
97+
}
98+
99+
@java.lang.Override
100+
protected CalculatorStub build(io.grpc.Channel channel,
101+
io.grpc.CallOptions callOptions) {
102+
return new CalculatorStub(channel, callOptions);
103+
}
104+
105+
/**
106+
*/
107+
public void calculate(io.grpc.examples.CalculatorOuterClass.CalculatorRequest request,
108+
io.grpc.stub.StreamObserver<io.grpc.examples.CalculatorOuterClass.CalculatorResponse> responseObserver) {
109+
asyncUnaryCall(
110+
getChannel().newCall(METHOD_CALCULATE, getCallOptions()), request, responseObserver);
111+
}
112+
}
113+
114+
/**
115+
*/
116+
public static final class CalculatorBlockingStub extends io.grpc.stub.AbstractStub<CalculatorBlockingStub> {
117+
private CalculatorBlockingStub(io.grpc.Channel channel) {
118+
super(channel);
119+
}
120+
121+
private CalculatorBlockingStub(io.grpc.Channel channel,
122+
io.grpc.CallOptions callOptions) {
123+
super(channel, callOptions);
124+
}
125+
126+
@java.lang.Override
127+
protected CalculatorBlockingStub build(io.grpc.Channel channel,
128+
io.grpc.CallOptions callOptions) {
129+
return new CalculatorBlockingStub(channel, callOptions);
130+
}
131+
132+
/**
133+
*/
134+
public io.grpc.examples.CalculatorOuterClass.CalculatorResponse calculate(io.grpc.examples.CalculatorOuterClass.CalculatorRequest request) {
135+
return blockingUnaryCall(
136+
getChannel(), METHOD_CALCULATE, getCallOptions(), request);
137+
}
138+
}
139+
140+
/**
141+
*/
142+
public static final class CalculatorFutureStub extends io.grpc.stub.AbstractStub<CalculatorFutureStub> {
143+
private CalculatorFutureStub(io.grpc.Channel channel) {
144+
super(channel);
145+
}
146+
147+
private CalculatorFutureStub(io.grpc.Channel channel,
148+
io.grpc.CallOptions callOptions) {
149+
super(channel, callOptions);
150+
}
151+
152+
@java.lang.Override
153+
protected CalculatorFutureStub build(io.grpc.Channel channel,
154+
io.grpc.CallOptions callOptions) {
155+
return new CalculatorFutureStub(channel, callOptions);
156+
}
157+
158+
/**
159+
*/
160+
public com.google.common.util.concurrent.ListenableFuture<io.grpc.examples.CalculatorOuterClass.CalculatorResponse> calculate(
161+
io.grpc.examples.CalculatorOuterClass.CalculatorRequest request) {
162+
return futureUnaryCall(
163+
getChannel().newCall(METHOD_CALCULATE, getCallOptions()), request);
164+
}
165+
}
166+
167+
private static final int METHODID_CALCULATE = 0;
168+
169+
private static class MethodHandlers<Req, Resp> implements
170+
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
171+
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
172+
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
173+
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
174+
private final CalculatorImplBase serviceImpl;
175+
private final int methodId;
176+
177+
public MethodHandlers(CalculatorImplBase serviceImpl, int methodId) {
178+
this.serviceImpl = serviceImpl;
179+
this.methodId = methodId;
180+
}
181+
182+
@java.lang.Override
183+
@java.lang.SuppressWarnings("unchecked")
184+
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
185+
switch (methodId) {
186+
case METHODID_CALCULATE:
187+
serviceImpl.calculate((io.grpc.examples.CalculatorOuterClass.CalculatorRequest) request,
188+
(io.grpc.stub.StreamObserver<io.grpc.examples.CalculatorOuterClass.CalculatorResponse>) responseObserver);
189+
break;
190+
default:
191+
throw new AssertionError();
192+
}
193+
}
194+
195+
@java.lang.Override
196+
@java.lang.SuppressWarnings("unchecked")
197+
public io.grpc.stub.StreamObserver<Req> invoke(
198+
io.grpc.stub.StreamObserver<Resp> responseObserver) {
199+
switch (methodId) {
200+
default:
201+
throw new AssertionError();
202+
}
203+
}
204+
}
205+
206+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
207+
return new io.grpc.ServiceDescriptor(SERVICE_NAME,
208+
METHOD_CALCULATE);
209+
}
210+
211+
}

0 commit comments

Comments
 (0)