{"id":135883,"date":"2024-09-13T14:31:24","date_gmt":"2024-09-13T09:01:24","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135883"},"modified":"2024-09-13T14:31:24","modified_gmt":"2024-09-13T09:01:24","slug":"implementing-unary-api-server","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/","title":{"rendered":"Implementing Unary API Server"},"content":{"rendered":"\n<p>A unary API server in gRPC handles a single request and sends a single response. Here&#8217;s a detailed guide on how to implement one:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Define the Service and Messages<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a <code>.proto<\/code> file to define the service and message definitions.<\/li>\n\n\n\n<li>Use clear and concise names for services and messages.<\/li>\n\n\n\n<li>Specify the request and response types for the unary API.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Protocol Buffers<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>syntax = \"proto3\";\n\nservice Greeter {\n  rpc SayHello(HelloRequest) returns (HelloReply) {}\n}\n\nmessage HelloRequest {\n  string name = 1;\n}\n\nmessage HelloReply {\n  string message = 1;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Generate Server Code<\/strong> &nbsp;<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>protoc<\/code> compiler to generate server code from the <code>.proto<\/code> file.<\/li>\n\n\n\n<li>The generated code will include an interface that defines the service and a base class for implementing the service.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface GreeterGrpc {\n  public static abstract class GreeterImplBase extends io.grpc.stub.AbstractStub&lt;GreeterImplBase&gt; {\n    ...\n  }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Implement the Service<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a class that extends the generated base class.<\/li>\n\n\n\n<li>Override the method corresponding to the unary API you defined.<\/li>\n\n\n\n<li>Implement the logic for handling the request and generating the response.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class GreeterImpl extends GreeterGrpc.GreeterImplBase {\n    @Override\n    public void sayHello(HelloRequest request, StreamObserver&lt;HelloReply&gt; responseObserver) {\n        HelloReply reply = HelloReply.newBuilder().setMessage(\"Hello, &nbsp;  \" + request.getName()).build();\n        responseObserver.onNext(reply);\n        responseObserver.onCompleted(); &nbsp; \n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Start the Server<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a <code>ManagedChannelBuilder<\/code> object to configure the server.<\/li>\n\n\n\n<li>Set the server address and port.<\/li>\n\n\n\n<li>Build the channel and start the server.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Server {\n    public static void main(String&#91;] args) throws IOException, InterruptedException {\n        ServerBuilder serverBuilder = ServerBuilder.forPort(50051); &nbsp; \n        serverBuilder.addService(new GreeterImpl());\n        Server server = serverBuilder.build();\n\n        server.start();\n        System.out.println(\"Server started on port \" + server.getPort());\n\n        server.awaitTermination();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>By following these steps, you can create a basic unary API server in gRPC. You can customize the server implementation to handle specific use cases and add additional features as needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A unary API server in gRPC handles a single request and sends a single response. Here&#8217;s a detailed guide on how to implement one: 1. Define the Service and Messages Example: Protocol Buffers 2. Generate Server Code &nbsp; Example: Java 3. Implement the Service Example: Java 4. Start the Server Example: Java By following these&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-135883","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Implementing Unary API Server - Tutorial<\/title>\n<meta name=\"description\" content=\"Discover how to implement a Unary API server, handling single-request and single-response interactions efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing Unary API Server - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Discover how to implement a Unary API server, handling single-request and single-response interactions efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/\",\"name\":\"Implementing Unary API Server - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-13T09:01:24+00:00\",\"description\":\"Discover how to implement a Unary API server, handling single-request and single-response interactions efficiently.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Implementing Unary API Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implementing Unary API Server - Tutorial","description":"Discover how to implement a Unary API server, handling single-request and single-response interactions efficiently.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/","og_locale":"en_US","og_type":"article","og_title":"Implementing Unary API Server - Tutorial","og_description":"Discover how to implement a Unary API server, handling single-request and single-response interactions efficiently.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/","name":"Implementing Unary API Server - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-13T09:01:24+00:00","description":"Discover how to implement a Unary API server, handling single-request and single-response interactions efficiently.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/implementing-unary-api-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Implementing Unary API Server"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=135883"}],"version-history":[{"count":1,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135883\/revisions"}],"predecessor-version":[{"id":135889,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135883\/revisions\/135889"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}