From 2ee492fb7443aa37d0ee0e964d2259d796c5bd6b Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Tue, 20 Feb 2024 14:18:22 +0100 Subject: [PATCH] (gRPC) Bind gRPC services to an interface By default gRPC it magically decides on an interface. The change will explicitly tell it what to use. --- .../service/discovery/property/ServiceEndpoint.java | 4 ++++ .../main/java/nu/marginalia/service/server/Service.java | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/code/common/service-discovery/src/main/java/nu/marginalia/service/discovery/property/ServiceEndpoint.java b/code/common/service-discovery/src/main/java/nu/marginalia/service/discovery/property/ServiceEndpoint.java index 8102f52f..01caba90 100644 --- a/code/common/service-discovery/src/main/java/nu/marginalia/service/discovery/property/ServiceEndpoint.java +++ b/code/common/service-discovery/src/main/java/nu/marginalia/service/discovery/property/ServiceEndpoint.java @@ -3,6 +3,7 @@ package nu.marginalia.service.discovery.property; import lombok.SneakyThrows; +import java.net.InetSocketAddress; import java.net.URI; import java.net.URL; import java.util.UUID; @@ -12,6 +13,9 @@ public sealed interface ServiceEndpoint { int port(); URL toURL(String endpoint, String query); + default InetSocketAddress toInetSocketAddress() { + return new InetSocketAddress(host(), port()); + } static ServiceEndpoint forSchema(ApiSchema schema, String host, int port) { return switch (schema) { diff --git a/code/common/service/src/main/java/nu/marginalia/service/server/Service.java b/code/common/service/src/main/java/nu/marginalia/service/server/Service.java index 73cd5977..b9bb082b 100644 --- a/code/common/service/src/main/java/nu/marginalia/service/server/Service.java +++ b/code/common/service/src/main/java/nu/marginalia/service/server/Service.java @@ -1,7 +1,7 @@ package nu.marginalia.service.server; -import io.grpc.BindableService; -import io.grpc.ServerBuilder; +import io.grpc.*; +import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; import io.prometheus.client.Counter; import lombok.SneakyThrows; import nu.marginalia.mq.inbox.*; @@ -108,7 +108,8 @@ public class Service { config.externalAddress() ); - var grpcServerBuilder = ServerBuilder.forPort(grpcEndpoint.port()); + // Start the gRPC server + var grpcServerBuilder = NettyServerBuilder.forAddress(grpcEndpoint.toInetSocketAddress()); for (var grpcService : grpcServices) { grpcServerBuilder.addService(grpcService); }