2 Star 9 Fork 3

yasenagat / gokit-inaction

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
main.go 2.14 KB
Copy Edit Raw Blame History
yasenagat authored 2020-04-09 10:31 . U-更新readme
package main
import (
"flag"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/sd"
"github.com/go-kit/kit/sd/consul"
"github.com/go-kit/kit/sd/lb"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/hashicorp/consul/api"
"golang.org/x/net/context"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
)
func main() {
endpoint := newEndpoint()
s := kithttp.NewServer(func(ctx context.Context, request interface{}) (response interface{}, err error) {
return endpoint(ctx, request)
}, func(i context.Context, req *http.Request) (request interface{}, err error) {
return nil, nil
}, func(i context.Context, writer http.ResponseWriter, v interface{}) error {
if r, ok := v.(string); ok {
io.WriteString(writer, r)
}
return nil
})
http.ListenAndServe(":7777", s)
}
func newEndpoint() endpoint.Endpoint {
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
consuladdr := flag.String("consul.addr", "192.168.10.210:8500", "")
flag.Parse()
cfg := api.DefaultConfig()
cfg.Address = *consuladdr
c, e := api.NewClient(cfg)
if e != nil {
logger.Log("err", e)
os.Exit(-1)
}
kitc := consul.NewClient(c)
instancer := consul.NewInstancer(kitc, logger, "TimeSvr", nil, true)
endpointer := sd.NewEndpointer(instancer, func(instance string) (endpoint.Endpoint, io.Closer, error) {
logger.Log("instance", instance)
tgt, e := url.Parse(instance)
if e != nil {
logger.Log("err", e)
}
return kithttp.NewClient("GET", tgt, func(context context.Context, request *http.Request, i interface{}) error {
request.Body = ioutil.NopCloser(strings.NewReader(""))
return nil
}, func(i context.Context, res *http.Response) (response interface{}, err error) {
defer res.Body.Close()
byte, e := ioutil.ReadAll(res.Body)
if e != nil {
logger.Log("err", e)
}
return string(byte), nil
}).Endpoint(), nil, nil
}, logger)
balancer := lb.NewRoundRobin(endpointer)
retry := lb.Retry(3, 5000*time.Millisecond, balancer)
return retry
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/godY/gokit-inaction.git
git@gitee.com:godY/gokit-inaction.git
godY
gokit-inaction
gokit-inaction
201036b8f164

Search