Ants
![view Go网络编程群](https://img.shields.io/badge/%E5%AE%98%E6%96%B9QQ%E7%BE%A4-Go%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B(42730308)-27a5ea.svg)
Ants is a highly available micro service platform based on TP-Micro and Teleport.
简体中文
Install
go version ≥ 1.9
go get -u -f -d github.com/xiaoenai/ants/...
Feature
- Support auto service-discovery
- Supports custom service linker
- Support load balancing
- Support NIO and connection pool
- Support custom protocol
- Support custom body codec
- Support plug-in expansion
- Support heartbeat mechanism
- Detailed log information, support print input and output details
- Support for setting slow operation alarm thresholds
- Support for custom log
- Support smooth shutdown and update
- Support push handler
- Support network list:
tcp
, tcp4
, tcp6
, unix
, unixpacket
and so on
- Client support automatically redials after disconnection
Project Management
Install Ant Command
cd $GOPATH/src/github.com/xiaoenai/ants/ant
go install
Generate project
ant gen
command help:
NAME:
ant gen - Generate an ant project
USAGE:
ant gen [command options] [arguments...]
OPTIONS:
--template value, -t value The template for code generation(relative/absolute)
--app_path value, -p value The path(relative/absolute) of the project
example: ant gen -t ./__ant__tpl__.go -p ./myant
or default ant gen myant
- template file
__ant__tpl__.go
demo:
// package __ANT__TPL__ is the project template
package __ANT__TPL__
// __API__PULL__ register PULL router:
// /home
// /math/divide
type __API__PULL__ interface {
Home(*struct{}) *HomeReply
Math
}
// __API__PUSH__ register PUSH router:
// /stat
type __API__PUSH__ interface {
Stat(*StatArgs)
}
// Math controller
type Math interface {
// Divide handler
Divide(*DivideArgs) *DivideReply
}
// HomeReply home reply
type HomeReply struct {
Content string // text
}
type (
// DivideArgs divide api args
DivideArgs struct {
// dividend
A float64
// divisor
B float64 `param:"<range: 0.01:100000>"`
}
// DivideReply divide api result
DivideReply struct {
// quotient
C float64
}
)
// StatArgs stat handler args
type StatArgs struct {
Ts int64 // timestamps
}
- The template generated by
ant gen
command.
├── README.md
├── main.go
├── api
│ ├── handlers.gen.go
│ ├── handlers.go
│ ├── router.gen.go
│ └── router.go
├── logic
│ └── tmp_code.gen.go
├── sdk
│ ├── rpc.gen.go
│ ├── rpc.gen_test.go
│ ├── rpc.go
│ └── rpc_test.go
└── types
├── types.gen.go
└── types.go
Desc:
- add
.gen
suffix to the file name of the automatically generated file
tmp_code.gen.go
is temporary code used to ensure successful compilation!
When the project is completed, it should be removed!
Run project
ant run
command help:
NAME:
ant run - Compile and run gracefully (monitor changes) an any existing go project
USAGE:
ant run [options] [arguments...]
or
ant run [options except -app_path] [arguments...] {app_path}
OPTIONS:
--watch_exts value, -x value Specified to increase the listening file suffix (default: ".go", ".ini", ".yaml", ".toml", ".xml")
--app_path value, -p value The path(relative/absolute) of the project
example: ant run -x .yaml -p myant
or ant run
More Ant Command
Demo
package main
import (
micro "github.com/henrylee2cn/tp-micro"
tp "github.com/henrylee2cn/teleport"
)
// Args args
type Args struct {
A int
B int `param:"<range:1:>"`
}
// P handler
type P struct {
tp.PullCtx
}
// Divide divide API
func (p *P) Divide(args *Args) (int, *tp.Rerror) {
return args.A / args.B, nil
}
func main() {
srv := micro.NewServer(micro.SrvConfig{
ListenAddress: ":9090",
})
srv.RoutePull(new(P))
srv.Listen()
}
package main
import (
micro "github.com/henrylee2cn/tp-micro"
tp "github.com/henrylee2cn/teleport"
)
func main() {
cli := micro.NewClient(
micro.CliConfig{},
micro.NewStaticLinker(":9090"),
)
defer cli.Close()
type Args struct {
A int
B int
}
var reply int
rerr := cli.Pull("/p/divide", &Args{
A: 10,
B: 2,
}, &reply).Rerror()
if rerr != nil {
tp.Fatalf("%v", rerr)
}
tp.Infof("10/2=%d", reply)
rerr = cli.Pull("/p/divide", &Args{
A: 10,
B: 0,
}, &reply).Rerror()
if rerr == nil {
tp.Fatalf("%v", rerr)
}
tp.Infof("test binding error: ok: %v", rerr)
}
More
License
Ants is under Apache v2 License. See the LICENSE file for the full license text