stark

package module
v0.0.0-...-82b0641 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2021 License: MIT Imports: 6 Imported by: 0

README

stark

Github Actions Go Report Card LICENSE

stark 命名源自漫威 Tony Stark

一个用于构建分布式系统的工具集或者轻框架,支持 grpc 和 http ,支持多种注册中心 consul ,etcd , mdns 等。

功能

  • 服务发现
  • 负载均衡
  • grpc/http Server 和 Client
  • ...

stark ctl

go get  github.com/Allenxuxu/stark/cmd/stark

查询注册服务

stark service -r consul -rg 127.0.0.1:8500 {service name}

Example

grpc server

rg, err := mdns.NewRegistry()
if err != nil {
    panic(err)
}

s := stark.NewRPCServer(rg,
    rpc.Name("stark.rpc.test"),
    rpc.Version("v0.0.1"),
)

rs := &routeGuideServer{}
pb.RegisterRouteGuideServer(s.GrpcServer(), rs)

if err := s.Start(); err != nil {
    panic(err)
}
cd example/rpc/server
go run main.go

使用 stark 工具 查看已经注册的服务

stark service stark.rpc.test

grpc client

rg, err := mdns.NewRegistry()
if err != nil {
    panic(err)
}

s, err := registry.NewSelector(rg,
    selector.BalancerName(balancer.RoundRobin),
)
if err != nil {
    panic(err)
}

client, err := stark.NewRPCClient("stark.rpc.test", s,
    rpc.GrpcDialOption(
        grpc.WithInsecure(),
    ),
)
if err != nil {
    panic(err)
}

c := routeguide.NewRouteGuideClient(client.Conn())

for i := 0; i < 10; i++ {
    resp, err := c.GetFeature(context.Background(), &routeguide.Point{
        Latitude:  0,
        Longitude: 0,
    })
    if err != nil {
        panic(err)
    }
}

http server

rg, err := mdns.NewRegistry()
if err != nil {
    panic(err)
}

r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
})

s := stark.NewRestServer(rg, r,
    rest.Name("stark.http.test"),
)

if err := s.Start(); err != nil {
    panic(err)
}

http client

rg, err := mdns.NewRegistry()
if err != nil {
    panic(err)
}

s, err := registry.NewSelector(rg)
if err != nil {
    panic(err)
}

c, err := stark.NewRestClient("stark.http.test", s)
if err != nil {
    panic(err)
}

for i := 0; i < 5; i++ {
    r, err := c.Request()
    if err != nil {
        panic(err)
    }

    resp, err := r.Get("/ping")
    if err != nil {
        panic(err)
    }

    log.Info(resp)
}

感谢

go-micro 本项目就是受到 go-micro 的启发而来,本意就是构建一个比 go-micro 更加轻量,够用就好的微服务框架。 项目中 config,registry 都是从 go-micro v1.18 改造而来。

Directories

Path Synopsis
cmd
encoder
Package encoder handles source encoding formats
Package encoder handles source encoding formats
loader
package loader manages loading from multiple sources
package loader manages loading from multiple sources
reader
Package reader parses change sets and provides config values
Package reader parses change sets and provides config values
source
Package source is the interface for sources
Package source is the interface for sources
source/file
Package file is a file source.
Package file is a file source.
source/memory
Package memory is a memory source
Package memory is a memory source
example
log
pkg
Package registry is an interface for service discovery
Package registry is an interface for service discovery
cache
Package cache provides a registry cache
Package cache provides a registry cache
etcd
Package etcd provides an etcd service registry
Package etcd provides an etcd service registry
mdns
Package mdns is a multicast dns registry Package mdns provides a multicast dns registry
Package mdns is a multicast dns registry Package mdns provides a multicast dns registry
memory
Package memory provides an in-memory registry
Package memory provides an in-memory registry
rpc
client/selector/static
Package static provides a static resolver which returns the name/ip passed in without any change
Package static provides a static resolver which returns the name/ip passed in without any change
test
rpc

Jump to

Keyboard shortcuts

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