下面列出了怎么用io.grpc.util.MutableHandlerRegistry的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Before the test has started, create the server and channel.
*/
@Override
protected void before() throws Throwable {
serverName = UUID.randomUUID().toString();
serviceRegistry = new MutableHandlerRegistry();
InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(serverName)
.fallbackHandlerRegistry(serviceRegistry);
if (useDirectExecutor) {
serverBuilder.directExecutor();
}
server = serverBuilder.build().start();
InProcessChannelBuilder channelBuilder = InProcessChannelBuilder.forName(serverName);
if (useDirectExecutor) {
channelBuilder.directExecutor();
}
channel = channelBuilder.build();
}
/**
* 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();
}
@BeforeEach
public void setUp() throws IOException, URISyntaxException {
observerQueue = new LinkedBlockingQueue<>();
executor = Executors.newFixedThreadPool(2);
serviceRegistry = new MutableHandlerRegistry();
serviceRegistry.addService(new MaintenanceImplBase() {
@Override
public void snapshot(SnapshotRequest request, StreamObserver<SnapshotResponse> observer) {
try {
observerQueue.put(observer);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
fakeServer = NettyServerBuilder.forPort(TestUtil.findNextAvailablePort()).fallbackHandlerRegistry(serviceRegistry)
.directExecutor().build().start();
client = Client.builder().endpoints(new URI("http://127.0.0.1:" + fakeServer.getPort())).build();
maintenance = client.getMaintenanceClient();
}
/**
* Before the test has started, create the server and channel.
*/
@Override
public void beforeEach(ExtensionContext context) throws Exception {
serverName = UUID.randomUUID().toString();
serviceRegistry = new MutableHandlerRegistry();
InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(serverName)
.fallbackHandlerRegistry(serviceRegistry);
if (useDirectExecutor) {
serverBuilder.directExecutor();
}
server = serverBuilder.build().start();
InProcessChannelBuilder channelBuilder = InProcessChannelBuilder.forName(serverName);
if (useDirectExecutor) {
channelBuilder.directExecutor();
}
channel = channelBuilder.build();
}
/**
* Before the test has started, create the server and channel.
*/
@Override
protected void before() throws Throwable {
serverName = UUID.randomUUID().toString();
serviceRegistry = new MutableHandlerRegistry();
InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(serverName)
.fallbackHandlerRegistry(serviceRegistry);
if (useDirectExecutor) {
serverBuilder.directExecutor();
}
server = serverBuilder.build().start();
InProcessChannelBuilder channelBuilder = InProcessChannelBuilder.forName(serverName);
if (useDirectExecutor) {
channelBuilder.directExecutor();
}
channel = channelBuilder.build();
}
/**
* Set up the registry.
*/
@Setup(Level.Trial)
public void setup() throws Exception {
registry = new MutableHandlerRegistry();
fullMethodNames = new ArrayList<>(serviceCount * methodCountPerService);
for (int serviceIndex = 0; serviceIndex < serviceCount; ++serviceIndex) {
String serviceName = randomString();
ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(serviceName);
for (int methodIndex = 0; methodIndex < methodCountPerService; ++methodIndex) {
String methodName = randomString();
MethodDescriptor<Void, Void> methodDescriptor = MethodDescriptor.<Void, Void>newBuilder()
.setType(MethodDescriptor.MethodType.UNKNOWN)
.setFullMethodName(MethodDescriptor.generateFullMethodName(serviceName, methodName))
.setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
.build();
serviceBuilder.addMethod(methodDescriptor,
new ServerCallHandler<Void, Void>() {
@Override
public Listener<Void> startCall(ServerCall<Void, Void> call,
Metadata headers) {
return null;
}
});
fullMethodNames.add(methodDescriptor.getFullMethodName());
}
registry.addService(serviceBuilder.build());
}
}
@Override
public RpcServer createRpcServer(final Endpoint endpoint, final ConfigHelper<RpcServer> helper) {
final int port = Requires.requireNonNull(endpoint, "endpoint").getPort();
Requires.requireTrue(port > 0 && port < 0xFFFF, "port out of range:" + port);
final MutableHandlerRegistry handlerRegistry = new MutableHandlerRegistry();
final Server server = ServerBuilder.forPort(port) //
.fallbackHandlerRegistry(handlerRegistry) //
.directExecutor() //
.build();
final RpcServer rpcServer = new GrpcServer(server, handlerRegistry, this.parserClasses, getMarshallerRegistry());
if (helper != null) {
helper.config(rpcServer);
}
return rpcServer;
}
public GrpcServer(Server server, MutableHandlerRegistry handlerRegistry, Map<String, Message> parserClasses,
MarshallerRegistry marshallerRegistry) {
this.server = server;
this.handlerRegistry = handlerRegistry;
this.parserClasses = parserClasses;
this.marshallerRegistry = marshallerRegistry;
registerDefaultServerInterceptor();
}
/**
* Before the test has started, create the server and channel.
*/
@Override
protected void before() throws Throwable {
serviceRegistry = new MutableHandlerRegistry();
NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(getAvailablePort())
.fallbackHandlerRegistry(serviceRegistry);
configureServerBuilder(serverBuilder);
server = serverBuilder.build().start();
logger.info("Started gRPC server on port: " + server.getPort());
}
public MutableHandlerRegistry getHandlerRegistry() {
return handlerRegistry;
}
static void addGeeterService(MutableHandlerRegistry registry) {
registry.addService(new GreeterImpl());
}
static void addGeeterService(MutableHandlerRegistry registry, ServerInterceptor... interceptors) {
registry.addService(ServerInterceptors.intercept(new GreeterImpl(), interceptors));
}
/**
* Returns the service registry for this service. The registry is used to add service instances
* (e.g. {@link BindableService} or {@link ServerServiceDefinition} to the server.
*/
public final MutableHandlerRegistry getServiceRegistry() {
return serviceRegistry;
}
/**
* Returns the service registry for this service. The registry is used to add service instances
* (e.g. {@link BindableService} or {@link ServerServiceDefinition} to the server.
*/
public final MutableHandlerRegistry getServiceRegistry() {
return serviceRegistry;
}
/**
* Returns the service registry for this service. The registry is used to add service instances
* (e.g. {@link io.grpc.BindableService} or {@link io.grpc.ServerServiceDefinition} to the server.
*/
public final MutableHandlerRegistry getServiceRegistry() {
return serviceRegistry;
}
/**
* Returns the service registry for this service. The registry is used to add service instances
* (e.g. {@link BindableService} or {@link ServerServiceDefinition} to the server.
*/
public final MutableHandlerRegistry getServiceRegistry() {
return serviceRegistry;
}
/**
* Returns the service registry for this service. The registry is used to add service instances
* (e.g. {@link BindableService} or {@link ServerServiceDefinition} to the server.
*/
public final MutableHandlerRegistry getServiceRegistry() {
return serviceRegistry;
}