focus-go

module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: Apache-2.0

README

Focus-go

Focus is the next generation cross language lightweight RPC framework. It can quickly and easily develop microservice applications, which greatly simplifies RPC programming.

Focus-go is the go language implementation of the Focus.

Install

go install github.com/golang/protobuf/protoc-gen-go
go install github.com/dinstone/focus-go/protoc-gen-focus

Quick Start

  1. create a demo project and import the focus package:
> go mod init demo
> go get github.com/dinstone/focus-go
> go get github.com/golang/protobuf/protoc-gen-go
> go get github.com/dinstone/focus-go/protoc-gen-focus
  1. under the path of the project, create a protobuf file arith.proto:
syntax = "proto3";

package protobuf;
option go_package="/protobuf";

// ArithService Defining Computational Digital Services
service ArithService {
  // Add addition
  rpc Add(ArithRequest) returns (ArithResponse);
  // Sub subtraction
  rpc Sub(ArithRequest) returns (ArithResponse);
  // Mul multiplication
  rpc Mul(ArithRequest) returns (ArithResponse);
  // Div division
  rpc Div(ArithRequest) returns (ArithResponse);
}

message ArithRequest {
  int32 a = 1;
  int32 b = 2;
}

message ArithResponse {
  int32 c = 1;
}
  1. using protoc to generate code:
> protoc --focus_out=. arith.proto --go_out=. arith.proto

at this time, two files will be generated in the directory protobuf: arith.pb.go and arith.svr.go

  1. implement the ArithService in the file arith.svr.go :
package protobuf

import "errors"

// ArithService Defining Computational Digital Services
type ArithService struct{}

// Add addition
func (this *ArithService) Add(args *ArithRequest, reply *ArithResponse) error {
	reply.C = args.A + args.B
	return nil
}

// Sub subtraction
func (this *ArithService) Sub(args *ArithRequest, reply *ArithResponse) error {
	reply.C = args.A - args.B
	return nil
}

// Mul multiplication
func (this *ArithService) Mul(args *ArithRequest, reply *ArithResponse) error {
	reply.C = args.A * args.B
	return nil
}

// Div division
func (this *ArithService) Div(args *ArithRequest, reply *ArithResponse) error {
	if args.B == 0 {
		return errors.New("divided is zero")
	}
	reply.C = args.A / args.B
	return nil
}

Server

under the path of the project, we create a file named focus_server.go, create a focus server and publish service.

package main

import (
	"demo/protobuf"
	"log"
	"net"

	"github.com/dinstone/focus-go/focus"
)

func main() {
	// server
	x := options.NewServerOptions(":8082")
	x.SetSerializer(serializer.Protobuf)
	server := focus.NewServer(x)
	// register service
	err := server.RegisterName("ArithService", new(protobuf.ArithService))
	if err != nil {
		log.Fatal(err)
	}
	
	server.Start()
}

Client

under the path of the project, we create a file named focus_client.go, create a focus client and call it synchronously with the Add function:

import (
	"demo/protobuf"
	"github.com/dinstone/focus-go/focus"
...
	// client
	x := options.NewClientOptions(":8082")
	x.SetSerializer(serializer.Protobuf)
	client := focus.NewClient(x)
	defer client.Close()

	resq := protobuf.ArithRequest{A: 20, B: 5}
	resp := protobuf.ArithResponse{}
	err = client.Call("ArithService", "Add", &resq, &resp)
	log.Printf("Arith.Add(%v, %v): %v ,Error: %v", resq.A, resq.B, resp.C, err)

you can also call asynchronously, which will return a channel of type *focus.Call:

	result := client.AsyncCall("ArithService", "Add", &resq, &resp)
	select {
	case call := <-result:
		log.Printf("Arith.Add(%v, %v): %v ,Error: %v", resq.A, resq.B, resp.C, call.Error)
	case <-time.After(100 * time.Microsecond):
		log.Fatal("time out")
	}

of course, you can also compress with three supported formats gzip, snappy, zlib:

import (
    "github.com/dinstone/focus-go/focus"
    "github.com/dinstone/focus-go/focus/options"
    "github.com/dinstone/focus-go/focus/serializer"
	"github.com/dinstone/focus-go/focus/compressor"
)

...

x := options.NewServerOptions(":8008")
x.SetCompressor(compressor.Gzip)
x.SetSerializer(serializer.Protobuf)
client := focus.NewClient(x)

Custom Serializer

If you want to customize the serializer, you must implement the Serializer interface:

type Serializer interface {
    Marshal(message interface{}) ([]byte, error)
    Unmarshal(data []byte, message interface{}) error
    Type() string
}

JsonSerializer is a serializer based Json:

type JsonSerializer struct{}

func (_ JsonSerializer) Marshal(message interface{}) ([]byte, error) {
	return json.Marshal(message)
}

func (_ JsonSerializer) Unmarshal(data []byte, message interface{}) error {
	return json.Unmarshal(data, message)
}

now, we can create a HelloService with the following code:

type HelloRequest struct {
	Req string `json:"req"`
}

type HelloResponce struct {
	Resp string `json:"resp"`
}

type HelloService struct{}

func (_ *HelloService) SayHello(args *HelloRequest, reply *HelloResponce) error {
	reply.Resp = args.Req
	return nil
}

finally, we need to set the serializer on the focus server:

func main() {

	x := options.NewServerOptions(":8008")
	x.SetSerializer(serializer.Json)
	server := focus.NewServer(x)

	server.Register(new(HelloService))

	server.Start()
}

Remember that when the focus client calls the service, it also needs to set the serializer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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