|
| 1 | += Designing a gRPC API Specification with Anypoint Code Builder |
| 2 | +:page-deployment-options: cloud-ide, desktop-ide |
| 3 | + |
| 4 | +include::reuse::partial$beta-banner.adoc[tag="anypoint-code-builder"] |
| 5 | + |
| 6 | +Anypoint Code Builder supports designing gRPC API specifications using Protocol Buffers 3 (Protobuf 3). Create and edit `.proto` files, import reusable Protobuf libraries from Anypoint Exchange, and use syntax highlighting and validation. gRPC uses HTTP/2 and supports four RPC patterns: |
| 7 | + |
| 8 | +* Unary |
| 9 | +* Client streaming |
| 10 | +* Server streaming |
| 11 | +* Bidirectional streaming |
| 12 | +
|
| 13 | +You author service and message definitions in `.proto` files and can reuse shared types by importing Protobuf fragments (libraries) from Exchange. |
| 14 | + |
| 15 | +== Before You Begin |
| 16 | + |
| 17 | +* xref:start-acb.adoc[]. |
| 18 | +* Understand the basics of gRPC and Protocol Buffers (Protobuf 3). See https://grpc.io/docs/what-is-grpc/introduction/[Introduction to gRPC] and the https://protobuf.dev/programming-guide/proto3/[Protocol Buffers 3 Language Guide]. |
| 19 | +* Have some familiarity with xref:access-management::business-groups.adoc[]. |
| 20 | + |
| 21 | +== Create a New gRPC API Specification Project |
| 22 | + |
| 23 | +To create a gRPC API spec project in Anypoint Code Builder: |
| 24 | + |
| 25 | +include::partial$acb-reusable-steps.adoc[tags="open-ide"] |
| 26 | + |
| 27 | +. From *Create*, click *Design an API*: |
| 28 | +. Complete the *API Specification* form: |
| 29 | ++ |
| 30 | +[%header,cols="20a,60a"] |
| 31 | +|=== |
| 32 | +| Field Name | Field Value |
| 33 | + |
| 34 | +| *Project Name* | Unique name for your project. |
| 35 | + |
| 36 | +This name is used as the API spec title in Exchange, the name of the spec file, and the name of the project's root directory. |
| 37 | + |
| 38 | +include::partial$acb-reusable-steps.adoc[tags="no-project-name-reuse"] |
| 39 | +| *Project Location* | Your home directory or another directory you create. |
| 40 | + |
| 41 | +See xref:start-add-folders.adoc[]. |
| 42 | + |
| 43 | +include::partial$acb-reusable-steps.adoc[tags="proj-directory-warn"] |
| 44 | +| *API Type* | The type of API spec to create. |
| 45 | + |
| 46 | +Select *gRPC API* to create a gRPC project. |
| 47 | + |
| 48 | +| *API Specification Language* | The language for the API specification. |
| 49 | + |
| 50 | +Select *Protobuf 3* for gRPC API projects. |
| 51 | +|=== |
| 52 | +. Click *Create Project*. |
| 53 | ++ |
| 54 | +If prompted, trust the authors of the files in the folder. |
| 55 | ++ |
| 56 | +When the project is ready, the API project opens with a default `.proto` file in the Editor view. |
| 57 | ++ |
| 58 | +. Continue designing your gRPC API spec. |
| 59 | ++ |
| 60 | +As you enter elements, use xref:start-discover-ui.adoc#auto-complete[auto-complete] (or press Ctrl+Space) to display available options within the context. |
| 61 | + |
| 62 | +== Organize gRPC API Projects and Dependencies |
| 63 | + |
| 64 | +A new gRPC API project includes a root `.proto` file (for example, `main.proto` or a file named after your project) that defines your service and messages. |
| 65 | + |
| 66 | +Edit the main `.proto` file to define `service`, `rpc`, and `message` blocks. To reuse shared types, add new `.proto` files in the same project and import them. You can also add a Protobuf fragment as a dependency from Exchange and import it in your specification. |
| 67 | + |
| 68 | +== Publish a gRPC Fragment to Exchange |
| 69 | + |
| 70 | +Publish reusable `.proto` definitions as a fragment to Anypoint Exchange so other gRPC API specifications can import them as dependencies. |
| 71 | + |
| 72 | +A fragment contains shared definitions such as `message` or `enum` blocks. |
| 73 | + |
| 74 | +=== Publish the Fragment |
| 75 | + |
| 76 | +To publish a gRPC fragment to Exchange: |
| 77 | + |
| 78 | +. Ensure that you're logged in to Anypoint Platform. |
| 79 | +. Open the project that contains your reusable `.proto` files. |
| 80 | +. From the IDE, select the publish action. |
| 81 | +. When prompted: |
| 82 | ++ |
| 83 | +* Select the business group. |
| 84 | +* Provide the asset name and version. |
| 85 | +* Add any required metadata. |
| 86 | +. Confirm the publication. |
| 87 | + |
| 88 | +After publishing, the fragment appears in Exchange as an API Fragment asset and can be added as a dependency to other gRPC API specification projects. |
| 89 | + |
| 90 | +To use the fragment in another gRPC API specification: |
| 91 | + |
| 92 | +. Add the fragment as a dependency from Exchange. |
| 93 | +. Import the corresponding `.proto` file in your main specification. |
| 94 | +. Validate that all references resolve correctly. |
| 95 | + |
| 96 | +== Import a Protobuf Library from Anypoint Exchange |
| 97 | + |
| 98 | +Import reusable Protobuf libraries (fragments) from Anypoint Exchange and add them as dependencies to your gRPC API project. You can reuse messages and services across multiple specifications. |
| 99 | + |
| 100 | +To use a Protobuf fragment published to Exchange: |
| 101 | + |
| 102 | +. Ensure you're logged in to Anypoint Platform. |
| 103 | +. Open your gRPC API project. |
| 104 | +. Open the Command Palette (Cmd+Shift+P on macOS or Ctrl+Shift+P on Windows). |
| 105 | +. Select *MuleSoft: Add Fragment dependency from Exchange*. |
| 106 | +. Search for the Protobuf fragment you need and select the version to add. |
| 107 | +. After the dependency is added, the library is available under the project. |
| 108 | +. In your main `.proto` file, use `import` to reference the library's package and message or service definitions. For example: |
| 109 | ++ |
| 110 | +[source,protobuf] |
| 111 | +---- |
| 112 | +syntax = "proto3"; |
| 113 | +
|
| 114 | +import "path/to/library.proto"; |
| 115 | +
|
| 116 | +service MyService { |
| 117 | + rpc GetOrder(OrderRequest) returns (OrderResponse); |
| 118 | +} |
| 119 | +---- |
| 120 | +. Save the spec. Anypoint Code Builder resolves the dependency and validates references. You can reuse messages and services from the imported library across your gRPC specs. |
| 121 | + |
| 122 | +== Define Technical Details for gRPC Specifications |
| 123 | + |
| 124 | +Specify RPC communication patterns in your service definitions and use built-in validation to verify your Protobuf specification. |
| 125 | + |
| 126 | +=== Declare Communication Patterns for gRPC Services |
| 127 | + |
| 128 | +Anypoint Code Builder and Mule runtime support all four gRPC RPC patterns: |
| 129 | + |
| 130 | +* **Unary** – Single request, single response. |
| 131 | +* **Client streaming** – Client sends a stream of messages; server returns a single response. |
| 132 | +* **Server streaming** – Client sends a single request; server returns a stream of messages. |
| 133 | +* **Bidirectional streaming** – Both client and server send and receive streams of messages. |
| 134 | + |
| 135 | +Define these in your `.proto` file using the appropriate `rpc` declarations and stream modifiers. |
| 136 | + |
| 137 | +=== Verify Specification Accuracy with Syntax Validation |
| 138 | + |
| 139 | +Anypoint Code Builder provides syntax highlighting and validation for Protobuf 3 specifications. Validation is performed using the API Modeling Framework (AMF) parser, so you get inline feedback and error reporting as you edit your `.proto` files. Use the editor and the xref:start-open-output-panel.adoc[output panel] to track validation and scaffolding status. |
| 140 | + |
| 141 | +== See Also |
| 142 | + |
| 143 | +// TODO: Include a link to gRPC Connector docs. |
| 144 | +* xref:start-acb.adoc[] |
| 145 | +* xref:imp-implement-grpc-api-spec.adoc[] |
| 146 | +* xref:des-designing-api-specs.adoc[] |
| 147 | +* xref:des-create-api-specs.adoc[] |
| 148 | +* xref:des-create-api-fragments.adoc[] |
| 149 | +* xref:des-publish-grpc-api-to-exchange.adoc[] |
| 150 | +* xref:des-publish-api-spec-to-exchange.adoc[] |
| 151 | +* xref:start-discover-ui.adoc#use-autocomplete[Use Auto-Complete in the Editors] |
0 commit comments