Thanks to visit codestin.com
Credit goes to pkg.go.dev

rpcplatform

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

README

RPCPlatform

Build PkgGoDev GoReportCard CodeCov

An easy-to-use platform for creating microservices without complex infrastructure dependencies. Only etcd is required. Out of the box you get service discovery, tracing between services and other useful features. gRPC is used for communication between services.

etcd is required

If there is no etcd in your infrastructure, you can run it via Docker for testing:

docker run -d --name etcd \
	-p 2379:2379 -p 2380:2380 \
	-e ETCD_NAME=etcd -e ETCD_INITIAL_CLUSTER=etcd=http://127.0.0.1:2380 \
	-e ETCD_INITIAL_ADVERTISE_PEER_URLS=http://127.0.0.1:2380 -e ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 \
	-e ETCD_ADVERTISE_CLIENT_URLS=http://127.0.0.1:2379 -e ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 \
	gcr.io/etcd-development/etcd:v3.6.5

Of course, you can use Docker in production or install etcd using your favorite package manager. Just remember that the example above is for testing purposes!

How does it work?

All you need to do is assign a name to your server. When it starts, it will automatically select a free port and listen on it (unless you specify otherwise). All clients will connect to this server by its name. If there are multiple servers with the same name, load balancing is distributed among them.

In the following code examples, error handling is omitted to improve readability. A pre-built proto will also be used.

First, let's create a new rpcplatform instance:

rpcp, err := rpcplatform.New("rpcplatform", etcdClient,
	rpcplatform.PlatformOptions.ClientOptions(
		rpcplatform.ClientOptions.GRPCOptions(grpc.WithTransportCredentials(insecure.NewCredentials())),
	),
)

Now let's create a new server named myServerName, register the implementation of our Sum service and run it on localhost (sumServer implementation is omitted):

server, err := rpcp.NewServer("myServerName", "localhost:")
proto.RegisterSumServer(server.Server(), &sumServer{})
err = server.Serve()

And finally, we create a client that connects to our myServerName (sumClient usage is omitted):

client, err := rpcp.NewClient("myServerName")
sumClient := proto.NewSumClient(client.Client())

This setup is fully functional: you can add or remove copies of your server and create new clients dynamically. But to see our service graph and get telemetry for all gRPC methods, we need to run containers with telemetry services and enable telemetry in rpcplatform.

Let's run containers with Zipkin and Jaeger:

docker run -d --name zipkin -p 9411:9411 openzipkin/zipkin
docker run -d --name jaeger -p 16686:16686 -p 4317:4317 jaegertracing/all-in-one

Now let's create the necessary collectors and add the OpenTelemetry option to the rpcplatform instance:

otlpExporter, err := otlptracegrpc.New(context.Background(),
	otlptracegrpc.WithEndpoint("localhost:4317"), otlptracegrpc.WithInsecure(),
)

zipkinExporter, err := zipkin.New("http://localhost:9411/api/v2/spans")

rpcp, err := rpcplatform.New("rpcplatform", etcdClient,
	rpcplatform.PlatformOptions.ClientOptions(
		rpcplatform.ClientOptions.GRPCOptions(grpc.WithTransportCredentials(insecure.NewCredentials())),
	),
	rpcplatform.PlatformOptions.OpenTelemetry("myServiceName", 1, otlpExporter, zipkinExporter),
)

The tracing dashboards are available at:

Zipkin (http://localhost:9411) Jaeger (http://localhost:16686)
Zipkin Jaeger

Usage examples (with source code)

  • QuickStart: contains the simplest example without additional features
  • OpenTelemetry: example integrating distributed tracing systems
  • Attributes: example using additional settings for client and server

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidEtcdPrefix = errors.New("invalid etcd prefix")
	ErrInvalidTargetName = errors.New("invalid target name")
	ErrInvalidServerName = errors.New("invalid server name")
)
View Source
var (
	// PlatformOptions provides functional options for configuring RPCPlatform.
	PlatformOptions = options.Platform{}

	// ClientOptions provides functional options for configuring Client.
	ClientOptions = options.Client{}

	// ServerOptions provides functional options for configuring Server.
	ServerOptions = options.Server{}
)

Functions

This section is empty.

Types

type Attributes

type Attributes = attributes.Attributes

Attributes contains server attribute values.

func NewAttributes added in v1.5.0

func NewAttributes() *Attributes

NewAttributes returns new Attributes with default values.

type Client

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

func (*Client) Client

func (c *Client) Client() *grpc.ClientConn

Client returns the underlying gRPC ClientConn.

func (*Client) ID added in v1.5.0

func (c *Client) ID() string

ID returns the client identifier.

type ClientOption added in v1.5.0

type ClientOption = func(*config.Client)

ClientOption is a functional option that configures a Client.

type PlatformOption added in v1.5.0

type PlatformOption = func(*config.Platform)

PlatformOption is a functional option that configures an RPCPlatform.

type RPCPlatform

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

func New

func New(etcdPrefix string, etcdClient *etcd.Client, options ...PlatformOption) (*RPCPlatform, error)

New creates a new RPCPlatform for creating clients and servers. You can create one RPCPlatform instance and reuse it throughout your program. All RPCPlatform methods are thread-safe.

func (*RPCPlatform) Lookup added in v1.4.0

func (p *RPCPlatform) Lookup(ctx context.Context, target string, watch bool) (<-chan map[string]*ServerInfo, error)

Lookup returns information about available servers with the given name. If watch is true, the returned channel sends updates whenever servers change. If watch is false, the channel closes after the first update. The returned map keys are server IDs.

func (*RPCPlatform) NewClient

func (p *RPCPlatform) NewClient(target string, options ...ClientOption) (*Client, error)

NewClient creates a new client connecting to the specified server name.

func (*RPCPlatform) NewServer

func (p *RPCPlatform) NewServer(name, addr string, options ...ServerOption) (*Server, error)

NewServer creates a new server with the given name listening on addr. If addr is empty, the server listens on all available interfaces. If the port is 0, a random available port is automatically assigned.

type Server

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

func (*Server) ID added in v1.4.0

func (s *Server) ID() string

ID returns the server identifier.

func (*Server) Serve

func (s *Server) Serve() error

Serve starts the gRPC server and blocks until it exits or an error occurs.

func (*Server) Server

func (s *Server) Server() *grpc.Server

Server returns the underlying gRPC server.

type ServerInfo added in v1.5.0

type ServerInfo struct {
	Address    string
	Attributes *Attributes
}

ServerInfo contains information about a server stored in etcd.

type ServerOption added in v1.5.0

type ServerOption = func(*config.Server)

ServerOption is a functional option that configures a Server.

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

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