tds

package module
v0.0.0-...-a38fd9e Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: MIT Imports: 8 Imported by: 0

README

Go-tds

Go Go Report Card codebeat badge codecov

一个基于RPC的小型微服务库, 包含服务注册中心. 使用Go编写. 总代码不超过2000行. 它只实现了微服务最核心的功能。

Example

Warning: 目前还处于开发阶段.不适用于生产环境.

Start Hub

首先您需要在启动hub.请clone本仓库的代码.

git clone https://github.com/sdttttt/go-tds.git

然后编译,运行它.

go build -v -o hub
./hub

当然, 您可以自定义你的Hub配置文件.修改目录下的tclient.yaml中的配置即可.

hub:
  address: localhost
  port: 1234 // 默认在1234端口
  checkSurvivalTime: 120 // 检查服务生存间隔时间 (Unit: Seconds)
Provider Service

您需要在您的目录下配置tclient.yaml文件.可以在主函数的当前目录,也可以在上一级. 如果您需要改变您的配置文件路径,可以使用configuration.ChangeConfigFilePath.

hub:
  address: localhost
  port: 1234

self:
  address: localhost
  port: 5555
  survivalTime: 45 // 设置服务的心跳间隔, 这个值得小于Hub上的服务检查时间间隔

编写您的服务, 这里使用golang的RPC作为示范.

功能很简单,将第一次参数值作为返回值.

package main

import (
	"log"
	"net"
	"net/http"
	"net/rpc"

	"github.com/sdttttt/go-tds/trpc"
)

type API struct{}

func (a *API) Hello(in int, out *int) error {
	*out = 100
	return nil
}

func main() {
  // 注册填入您的服务名即可
	trpc.Register("API.Hello")
	api := new(API)
  
  // 在这里注册它
  rpc.Register(api)

	l, err := net.Listen("tcp", ":4321")
	if err != nil {
		log.Println(err)
		return
	}

	rpc.HandleHTTP()

	go http.Serve(l, nil)

	select {}
}

启动它!

Customer

服务消费端需要知道Hub的地址.

hub:
  address: localhost
  port: 1234
package main

import (
	"log"

	"github.com/sdttttt/go-tds/trpc"
)

func main() {
	var one int = 1
	var two int
  
  // 服务端如果使用golang的RPC
  // go-tds提供了简单便利的库, trpc
	err := trpc.Call("API.Hello", one, &two)

	if err != nil {
		log.Fatalln(err)
	}

	println(two)
}

启动它! 您会在终端中看到

localhost : 4321
API.Hello (0x88c720,0xc000136030) (0x8770e0,0xc000136008)
1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RoundRobin = func() Balance {

	return func(len *uint8, index *uint8) uint8 {
		result := *index
		*index++
		return result
	}
}

RoundRobin Strategy

Functions

func Main

func Main()

Main is The entrance to the entire application.

Types

type Address

type Address struct {
	IP   string
	Port string
}

Address is Service Addr

type Balance

type Balance = func(len *uint8, index *uint8) uint8

Balance is Load Balancing implement. len is Service Count index is Current Location. Index ensures that the position of index and index+1 is always valid in the group.

type EndPoint

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

EndPoint is Get Service Infomation Entrance. For Service customer.

func (*EndPoint) GetServiceInfo

func (ep *EndPoint) GetServiceInfo(ctx context.Context, in *proto.ProviderInfo) (*proto.ProviderInfo, error)

GetServiceInfo is get Service Info RPC service

type Onion

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

type Options

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

Options is Hub Config Options.

func NewOptions

func NewOptions(conf *configuration.Config) *Options

NewOptions return new Option instance.

type Receiver

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

Receiver is Rpc Service Entrance. For Service Provider

func (*Receiver) JoinServiceHub

func (recv *Receiver) JoinServiceHub(ctx context.Context, info *proto.ProviderInfo) (*proto.JoinResult, error)

JoinServiceHub is External registration service info is Service Info result is Whether the service registration is Successful.

func (*Receiver) ReportActive

func (recv *Receiver) ReportActive(ctx context.Context, info *proto.ProviderInfo) (*proto.ReportResult, error)

ReportActive is Receive the heartbeat packet of the service.

type ServiceGroup

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

ServiceGroup is The Same Service, put in this group.

func NewServiceGroup

func NewServiceGroup(len uint8, bala Balance) *ServiceGroup

NewServiceGroup is Get ServiceGroup Instance.

type ServiceHub

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

ServiceHub .

func Start

func Start(opt *Options) *ServiceHub

Start is Get ServiceHub Instance.

func (*ServiceHub) Join

func (hub *ServiceHub) Join(serviceName string, service *Address)

Join is Join to the service center

func (*ServiceHub) ServiceInfo

func (hub *ServiceHub) ServiceInfo(name string) *Address

ServiceInfo is a Service Infomation.

type ServiceInstance

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

ServiceInstance is a basic service Information.

func NewServiceInstance

func NewServiceInstance(group *ServiceGroup, ip string, port string) *ServiceInstance

NewServiceInstance is initializer a new ServiceInstance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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