natspb

package module
v0.0.0-...-93f8c24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2023 License: MIT Imports: 2 Imported by: 1

README

NATS-pb

natspb is a plugin for the Google protocol buffers compiler protoc. It generates NATS Microservices and clients of protobuf service definitions.

Through the NATS request-reply pattern we have support for load balancing and service discovery out of the box.
Combined with protobuf as the data serialization format, natspb provides a simple and efficient way to build microservices using NATS.

Installation

Compile from source

The following instructions assume you are using Go Modules for dependency management. Use a tool dependency to track the versions of the following executable packages:

// +build tools

package tools

import (
    _ "github.com/jonashiltl/nats.pb/protoc-gen-go-nats"
)

Run go mod tidy to resolve the versions. Install by running

go install github.com/jonashiltl/nats.pb/protoc-gen-go-nats

This will place four binaries in your $GOBIN;

  • protoc-gen-go-nats

Make sure that your $GOBIN is in your $PATH.

Usage

Buf

Add protoc-gen-go-nats to your buf.gen.yaml

version: v1
plugins:
  - plugin: go
    out: .
    opt: paths=source_relative
  - plugin: go-nats
    out: .
    opt: paths=source_relative
Protoc
protoc -I . --go-nats_out ./gen/go \
    your/service/v1/your_service.proto

First specify your protobuf service with the subject of protoc_gen_nats.options.nats for each method set.
You can optionally set the protoc_gen_nats.options.nats_service for each service which will be used when registering your service on NATS.

syntax = "proto3";

package example;
option go_package = "github.com/jonashiltl/nats.pb/example";

import "protoc-gen-go-nats/options/descriptor.proto";

service ExampleService {
  option(nats.pb.protoc_gen_nats.options.nats_service) = {
    name: "Example";
    description: "I'm a useful description";
    version: "1.0.0"
  };

  rpc Echo(Hello) returns (Hello) {
    option (nats.pb.protoc_gen_nats.options.nats) = {
      subject: "echo.echo";
    };
  };
}

message Hello {
  string greeting = 1;
}
Annotations

To use the nats specific method and service annotations, add buf.build/jonas/nats to your buf.yaml dependencies.

version: v1
name: ...
deps:
  - buf.build/jonas/nats

Always run buf mod update after adding a dependency to your buf.yaml.

If your using protoc you need to manually copy protoc-gen-go-nats/options/descriptor.proto and provide them to protoc when generating your stubs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NatsTransport

type NatsTransport struct {
	// contains filtered or unexported fields
}

func (*NatsTransport) Request

func (t *NatsTransport) Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error)

type Transport

type Transport interface {
	Request(subj string, data []byte, timeout time.Duration) (*nats.Msg, error)
}

func NewNatsTransport

func NewNatsTransport(nc *nats.Conn) Transport

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL