Skip to content

Commit 1939bc7

Browse files
authored
Merge pull request #496 from tentativafc/master
Change metadata from gRPC.port to gRPC_port
2 parents 3d7c3ce + b101f46 commit 1939bc7

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

docs/en/client/configuration.md

Lines changed: 1 addition & 1 deletion
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): \

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` 上.

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);

grpc-common-spring-boot/src/main/java/net/devh/boot/grpc/common/util/GrpcUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class GrpcUtils {
2929
/**
3030
* The cloud discovery metadata key used to identify the grpc port.
3131
*/
32-
public static final String CLOUD_DISCOVERY_METADATA_PORT = "gRPC.port";
32+
public static final String CLOUD_DISCOVERY_METADATA_PORT = "gRPC_port";
3333

3434
/**
3535
* The constant for the grpc server port, -1 represents don't start an inter process server.

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/autoconfigure/GrpcMetadataConsulConfiguration.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
package net.devh.boot.grpc.server.autoconfigure;
1919

20-
import java.util.ArrayList;
21-
import java.util.List;
20+
import java.util.HashMap;
21+
import java.util.Map;
2222

2323
import javax.annotation.PostConstruct;
2424

@@ -52,11 +52,13 @@ public class GrpcMetadataConsulConfiguration {
5252
public void init() {
5353
if (consulRegistration != null) {
5454
final int port = grpcProperties.getPort();
55-
List<String> tags = consulRegistration.getService().getTags();
56-
tags = tags == null ? new ArrayList<>() : tags;
55+
Map<String, String> meta = consulRegistration.getService().getMeta();
56+
if (meta == null) {
57+
meta = new HashMap<>();
58+
}
5759
if (GrpcUtils.INTER_PROCESS_DISABLE != port) {
58-
tags.add(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT + "=" + port);
59-
consulRegistration.getService().setTags(tags);
60+
meta.put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT, Integer.toString(port));
61+
consulRegistration.getService().setMeta(meta);
6062
}
6163
}
6264
}

0 commit comments

Comments
 (0)