Skip to content

Commit 2d0be5f

Browse files
committed
implement grpc request scope
1 parent c368a0b commit 2d0be5f

File tree

9 files changed

+1339
-1
lines changed

9 files changed

+1339
-1
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import io.grpc.examples.SecuredCalculatorGrpc;
66
import io.grpc.stub.StreamObserver;
77
import org.lognet.springboot.grpc.GRpcService;
8+
import org.lognet.springboot.grpc.autoconfigure.scope.GRpcRequestScope;
9+
import org.springframework.context.annotation.Bean;
810
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.Scope;
12+
import org.springframework.context.annotation.ScopedProxyMode;
913
import org.springframework.security.access.annotation.Secured;
1014

1115
@Configuration
@@ -58,4 +62,10 @@ public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObse
5862

5963

6064
}
65+
66+
@Bean
67+
@Scope(scopeName = GRpcRequestScope.GRPC_REQUEST_SCOPE_NAME, proxyMode = ScopedProxyMode.TARGET_CLASS)
68+
public RandomUUID testRequestScopeBean() {
69+
return new RandomUUID();
70+
}
6171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
import java.util.UUID;
4+
5+
public class RandomUUID {
6+
private final String id = UUID.randomUUID().toString();
7+
8+
public String getId() {
9+
return this.id;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2016-2021 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.lognet.springboot.grpc.demo;
19+
20+
import io.grpc.examples.RequestScopedGrpc;
21+
import io.grpc.examples.RequestScopedOuterClass;
22+
import io.grpc.stub.StreamObserver;
23+
import org.lognet.springboot.grpc.GRpcService;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
26+
import java.util.UUID;
27+
28+
@GRpcService
29+
public class RequestScopedService extends RequestScopedGrpc.RequestScopedImplBase {
30+
@Autowired
31+
private RandomUUID testRequestScopeBean;
32+
33+
@Override
34+
public StreamObserver<RequestScopedOuterClass.RequestScopedMessage> requestScoped(StreamObserver<RequestScopedOuterClass.RequestScopedMessage> observer) {
35+
return new StreamObserver<RequestScopedOuterClass.RequestScopedMessage>() {
36+
@Override
37+
public void onNext(RequestScopedOuterClass.RequestScopedMessage value) {
38+
observer.onNext(value.toBuilder().setStr(RequestScopedService.this.testRequestScopeBean.getId()).build());
39+
}
40+
41+
@Override
42+
public void onError(Throwable t) {
43+
observer.onError(t);
44+
}
45+
46+
@Override
47+
public void onCompleted() {
48+
observer.onCompleted();
49+
}
50+
};
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
3+
option java_package = "io.grpc.examples";
4+
5+
service RequestScoped {
6+
rpc RequestScoped(stream RequestScopedMessage) returns (stream RequestScopedMessage) {}
7+
}
8+
9+
message RequestScopedMessage {
10+
string str = 1;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package io.grpc.examples;
2+
3+
import static io.grpc.MethodDescriptor.generateFullMethodName;
4+
5+
/**
6+
*/
7+
@javax.annotation.Generated(
8+
value = "by gRPC proto compiler (version 1.36.0)",
9+
comments = "Source: request_scoped.proto")
10+
public final class RequestScopedGrpc {
11+
12+
private RequestScopedGrpc() {}
13+
14+
public static final String SERVICE_NAME = "RequestScoped";
15+
16+
// Static method descriptors that strictly reflect the proto.
17+
private static volatile io.grpc.MethodDescriptor<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage,
18+
io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> getRequestScopedMethod;
19+
20+
@io.grpc.stub.annotations.RpcMethod(
21+
fullMethodName = SERVICE_NAME + '/' + "RequestScoped",
22+
requestType = io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage.class,
23+
responseType = io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage.class,
24+
methodType = io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
25+
public static io.grpc.MethodDescriptor<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage,
26+
io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> getRequestScopedMethod() {
27+
io.grpc.MethodDescriptor<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage, io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> getRequestScopedMethod;
28+
if ((getRequestScopedMethod = RequestScopedGrpc.getRequestScopedMethod) == null) {
29+
synchronized (RequestScopedGrpc.class) {
30+
if ((getRequestScopedMethod = RequestScopedGrpc.getRequestScopedMethod) == null) {
31+
RequestScopedGrpc.getRequestScopedMethod = getRequestScopedMethod =
32+
io.grpc.MethodDescriptor.<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage, io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage>newBuilder()
33+
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
34+
.setFullMethodName(generateFullMethodName(SERVICE_NAME, "RequestScoped"))
35+
.setSampledToLocalTracing(true)
36+
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
37+
io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage.getDefaultInstance()))
38+
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
39+
io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage.getDefaultInstance()))
40+
.setSchemaDescriptor(new RequestScopedMethodDescriptorSupplier("RequestScoped"))
41+
.build();
42+
}
43+
}
44+
}
45+
return getRequestScopedMethod;
46+
}
47+
48+
/**
49+
* Creates a new async stub that supports all call types for the service
50+
*/
51+
public static RequestScopedStub newStub(io.grpc.Channel channel) {
52+
io.grpc.stub.AbstractStub.StubFactory<RequestScopedStub> factory =
53+
new io.grpc.stub.AbstractStub.StubFactory<RequestScopedStub>() {
54+
@java.lang.Override
55+
public RequestScopedStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
56+
return new RequestScopedStub(channel, callOptions);
57+
}
58+
};
59+
return RequestScopedStub.newStub(factory, channel);
60+
}
61+
62+
/**
63+
* Creates a new blocking-style stub that supports unary and streaming output calls on the service
64+
*/
65+
public static RequestScopedBlockingStub newBlockingStub(
66+
io.grpc.Channel channel) {
67+
io.grpc.stub.AbstractStub.StubFactory<RequestScopedBlockingStub> factory =
68+
new io.grpc.stub.AbstractStub.StubFactory<RequestScopedBlockingStub>() {
69+
@java.lang.Override
70+
public RequestScopedBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
71+
return new RequestScopedBlockingStub(channel, callOptions);
72+
}
73+
};
74+
return RequestScopedBlockingStub.newStub(factory, channel);
75+
}
76+
77+
/**
78+
* Creates a new ListenableFuture-style stub that supports unary calls on the service
79+
*/
80+
public static RequestScopedFutureStub newFutureStub(
81+
io.grpc.Channel channel) {
82+
io.grpc.stub.AbstractStub.StubFactory<RequestScopedFutureStub> factory =
83+
new io.grpc.stub.AbstractStub.StubFactory<RequestScopedFutureStub>() {
84+
@java.lang.Override
85+
public RequestScopedFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
86+
return new RequestScopedFutureStub(channel, callOptions);
87+
}
88+
};
89+
return RequestScopedFutureStub.newStub(factory, channel);
90+
}
91+
92+
/**
93+
*/
94+
public static abstract class RequestScopedImplBase implements io.grpc.BindableService {
95+
96+
/**
97+
*/
98+
public io.grpc.stub.StreamObserver<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> requestScoped(
99+
io.grpc.stub.StreamObserver<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> responseObserver) {
100+
return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getRequestScopedMethod(), responseObserver);
101+
}
102+
103+
@java.lang.Override public final io.grpc.ServerServiceDefinition bindService() {
104+
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
105+
.addMethod(
106+
getRequestScopedMethod(),
107+
io.grpc.stub.ServerCalls.asyncBidiStreamingCall(
108+
new MethodHandlers<
109+
io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage,
110+
io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage>(
111+
this, METHODID_REQUEST_SCOPED)))
112+
.build();
113+
}
114+
}
115+
116+
/**
117+
*/
118+
public static final class RequestScopedStub extends io.grpc.stub.AbstractAsyncStub<RequestScopedStub> {
119+
private RequestScopedStub(
120+
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
121+
super(channel, callOptions);
122+
}
123+
124+
@java.lang.Override
125+
protected RequestScopedStub build(
126+
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
127+
return new RequestScopedStub(channel, callOptions);
128+
}
129+
130+
/**
131+
*/
132+
public io.grpc.stub.StreamObserver<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> requestScoped(
133+
io.grpc.stub.StreamObserver<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage> responseObserver) {
134+
return io.grpc.stub.ClientCalls.asyncBidiStreamingCall(
135+
getChannel().newCall(getRequestScopedMethod(), getCallOptions()), responseObserver);
136+
}
137+
}
138+
139+
/**
140+
*/
141+
public static final class RequestScopedBlockingStub extends io.grpc.stub.AbstractBlockingStub<RequestScopedBlockingStub> {
142+
private RequestScopedBlockingStub(
143+
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
144+
super(channel, callOptions);
145+
}
146+
147+
@java.lang.Override
148+
protected RequestScopedBlockingStub build(
149+
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
150+
return new RequestScopedBlockingStub(channel, callOptions);
151+
}
152+
}
153+
154+
/**
155+
*/
156+
public static final class RequestScopedFutureStub extends io.grpc.stub.AbstractFutureStub<RequestScopedFutureStub> {
157+
private RequestScopedFutureStub(
158+
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
159+
super(channel, callOptions);
160+
}
161+
162+
@java.lang.Override
163+
protected RequestScopedFutureStub build(
164+
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
165+
return new RequestScopedFutureStub(channel, callOptions);
166+
}
167+
}
168+
169+
private static final int METHODID_REQUEST_SCOPED = 0;
170+
171+
private static final class MethodHandlers<Req, Resp> implements
172+
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
173+
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
174+
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
175+
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
176+
private final RequestScopedImplBase serviceImpl;
177+
private final int methodId;
178+
179+
MethodHandlers(RequestScopedImplBase serviceImpl, int methodId) {
180+
this.serviceImpl = serviceImpl;
181+
this.methodId = methodId;
182+
}
183+
184+
@java.lang.Override
185+
@java.lang.SuppressWarnings("unchecked")
186+
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
187+
switch (methodId) {
188+
default:
189+
throw new AssertionError();
190+
}
191+
}
192+
193+
@java.lang.Override
194+
@java.lang.SuppressWarnings("unchecked")
195+
public io.grpc.stub.StreamObserver<Req> invoke(
196+
io.grpc.stub.StreamObserver<Resp> responseObserver) {
197+
switch (methodId) {
198+
case METHODID_REQUEST_SCOPED:
199+
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.requestScoped(
200+
(io.grpc.stub.StreamObserver<io.grpc.examples.RequestScopedOuterClass.RequestScopedMessage>) responseObserver);
201+
default:
202+
throw new AssertionError();
203+
}
204+
}
205+
}
206+
207+
private static abstract class RequestScopedBaseDescriptorSupplier
208+
implements io.grpc.protobuf.ProtoFileDescriptorSupplier, io.grpc.protobuf.ProtoServiceDescriptorSupplier {
209+
RequestScopedBaseDescriptorSupplier() {}
210+
211+
@java.lang.Override
212+
public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() {
213+
return io.grpc.examples.RequestScopedOuterClass.getDescriptor();
214+
}
215+
216+
@java.lang.Override
217+
public com.google.protobuf.Descriptors.ServiceDescriptor getServiceDescriptor() {
218+
return getFileDescriptor().findServiceByName("RequestScoped");
219+
}
220+
}
221+
222+
private static final class RequestScopedFileDescriptorSupplier
223+
extends RequestScopedBaseDescriptorSupplier {
224+
RequestScopedFileDescriptorSupplier() {}
225+
}
226+
227+
private static final class RequestScopedMethodDescriptorSupplier
228+
extends RequestScopedBaseDescriptorSupplier
229+
implements io.grpc.protobuf.ProtoMethodDescriptorSupplier {
230+
private final String methodName;
231+
232+
RequestScopedMethodDescriptorSupplier(String methodName) {
233+
this.methodName = methodName;
234+
}
235+
236+
@java.lang.Override
237+
public com.google.protobuf.Descriptors.MethodDescriptor getMethodDescriptor() {
238+
return getServiceDescriptor().findMethodByName(methodName);
239+
}
240+
}
241+
242+
private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
243+
244+
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
245+
io.grpc.ServiceDescriptor result = serviceDescriptor;
246+
if (result == null) {
247+
synchronized (RequestScopedGrpc.class) {
248+
result = serviceDescriptor;
249+
if (result == null) {
250+
serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME)
251+
.setSchemaDescriptor(new RequestScopedFileDescriptorSupplier())
252+
.addMethod(getRequestScopedMethod())
253+
.build();
254+
}
255+
}
256+
}
257+
return result;
258+
}
259+
}

0 commit comments

Comments
 (0)