简体   繁体   English

使用转发代理从客户端到服务器的 GRPC

[英]GRPC from client to server with forward proxy

Using grpc from either nodejs or java, what are the properties or configuration necessary to get a grpc client to connect to a server through a proxy?使用来自 nodejs 或 java 的 grpc,让 grpc 客户端通过代理连接到服务器所需的属性或配置是什么?

I have been unable to find either an example or a document explaining the settings.我一直无法找到解释设置的示例或文档。 Do I need to do something in the code itself?我需要在代码本身中做些什么吗?

I am behind a proxy and I am not sure if the issue is that my settings are incorrect or that my proxy does not support grpc.我在代理后面,我不确定问题是我的设置不正确还是我的代理不支持 grpc。 It supports http/2 as a protocol upgrade.它支持 http/2 作为协议升级。

My proxy settings in java are:我在 Java 中的代理设置是:

-Dhttp.proxyHost=xxx.xxx.xxx
-Dhttp.proxyPort=8888 
-Dhttp.nonProxyHosts="*.nowhere.nothing"
-Dhttps.proxyHost=xxx.xxx.com
-Dhttps.proxyPort=8888
-Dhttps.nonProxyHosts="*.nowhere.nothing"
-Dsocks.proxyHost=xxx.xxx.xxx
-Dsocks.proxyPort=8888
-Dsocks.nonProxyHosts="*.nowhere.nothing"

Since grpc-java 1.0.3 you can specify the environment variable GRPC_PROXY_EXP with a value in the form host:port .从 grpc-java 1.0.3 开始,您可以使用host:port形式的值指定环境变量GRPC_PROXY_EXP The "EXP" means experimental, as it will be removed after grpc-java observes the normal Java settings (like https.proxyHost ). “EXP”表示实验性的,因为它会在 grpc-java 观察到正常的 Java 设置(如https.proxyHosthttps.proxyHost

If you prefer to not use the global https.proxyHost , https.proxyPort properties, you could use the StubSettings of your client to specify a ChannelConfigurator .如果您不想使用全局https.proxyHosthttps.proxyPort属性,则可以使用客户端的StubSettings来指定ChannelConfigurator It might then look like this:它可能看起来像这样:

InetSocketAddress proxyAddress = new InetSocketAddress("my.proxy.local", 8080);
InstantiatingGrpcChannelProvider transportProvider = SessionsStubSettings.defaultGrpcTransportProviderBuilder()
    .setChannelConfigurator(new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
        @Override
        public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
            return input.proxyDetector(new ProxyDetector() {
                @Override
                public ProxiedSocketAddress proxyFor(SocketAddress targetServerAddress) throws IOException {
                    if (!(targetServerAddress instanceof InetSocketAddress) || targetServerAddress == null) {
                        return null;
                    } 
                    return HttpConnectProxiedSocketAddress.newBuilder()
                        .setTargetAddress((InetSocketAddress) targetServerAddress)
                        .setProxyAddress(proxyAddress)
                        .build();
                    }
                });
            }
    })
    .build();

and then you could use the stubSettings bellow to create your GRPC client:然后你可以使用下面的stubSettings来创建你的 GRPC 客户端:


stubSettings = XYZStubSettings.newBuilder().setTransportChannelProvider(transportProvider);

In later releases (I think since 1.8.0+) you need:在以后的版本中(我认为从 1.8.0+ 开始)你需要:

System.setProperty("http.proxyHost", "http-ip-address-hostname");
System.setProperty("http.proxyPort", "http-port-value");    
System.setProperty("https.proxyHost", "https-ip-address-hostname");
System.setProperty("https.proxyPort", "https-port-value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM