Skip to content

Instantly share code, notes, and snippets.

@volyx
Created February 6, 2020 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volyx/837a4104ab7803e0cc3c7d7306d4a0b6 to your computer and use it in GitHub Desktop.
Save volyx/837a4104ab7803e0cc3c7d7306d4a0b6 to your computer and use it in GitHub Desktop.
client = new HelloServiceClient("http://localhost:8080", null, {format: 'text'});
let getRequest = new HelloRequest();
getRequest.setName(message);
// it sends header 'application/grpc-web-text' but Armeria expects 'application/protobuf'
client.hello(getRequest, (err, response) => {
console.log('response', response)
let reply = response.toObject().HelloReply;
console.log(reply.message);
});
import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpMethod;
import com.linecorp.armeria.common.grpc.GrpcSerializationFormats;
import com.linecorp.armeria.common.grpc.protocol.GrpcHeaderNames;
import com.linecorp.armeria.server.HttpServiceWithRoutes;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.cors.CorsServiceBuilder;
import com.linecorp.armeria.server.docs.DocServiceBuilder;
import com.linecorp.armeria.server.docs.DocServiceFilter;
import com.linecorp.armeria.server.grpc.GrpcService;
import com.linecorp.armeria.server.logging.LoggingService;
import example.armeria.grpc.Hello;
import example.armeria.grpc.HelloServiceGrpc;
import io.grpc.protobuf.services.ProtoReflectionService;
import io.grpc.reflection.v1alpha.ServerReflectionGrpc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
final Server server = newServer(8080, 8443);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.stop().join();
logger.info("Server has been stopped.");
}));
server.start().join();
logger.info("Server has been started. Serving DocService at http://127.0.0.1:{}/docs",
server.activeLocalPort());
}
static Server newServer(int httpPort, int httpsPort) throws Exception {
final CorsServiceBuilder corsBuilder =
CorsServiceBuilder.forOrigin("*")
.allowRequestMethods(HttpMethod.values()) // Allow POST method.
// Allow Content-type and X-GRPC-WEB headers.
.allowRequestHeaders(
HttpHeaderNames.CONTENT_TYPE,
HttpHeaderNames.of("X-GRPC-WEB"),
HttpHeaderNames.of("x-user-agent")
)
// Expose trailers of the HTTP response to the client.
.exposeHeaders(
GrpcHeaderNames.GRPC_STATUS,
GrpcHeaderNames.GRPC_MESSAGE,
GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN
);
final Hello.HelloRequest exampleRequest = Hello.HelloRequest.newBuilder().setName("Armeria").build();
final HttpServiceWithRoutes grpcService =
GrpcService.builder()
.addService(new HelloServiceImpl())
// See https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md
.addService(ProtoReflectionService.newInstance())
.supportedSerializationFormats(GrpcSerializationFormats.values())
.enableUnframedRequests(true)
// You can set useBlockingTaskExecutor(true) in order to execute all gRPC
// methods in the blockingTaskExecutor thread pool.
// .useBlockingTaskExecutor(true)
.build();
return Server.builder()
.http(httpPort)
.https(httpsPort)
.tlsSelfSigned()
.service(grpcService, corsBuilder.newDecorator(), LoggingService.newDecorator())
// You can access the documentation service at http://127.0.0.1:8080/docs.
// See https://line.github.io/armeria/server-docservice.html for more information.
.serviceUnder("/docs",
new DocServiceBuilder()
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"Hello", exampleRequest)
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"LazyHello", exampleRequest)
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"BlockingHello", exampleRequest)
.exclude(DocServiceFilter.ofServiceName(
ServerReflectionGrpc.SERVICE_NAME))
.build()
)
.build();
}
private Main() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment