Skip to content

Commit 31ea48c

Browse files
committed
Merge branch 'master' into optional-grpc-services
2 parents c89ca9a + 1939bc7 commit 31ea48c

File tree

12 files changed

+190
-23
lines changed

12 files changed

+190
-23
lines changed

docs/en/client/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ There are a number of supported schemes, that you can use to determine the targe
6565
one and also supports `SVC` lookups. See also [Kubernetes Setup](../kubernetes.md).
6666
- `discovery` (Prio 6): \
6767
(Optional) Uses spring-cloud's `DiscoveryClient` to lookup appropriate targets. The connections will be refreshed
68-
automatically during `HeartbeatEvent`s. Uses the `gRPC.port` metadata to determine the port, otherwise uses the
68+
automatically during `HeartbeatEvent`s. Uses the `gRPC_port` metadata to determine the port, otherwise uses the
6969
service port. \
7070
Example: `discovery:///service-name`
7171
- `self` (Prio 0): \
@@ -154,7 +154,7 @@ The following examples demonstrate how to use annotations to create a global cli
154154
public class ThirdPartyInterceptorConfig {}
155155

156156
@GrpcGlobalServerInterceptor
157-
ServerInterceptor logServerInterceptor() {
157+
LogGrpcInterceptor logServerInterceptor() {
158158
return new LogGrpcInterceptor();
159159
}
160160

docs/zh-CN/client/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ grpc.client.__name__.address=static://localhost:9090
4444

4545
- `static`(优先级 4): 一个简单的IP静态列表 (v4 和 v6), 可用于连接到服务端 (支持 `localhost`)。 例如:`static://192.168.1:8080,10.0.0.1:1337`
4646
- [`dns`](https://github.com/grpc/grpc-java/blob/master/core/src/main/java/io/grpc/internal/DnsNameResolver.java#L66)(优先级 5):解析并绑定到给定 DNS 名称的所有地址。 地址将被缓存,只有当现有连接被关闭 / 失败时才会刷新。 更多选项,例如 `SVC` 查找(对 kubernetes 有用),可以通过系统属性启用。 例如:`dns:///example.my.company`
47-
- `discovery` (优先级 6):(可选) 使用 Spring Cloud 的`DiscoveryClient` 去查找合适的目标。 在 `HeartbeatEvent` 的时候,连接将自动刷新。 使用 `gRPC.port` 元数据来确定端口,否则使用服务端口。 示例: `discovery:///service-name`
47+
- `discovery` (优先级 6):(可选) 使用 Spring Cloud 的`DiscoveryClient` 去查找合适的目标。 在 `HeartbeatEvent` 的时候,连接将自动刷新。 使用 `gRPC_port` 元数据来确定端口,否则使用服务端口。 示例: `discovery:///service-name`
4848
- `self`(优先级 0):如果您使用 `grpc-server-spring-boot-starter` 并且不想指定自己的地址 / 端口,则可以使用 self 关键词作为 scheme 或者 address 。 这对于需要使用随机服务器端口以避免冲突的测试特别有用。 例如:`self``self:self`
4949
- `in-process`:这是一个特殊的方案,将使用 `InProcessChannelFactory` 来替代原有正常的 ChannelFactory。 并使用它连接到 [`InProcessServer`](../server/configuration.md#enabling-the-inprocessserver)。 例如:`in-process:foobar`
5050
- *custom*: 您可以通过 Java 的 `ServiceLoader` 或从 Spring 的应用上下文中选择要自定义的 [`NameResolverProvider`](https://javadoc.io/page/io.grpc/grpc-all/latest/io/grpc/NameResolverProvider.html) ,并将其注册到 `NameResolverRegistry` 上.

examples/cloud-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GlobalClientInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import org.springframework.core.Ordered;
2222
import org.springframework.core.annotation.Order;
2323

24-
import io.grpc.ClientInterceptor;
2524
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2625

2726
@Order(Ordered.LOWEST_PRECEDENCE)
2827
@Configuration(proxyBeanMethods = false)
2928
public class GlobalClientInterceptorConfiguration {
3029

3130
@GrpcGlobalClientInterceptor
32-
ClientInterceptor logClientInterceptor() {
31+
LogGrpcInterceptor logClientInterceptor() {
3332
return new LogGrpcInterceptor();
3433
}
3534

examples/cloud-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/GlobalInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
import org.springframework.context.annotation.Configuration;
2121

22-
import io.grpc.ServerInterceptor;
2322
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
2423

2524
@Configuration(proxyBeanMethods = false)
2625
public class GlobalInterceptorConfiguration {
2726

2827
@GrpcGlobalServerInterceptor
29-
ServerInterceptor logServerInterceptor() {
28+
LogGrpcInterceptor logServerInterceptor() {
3029
return new LogGrpcInterceptor();
3130
}
3231

examples/local-grpc-client/src/main/java/net/devh/boot/grpc/examples/local/client/GlobalClientInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import org.springframework.core.Ordered;
2222
import org.springframework.core.annotation.Order;
2323

24-
import io.grpc.ClientInterceptor;
2524
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2625

2726
@Order(Ordered.LOWEST_PRECEDENCE)
2827
@Configuration(proxyBeanMethods = false)
2928
public class GlobalClientInterceptorConfiguration {
3029

3130
@GrpcGlobalClientInterceptor
32-
ClientInterceptor logClientInterceptor() {
31+
LogGrpcInterceptor logClientInterceptor() {
3332
return new LogGrpcInterceptor();
3433
}
3534

examples/local-grpc-server/src/main/java/net/devh/boot/grpc/examples/local/server/GlobalInterceptorConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
import org.springframework.context.annotation.Configuration;
2121

22-
import io.grpc.ServerInterceptor;
2322
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
2423

2524
@Configuration(proxyBeanMethods = false)
2625
public class GlobalInterceptorConfiguration {
2726

2827
@GrpcGlobalServerInterceptor
29-
ServerInterceptor logServerInterceptor() {
28+
LogGrpcInterceptor logServerInterceptor() {
3029
return new LogGrpcInterceptor();
3130
}
3231

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientHealthAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public HealthIndicator grpcChannelHealthIndicator(final GrpcChannelFactory facto
5555
final ImmutableMap<String, ConnectivityState> states = ImmutableMap.copyOf(factory.getConnectivityState());
5656
final Health.Builder health;
5757
if (states.containsValue(ConnectivityState.TRANSIENT_FAILURE)) {
58-
health = Health.down();
58+
health = Health.outOfService();
5959
} else {
6060
health = Health.up();
6161
}

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected List<ClientInterceptor> interceptorsFromAnnotation(final GrpcClient an
180180
final List<ClientInterceptor> list = Lists.newArrayList();
181181
for (final Class<? extends ClientInterceptor> interceptorClass : annotation.interceptors()) {
182182
final ClientInterceptor clientInterceptor;
183-
if (this.applicationContext.getBeanNamesForType(ClientInterceptor.class).length > 0) {
183+
if (this.applicationContext.getBeanNamesForType(interceptorClass).length > 0) {
184184
clientInterceptor = this.applicationContext.getBean(interceptorClass);
185185
} else {
186186
try {

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/nameresolver/DiscoveryClientNameResolver.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@
4444

4545
/**
4646
* The DiscoveryClientNameResolver resolves the service hosts and their associated gRPC port using the channel's name
47-
* and spring's cloud {@link DiscoveryClient}. The ports are extracted from the {@code gRPC.port} metadata.
47+
* and spring's cloud {@link DiscoveryClient}. The ports are extracted from the {@code gRPC_port} metadata.
4848
*
4949
* @author Michael ([email protected])
5050
* @author Daniel Theuke ([email protected])
5151
*/
5252
@Slf4j
5353
public class DiscoveryClientNameResolver extends NameResolver {
5454

55+
@Deprecated
56+
private static final String LEGACY_CLOUD_DISCOVERY_METADATA_PORT = "gRPC.port";
5557
private static final List<ServiceInstance> KEEP_PREVIOUS = null;
5658

5759
private final String name;
@@ -243,9 +245,16 @@ private int getGRPCPort(final ServiceInstance instance) {
243245
if (metadata == null) {
244246
return instance.getPort();
245247
}
246-
final String portString = metadata.get(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT);
248+
String portString = metadata.get(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT);
247249
if (portString == null) {
248-
return instance.getPort();
250+
portString = metadata.get(LEGACY_CLOUD_DISCOVERY_METADATA_PORT);
251+
if (portString == null) {
252+
return instance.getPort();
253+
} else {
254+
log.warn("Found legacy grpc port metadata '{}' for client '{}' use '{}' instead",
255+
LEGACY_CLOUD_DISCOVERY_METADATA_PORT, DiscoveryClientNameResolver.this.name,
256+
GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT);
257+
}
249258
}
250259
try {
251260
return Integer.parseInt(portString);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 net.devh.boot.grpc.client.inject;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22+
23+
import java.lang.annotation.Annotation;
24+
import java.util.List;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.test.annotation.DirtiesContext;
32+
33+
import io.grpc.CallOptions;
34+
import io.grpc.Channel;
35+
import io.grpc.ClientCall;
36+
import io.grpc.ClientInterceptor;
37+
import io.grpc.MethodDescriptor;
38+
import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration;
39+
40+
/**
41+
* Tests for {@link GrpcClientBeanPostProcessor}.
42+
*
43+
* @author Daniel Theuke ([email protected])
44+
*/
45+
@SpringBootTest(classes = GrpcClientBeanPostProcessorTest.TestConfig.class)
46+
@DirtiesContext
47+
@ImportAutoConfiguration(GrpcClientAutoConfiguration.class)
48+
class GrpcClientBeanPostProcessorTest {
49+
50+
@Autowired
51+
GrpcClientBeanPostProcessor postProcessor;
52+
53+
@Autowired
54+
Interceptor1 interceptor1;
55+
56+
@Test
57+
void testInterceptorsFromAnnotation1() {
58+
final List<ClientInterceptor> beans =
59+
assertDoesNotThrow(() -> this.postProcessor.interceptorsFromAnnotation(new GrpcClient() {
60+
61+
@Override
62+
public Class<? extends Annotation> annotationType() {
63+
return GrpcClient.class;
64+
}
65+
66+
@Override
67+
public String value() {
68+
return "test";
69+
}
70+
71+
@Override
72+
public boolean sortInterceptors() {
73+
return false;
74+
}
75+
76+
@Override
77+
@SuppressWarnings("unchecked")
78+
public Class<? extends ClientInterceptor>[] interceptors() {
79+
return new Class[] {Interceptor1.class};
80+
}
81+
82+
@Override
83+
public String[] interceptorNames() {
84+
return new String[0];
85+
}
86+
87+
}));
88+
89+
assertThat(beans).containsExactly(this.interceptor1);
90+
}
91+
92+
@Test
93+
void testInterceptorsFromAnnotation2() {
94+
final List<ClientInterceptor> beans =
95+
assertDoesNotThrow(() -> this.postProcessor.interceptorsFromAnnotation(new GrpcClient() {
96+
97+
@Override
98+
public Class<? extends Annotation> annotationType() {
99+
return GrpcClient.class;
100+
}
101+
102+
@Override
103+
public String value() {
104+
return "test";
105+
}
106+
107+
@Override
108+
public boolean sortInterceptors() {
109+
return false;
110+
}
111+
112+
@Override
113+
@SuppressWarnings("unchecked")
114+
public Class<? extends ClientInterceptor>[] interceptors() {
115+
return new Class[] {Interceptor2.class};
116+
}
117+
118+
@Override
119+
public String[] interceptorNames() {
120+
return new String[0];
121+
}
122+
123+
}));
124+
125+
assertThat(beans).hasSize(1).doesNotContain(this.interceptor1);
126+
}
127+
128+
static class TestConfig {
129+
130+
@Bean
131+
Interceptor1 interceptor1() {
132+
return new Interceptor1();
133+
}
134+
135+
136+
}
137+
138+
public static class Interceptor1 implements ClientInterceptor {
139+
140+
@Override
141+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
142+
final MethodDescriptor<ReqT, RespT> method,
143+
final CallOptions callOptions, final Channel next) {
144+
return next.newCall(method, callOptions);
145+
}
146+
147+
}
148+
149+
public static class Interceptor2 implements ClientInterceptor {
150+
151+
@Override
152+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
153+
final MethodDescriptor<ReqT, RespT> method,
154+
final CallOptions callOptions, final Channel next) {
155+
return next.newCall(method, callOptions);
156+
}
157+
158+
}
159+
160+
}

0 commit comments

Comments
 (0)