Skip to content

Commit ef9c246

Browse files
committed
GH-1245 Add initial proxy support
This feature woudl allow request to be proxied to additional targets such as grpc, rsocket etc. Resolves #1245
1 parent 1234a94 commit ef9c246

File tree

5 files changed

+69
-31
lines changed

5 files changed

+69
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2021-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.grpc;
18+
19+
import java.util.function.Function;
20+
21+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
22+
import org.springframework.cloud.function.context.FunctionProperties;
23+
import org.springframework.cloud.function.context.MessageRoutingCallback;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.messaging.Message;
27+
28+
/**
29+
*
30+
* @author Oleg Zhurakousky
31+
* @since 3.2
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
@EnableConfigurationProperties(FunctionGrpcProperties.class)
35+
public class GrpcFunctionAutoConfiguration {
36+
37+
public static String GRPC_INVOKER_FUNCTION = "grpcInvokerFunction";
38+
39+
public static String GRPC = "grpc";
40+
41+
@Bean
42+
public Function<Message<byte[]>, Message<?>> grpcInvokerFunction() {
43+
return input -> {
44+
return GrpcUtils.requestReply(input);
45+
};
46+
}
47+
48+
@Bean
49+
public MessageRoutingCallback routingCallback() {
50+
return new MessageRoutingCallback() {
51+
public String routingResult(Message<?> message) {
52+
if (message.getHeaders().containsKey(FunctionProperties.PROXY)
53+
&& message.getHeaders().get(FunctionProperties.PROXY).equals(GRPC)) {
54+
return GRPC_INVOKER_FUNCTION;
55+
}
56+
return null;
57+
}
58+
};
59+
}
60+
}

spring-cloud-function-adapters/spring-cloud-function-grpc/src/main/java/org/springframework/cloud/function/grpc/GrpcServerMessageHandler.java

+1-30
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,11 @@
3232

3333
package org.springframework.cloud.function.grpc;
3434

35-
import java.util.Map;
36-
import java.util.concurrent.ExecutorService;
37-
import java.util.concurrent.Executors;
38-
import java.util.concurrent.LinkedBlockingQueue;
39-
import java.util.concurrent.TimeUnit;
40-
import java.util.concurrent.atomic.AtomicBoolean;
41-
//
42-
import io.grpc.Status;
43-
import io.grpc.stub.ServerCallStreamObserver;
44-
import io.grpc.stub.StreamObserver;
45-
import org.apache.commons.logging.Log;
46-
import org.apache.commons.logging.LogFactory;
47-
import org.reactivestreams.Publisher;
48-
import reactor.core.publisher.Flux;
49-
import reactor.core.publisher.Sinks;
50-
import reactor.core.publisher.Sinks.Many;
51-
//
52-
import org.springframework.cloud.function.context.FunctionCatalog;
53-
import org.springframework.cloud.function.context.FunctionProperties;
54-
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
5535
import org.springframework.cloud.function.grpc.MessagingServiceGrpc.MessagingServiceImplBase;
56-
import org.springframework.context.SmartLifecycle;
57-
import org.springframework.messaging.Message;
58-
import org.springframework.util.Assert;
59-
import org.springframework.util.CollectionUtils;
6036

6137
import com.google.protobuf.GeneratedMessageV3;
62-
//
63-
//import com.google.protobuf.GeneratedMessage;
6438

39+
import io.grpc.stub.StreamObserver;
6540

6641
/**
6742
*
@@ -72,12 +47,8 @@
7247
@SuppressWarnings("rawtypes")
7348
public class GrpcServerMessageHandler extends MessagingServiceImplBase {
7449

75-
private Log logger = LogFactory.getLog(GrpcServerMessageHandler.class);
76-
7750
private final MessageHandlingHelper helper;
7851

79-
private boolean running;
80-
8152
public GrpcServerMessageHandler(MessageHandlingHelper<GeneratedMessageV3> helper) {
8253
this.helper = helper;
8354
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.springframework.cloud.function.grpc.GrpcAutoConfiguration
2+
org.springframework.cloud.function.grpc.GrpcFunctionAutoConfiguration

spring-cloud-function-adapters/spring-cloud-function-grpc/src/test/java/org/springframework/cloud/function/grpc/GrpcInteractionTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* @author Oleg Zhurakousky
5151
* @author Chris Bono
5252
*/
53-
@Disabled
53+
//@Disabled
5454
public class GrpcInteractionTests {
5555

5656
@BeforeEach
@@ -211,6 +211,7 @@ public void testBidirectionalStreamWithReactiveFunction() throws Exception {
211211
}
212212

213213
@Test
214+
@Disabled
214215
public void testClientStreaming() throws Exception {
215216
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
216217
SampleConfiguration.class).web(WebApplicationType.NONE).run(

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/FunctionProperties.java

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public class FunctionProperties implements EnvironmentAware, ApplicationContextA
6060
*/
6161
public final static String FUNCTION_DEFINITION = PREFIX + ".definition";
6262

63+
/**
64+
* Key for the proxy name.
65+
*/
66+
public final static String PROXY = "proxy";
67+
6368
/**
6469
* Definition of the function to be used. This could be function name (e.g., 'myFunction')
6570
* or function composition definition (e.g., 'myFunction|yourFunction')

0 commit comments

Comments
 (0)