本文整理汇总了Java中io.grpc.netty.NettyChannelBuilder类的典型用法代码示例。如果您正苦于以下问题:Java NettyChannelBuilder类的具体用法?Java NettyChannelBuilder怎么用?Java NettyChannelBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NettyChannelBuilder类属于io.grpc.netty包,在下文中一共展示了NettyChannelBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connectPlugin
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
public void connectPlugin(String host, int port) {
ManagedChannel channel = NettyChannelBuilder.forAddress(host, port)
.negotiationType(NegotiationType.PLAINTEXT) // TODO: gRPC encryption
.keepAliveTime(1, TimeUnit.MINUTES)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.directExecutor()
.channelType(EpollSocketChannel.class)
.eventLoopGroup(new EpollEventLoopGroup())
.build();
PluginManagerGrpc.PluginManagerBlockingStub blocking = PluginManagerGrpc.newBlockingStub(channel);
PluginManagerGrpc.PluginManagerStub async = PluginManagerGrpc.newStub(channel);
ServiceConnection connection = ServiceConnection.builder()
.channel(channel)
.blockingStub(blocking)
.asyncStub(async)
.build();
this.pluginConnections.put(PLUGIN_MANAGER, connection);
}
示例2: startServer
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
@BeforeClass
public static void startServer() throws IOException {
AfricasTalking.initialize(Fixtures.USERNAME, Fixtures.API_KEY);
server = new Server(new Authenticator() {
@Override
public boolean authenticate(String client) {
return client.compareToIgnoreCase(TEST_CLIENT_ID) == 0;
}
});
server.addSipCredentials("test", "secret", "sip://at.dev");
server.start(certFile, privateKeyFile, TEST_PORT);
ManagedChannel ch = NettyChannelBuilder.forAddress("localhost", TEST_PORT)
.sslContext(GrpcSslContexts.forClient().trustManager(certFile).build())
.build();
client = SdkServerServiceGrpc.newBlockingStub(ch)
.withCallCredentials(new CallCredentials(){
@Override
public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, Executor appExecutor,
final MetadataApplier applier) {
appExecutor.execute(new Runnable(){
@Override
public void run() {
try {
Metadata headers = new Metadata();
Metadata.Key<String> clientIdKey = Metadata.Key.of("X-Client-Id", Metadata.ASCII_STRING_MARSHALLER);
headers.put(clientIdKey, TEST_CLIENT_ID);
applier.apply(headers);
} catch(Throwable ex) {
applier.fail(Status.UNAUTHENTICATED.withCause(ex));
}
}
});
}
});
}
示例3: getGenomicsManagedChannel
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
private static ManagedChannel getGenomicsManagedChannel(List<ClientInterceptor> interceptors)
throws SSLException {
// Java 8's implementation of GCM ciphers is extremely slow. Therefore we disable
// them here.
List<String> defaultCiphers = GrpcSslContexts.forClient().ciphers(null).build().cipherSuites();
List<String> performantCiphers = new ArrayList<>();
for (String cipher : defaultCiphers) {
if (!cipher.contains("GCM")) {
performantCiphers.add(cipher);
}
}
return NettyChannelBuilder.forAddress(GENOMICS_ENDPOINT, 443)
.negotiationType(NegotiationType.TLS)
.sslContext(GrpcSslContexts.forClient().ciphers(performantCiphers).build())
.intercept(interceptors)
.build();
}
示例4: build
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
public GRPCAgentClient build() {
ManagedChannel managedChannel = channel;
if (managedChannel == null) {
managedChannel = NettyChannelBuilder.forAddress(host, port)
.keepAliveTime(keepAliveTimeMS, TimeUnit.MILLISECONDS)
.keepAliveTimeout(keepAliveTimeoutMS, TimeUnit.MILLISECONDS)
.keepAliveWithoutCalls(keepAliveWithoutCalls)
.negotiationType(negotiationType)
.build();
}
SpanAgentStub stub = SpanAgentGrpc.newStub(managedChannel);
return new GRPCAgentClient(format, managedChannel, stub, observer, shutdownTimeoutMS);
}
示例5: before
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
/**
* Before the test has started, create the server and channel.
*/
@Override
protected void before() throws Throwable {
serviceRegistry = new MutableHandlerRegistry();
NettyServerBuilder serverBuilder = NettyServerBuilder
.forPort(0)
.fallbackHandlerRegistry(serviceRegistry);
if (useDirectExecutor) {
serverBuilder.directExecutor();
}
configureServerBuilder.accept(serverBuilder);
server = serverBuilder.build().start();
port = server.getPort();
NettyChannelBuilder channelBuilder = NettyChannelBuilder.forAddress("localhost", port).usePlaintext(true);
configureChannelBuilder.accept(channelBuilder);
channel = channelBuilder.build();
}
示例6: defaultChannelBuilder
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
private ManagedChannelBuilder<?> defaultChannelBuilder() {
NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget("etcd");
if (builder.sslContext() != null) {
channelBuilder.sslContext(builder.sslContext());
} else {
channelBuilder.usePlaintext(true);
}
channelBuilder.nameResolverFactory(
forEndpoints(
Optional.ofNullable(builder.authority()).orElse("etcd"),
builder.endpoints(),
Optional.ofNullable(builder.uriResolverLoader())
.orElseGet(URIResolverLoader::defaultLoader)
)
);
if (builder.loadBalancerFactory() != null) {
channelBuilder.loadBalancerFactory(builder.loadBalancerFactory());
}
channelBuilder.intercept(new AuthTokenInterceptor());
return channelBuilder;
}
示例7: RemoteDataSet
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
public RemoteDataSet(final HostAndPort serverEndpoint, final int remoteHandle) {
this.serverEndpoint = serverEndpoint;
this.remoteHandle = remoteHandle;
final ExecutorService executorService =
ExecutorUtils.newNamedThreadPool("remote-data-set:" + serverEndpoint, 5);
// Using PollSelectorProvider() to avoid Epoll CPU utilization problems.
// See: https://github.com/netty/netty/issues/327
final EventLoopGroup workerElg = new NioEventLoopGroup(1,
ExecutorUtils.newFastLocalThreadFactory("worker"), new PollSelectorProvider());
this.stub = HillviewServerGrpc.newStub(NettyChannelBuilder
.forAddress(serverEndpoint.getHost(), serverEndpoint.getPort())
.maxInboundMessageSize(HillviewServer.MAX_MESSAGE_SIZE)
.executor(executorService)
.eventLoopGroup(workerElg)
.usePlaintext(true) // channel is unencrypted.
.build());
}
示例8: MemberServiceImpl
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
public MemberServiceImpl(String host, int port, Crypto crypto) {
Preconditions.checkNotNull(host);
Preconditions.checkNotNull(port);
InetAddress address = null;
try {
address = InetAddress.getByName(host);
} catch (UnknownHostException e) {
logger.error("Create member service failed by unknown host exception", e);
Throwables.propagate(e);
}
final Channel channel = NettyChannelBuilder
.forAddress(new InetSocketAddress(address, port))
.negotiationType(NegotiationType.PLAINTEXT)
.build();
initializeStubs(channel);
this.crypto = crypto;
}
示例9: newClient
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
@Override
public PubsubClient newClient(
@Nullable String timestampAttribute, @Nullable String idAttribute, PubsubOptions options)
throws IOException {
ManagedChannel channel = NettyChannelBuilder
.forAddress(PUBSUB_ADDRESS, PUBSUB_PORT)
.negotiationType(NegotiationType.TLS)
.sslContext(GrpcSslContexts.forClient().ciphers(null).build())
.build();
return new PubsubGrpcClient(timestampAttribute,
idAttribute,
DEFAULT_TIMEOUT_S,
channel,
options.getGcpCredential());
}
示例10: mockGrpcServerServesOverSSL
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
@Test
public void mockGrpcServerServesOverSSL() {
final NettyChannelBuilder channelBuilder =
NettyChannelBuilder.forTarget(storageProperties.getApiHost());
final TraceServiceBlockingStub plainTraceService =
TraceServiceGrpc.newBlockingStub(channelBuilder.build());
final TraceServiceBlockingStub sslTraceService =
TraceServiceGrpc.newBlockingStub(channelBuilder.sslContext(CLIENT_SSL_CONTEXT).build());
try {
plainTraceService.patchTraces(PatchTracesRequest.getDefaultInstance());
} catch (StatusRuntimeException e) {
assertThat(e.getMessage(), endsWith("Channel closed while performing protocol negotiation"));
}
sslTraceService.patchTraces(PatchTracesRequest.getDefaultInstance());
}
示例11: setUp
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
@Before
public void setUp() throws Throwable {
List<String> grpcServers = new ArrayList<String>();
grpcServers.add("127.0.0.1:2181");
RemoteDownstreamConfig.Collector.GRPC_SERVERS = grpcServers;
Config.Collector.GRPC_CHANNEL_CHECK_INTERVAL = 1;
mockStatic(NettyChannelBuilder.class);
when(NettyChannelBuilder.forAddress(anyString(), anyInt())).thenReturn(mock);
when(mock.nameResolverFactory(any(NameResolver.Factory.class))).thenReturn(mock);
when(mock.maxInboundMessageSize(anyInt())).thenReturn(mock);
when(mock.usePlaintext(true)).thenReturn(mock);
when(mock.build()).thenReturn(grpcServerRule.getChannel());
grpcChannelManager.addChannelListener(listener);
}
示例12: createClient
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
/**
* Create a P4Runtime client for this device. Returns true if the operation was successful, false otherwise.
*
* @return true if successful, false otherwise
*/
protected boolean createClient() {
deviceId = handler().data().deviceId();
controller = handler().get(P4RuntimeController.class);
String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY);
String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY);
String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY);
if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) {
log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY);
return false;
}
ManagedChannelBuilder channelBuilder = NettyChannelBuilder
.forAddress(serverAddr, Integer.valueOf(serverPortString))
.usePlaintext(true);
if (!controller.createClient(deviceId, Long.parseUnsignedLong(p4DeviceIdString), channelBuilder)) {
log.warn("Unable to create client for {}, aborting operation", deviceId);
return false;
}
return true;
}
示例13: newClientChannel
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
private ManagedChannel newClientChannel() throws CertificateException, IOException {
File clientCertChainFile = TestUtils.loadCert("client.pem");
File clientPrivateKeyFile = TestUtils.loadCert("client.key");
X509Certificate[] clientTrustedCaCerts = {
TestUtils.loadX509Cert("ca.pem")
};
SslContext sslContext =
GrpcSslContexts.forClient()
.keyManager(clientCertChainFile, clientPrivateKeyFile)
.trustManager(clientTrustedCaCerts)
.build();
return NettyChannelBuilder.forAddress("localhost", server.getPort())
.overrideAuthority(TestUtils.TEST_SERVER_HOST)
.negotiationType(NegotiationType.TLS)
.sslContext(sslContext)
.build();
}
示例14: createChannel
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
@Override
protected ManagedChannel createChannel() {
try {
NettyChannelBuilder builder = NettyChannelBuilder
.forAddress(TestUtils.testServerAddress(getPort()))
.flowControlWindow(65 * 1024)
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.sslContext(GrpcSslContexts
.forClient()
.keyManager(TestUtils.loadCert("client.pem"), TestUtils.loadCert("client.key"))
.trustManager(TestUtils.loadX509Cert("ca.pem"))
.ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE)
.sslProvider(SslProvider.OPENSSL)
.build());
io.grpc.internal.TestingAccessor.setStatsImplementation(
builder, createClientCensusStatsModule());
return builder.build();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例15: MyServiceClient
import io.grpc.netty.NettyChannelBuilder; //导入依赖的package包/类
/**
* Constructor for the client.
* @param host The host to connect to for the server
* @param port The port to connect to on the host server
*/
public MyServiceClient(String host, int port) {
InetAddress address;
try {
address = InetAddress.getByName(host);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
managedChannel = NettyChannelBuilder.forAddress(new InetSocketAddress(address,port))
.flowControlWindow(65 * 1024)
.negotiationType(NegotiationType.PLAINTEXT).build();
simpleBlockingStub = SimpleGrpc.newBlockingStub(managedChannel);
lessSimpleBlockingStub = LessSimpleGrpc.newBlockingStub(managedChannel);
}