Skip to content

Commit 6323cf9

Browse files
committed
Simplify through use of grpc builder APIs
1 parent 4beb624 commit 6323cf9

File tree

9 files changed

+76
-195
lines changed

9 files changed

+76
-195
lines changed

samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.grpc.sample.proto.SimpleGrpc;
1616
import org.springframework.test.annotation.DirtiesContext;
1717

18+
import io.grpc.Channel;
1819
import io.grpc.Grpc;
1920
import io.grpc.InsecureChannelCredentials;
2021

@@ -45,11 +46,17 @@ void serverResponds() {
4546

4647
@TestConfiguration
4748
static class ExtraConfiguration {
49+
4850
@Bean
49-
SimpleGrpc.SimpleBlockingStub stub() {
50-
var channel = Grpc.newChannelBuilderForAddress("0.0.0.0", 9090, InsecureChannelCredentials.create())
51-
.build();
51+
SimpleGrpc.SimpleBlockingStub stub(Channel channel) {
5252
return SimpleGrpc.newBlockingStub(channel);
5353
}
54+
55+
@Bean(destroyMethod = "shutdown")
56+
Channel channel() {
57+
return Grpc.newChannelBuilderForAddress("0.0.0.0", 9090, InsecureChannelCredentials.create()).build();
58+
}
59+
5460
}
61+
5562
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024-2024 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+
package org.springframework.grpc.client;
17+
18+
import io.grpc.Channel;
19+
20+
public interface GrpcChannelFactory extends AutoCloseable {
21+
22+
Channel createChannel(String name);
23+
24+
}

spring-grpc-core/src/main/java/org/springframework/grpc/server/AbstractGrpcServerFactory.java renamed to spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 the original author or authors.
2+
* Copyright 2024-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* Partial copy from net.devh:grpc-spring-boot-starter.
1716
*/
1817
package org.springframework.grpc.server;
1918

@@ -32,7 +31,7 @@
3231
import io.grpc.ServerBuilder;
3332
import io.grpc.ServerServiceDefinition;
3433

35-
public abstract class AbstractGrpcServerFactory<T extends ServerBuilder<T>> implements GrpcServerFactory {
34+
public class DefaultGrpcServerFactory<T extends ServerBuilder<T>> implements GrpcServerFactory {
3635

3736
/** Logger available to subclasses. */
3837
protected final Log logger = LogFactory.getLog(getClass());
@@ -45,7 +44,7 @@ public abstract class AbstractGrpcServerFactory<T extends ServerBuilder<T>> impl
4544

4645
private final int port;
4746

48-
protected AbstractGrpcServerFactory(String address, int port, final List<GrpcServerConfigurer> serverConfigurers) {
47+
public DefaultGrpcServerFactory(String address, int port, final List<GrpcServerConfigurer> serverConfigurers) {
4948
this.serverConfigurers = requireNonNull(serverConfigurers, "serverConfigurers");
5049
this.address = address;
5150
this.port = port;
@@ -70,7 +69,11 @@ public Server createServer() {
7069
* Creates a new server builder.
7170
* @return The newly created server builder.
7271
*/
73-
protected abstract T newServerBuilder();
72+
@SuppressWarnings("unchecked")
73+
protected T newServerBuilder() {
74+
// TODO: Add support for address resolution
75+
return (T) ServerBuilder.forPort(getPort());
76+
}
7477

7578
/**
7679
* Configures the given server builder. This method can be overwritten to add features

spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java

-70
This file was deleted.

spring-grpc-core/src/main/java/org/springframework/grpc/util/GrpcUtils.java

-100
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024-2024 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+
package org.springframework.grpc.autoconfigure.client;
17+
18+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.grpc.server.GrpcServerFactory;
21+
22+
@Configuration(proxyBeanMethods = false)
23+
@ConditionalOnBean(GrpcServerFactory.class)
24+
public class GrpcClientAutoConfiguration {
25+
26+
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717

1818
import java.util.List;
1919

20-
import org.apache.commons.logging.Log;
21-
import org.apache.commons.logging.LogFactory;
2220
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
23-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2421
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2522
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2623
import org.springframework.context.annotation.Bean;
2724
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.grpc.server.DefaultGrpcServerFactory;
2826
import org.springframework.grpc.server.GrpcServerConfigurer;
2927
import org.springframework.grpc.server.GrpcServerFactory;
30-
import org.springframework.grpc.server.NettyGrpcServerFactory;
3128

3229
import io.grpc.ServerServiceDefinition;
3330

@@ -37,17 +34,12 @@
3734
@EnableConfigurationProperties(GrpcServerProperties.class)
3835
public class GrpcServerFactoryAutoConfiguration {
3936

40-
private static final Log logger = LogFactory.getLog(GrpcServerFactoryAutoConfiguration.class);
41-
4237
@ConditionalOnMissingBean(GrpcServerFactory.class)
43-
@ConditionalOnClass(name = { "io.netty.channel.Channel", "io.grpc.netty.NettyServerBuilder" })
4438
@Bean
45-
public NettyGrpcServerFactory nettyGrpcServerFactory(final GrpcServerProperties properties,
39+
public DefaultGrpcServerFactory<?> defaultGrpcServerFactory(final GrpcServerProperties properties,
4640
final GrpcServiceDiscoverer serviceDiscoverer, final List<GrpcServerConfigurer> serverConfigurers) {
47-
48-
logger.info("Detected grpc-netty: Creating NettyGrpcServerFactory");
49-
final NettyGrpcServerFactory factory = new NettyGrpcServerFactory(properties.getAddress(), properties.getPort(),
50-
serverConfigurers);
41+
final DefaultGrpcServerFactory<?> factory = new DefaultGrpcServerFactory<>(properties.getAddress(),
42+
properties.getPort(), serverConfigurers);
5143
for (final ServerServiceDefinition service : serviceDiscoverer.findGrpcServices()) {
5244
factory.addService(service);
5345
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerProperties.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020

2121
import org.springframework.boot.context.properties.ConfigurationProperties;
2222
import org.springframework.boot.convert.DurationUnit;
23-
import org.springframework.grpc.util.GrpcUtils;
2423

2524
@ConfigurationProperties(prefix = "spring.grpc.server")
2625
public class GrpcServerProperties {
2726

28-
private String address = GrpcUtils.ANY_IP_ADDRESS;
27+
private String address = "*";
2928

3029
private int port = 9090;
3130

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServiceAutoConfiguration.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GrpcServiceAutoConfiguration {
3737
@ConditionalOnMissingBean
3838
@Bean
3939
public GrpcServiceDiscoverer defaultGrpcServiceDiscoverer(ApplicationContext applicationContext) {
40-
return new AnnotationGrpcServiceDiscoverer(applicationContext);
40+
return new DefaultGrpcServiceDiscoverer(applicationContext);
4141
}
4242

4343
@ConditionalOnMissingBean
@@ -49,11 +49,11 @@ public GrpcServerLifecycle grpcServerLifecycle(final GrpcServerFactory factory,
4949

5050
}
5151

52-
class AnnotationGrpcServiceDiscoverer implements GrpcServiceDiscoverer {
52+
class DefaultGrpcServiceDiscoverer implements GrpcServiceDiscoverer {
5353

5454
private final ApplicationContext applicationContext;
5555

56-
public AnnotationGrpcServiceDiscoverer(ApplicationContext applicationContext) {
56+
public DefaultGrpcServiceDiscoverer(ApplicationContext applicationContext) {
5757
this.applicationContext = applicationContext;
5858
}
5959

0 commit comments

Comments
 (0)