Client
package main
import (
"context"
"fmt"
"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"
httptransport "github.com/go-kit/kit/transport/http"
consulapi "github.com/hashicorp/consul/api"
"gomicro2/Services"
"io"
"net/url"
"os"
)
func main() {
//第一步创建client
{
config := consulapi.DefaultConfig() //初始化consul的配置
config.Address = "localhost:8500" //consul的地址
api_client, _ := consulapi.NewClient(config) //根据consul的配置初始化client
client := consul.NewClient(api_client) //根据client创建实例
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stdout)
var Tag = []string{"primary"}
instancer := consul.NewInstancer(client, logger, "userservice", Tag, true) //最后的true表示只有通过健康检查的服务才能被得到
{
factory := func(service_url string) (endpoint.Endpoint, io.Closer, error) { //factory定义了如何获得服务端的endpoint,这里的service_url是从consul中读取到的service的address我这里是192.168.3.14:8000
tart, _ := url.Parse("http://" + service_url) //server ip +8080真实服务的地址
return httptransport.NewClient("GET", tart, Services.GetUserInfo_Request, Services.GetUserInfo_Response).Endpoint(), nil, nil //我再GetUserInfo_Request里面定义了访问哪一个api把url拼接成了http://192.168.3.14:8000/v1/user/{uid}的形式
}
endpointer := sd.NewEndpointer(instancer, factory, logger)
endpoints, _ := endpointer.Endpoints() //获取所有的服务端当前server的所有endpoint函数
fmt.Println("服务有", len(endpoints), "条")
mylb := lb.NewRoundRobin(endpointer) //使用go-kit自带的轮询
for {
getUserInfo, err := mylb.Endpoint() //写死获取第一个
ctx := context.Background() //第三步:创建一个context上下文对象
//第四步:执行
res, err := getUserInfo(ctx, Services.UserRequest{Uid: 101})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//第五步:断言,得到响应值
userinfo := res.(Services.UserResponse)
fmt.Println(userinfo.Result)
}
}
}
}
}
Server
package main
import (
"flag"
"fmt"
httptransport "github.com/go-kit/kit/transport/http"
mymux "github.com/gorilla/mux"
"gomicro/Services"
"gomicro/utils"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
)
func main() {
name := flag.String("name", "", "服务名称")
port := flag.Int("port", 0, "服务端口")
flag.Parse()
if *name == "" {
log.Fatal("请指定服务名")
}
if *port == 0 {
log.Fatal("请指定端口")
}
utils.SetServiceNameAndPort(*name, *port) //设置服务名和端口
user := Services.UserService{}
endp := Services.GenUserEnPoint(user)
serverHandler := httptransport.NewServer(endp, Services.DecodeUserRequest, Services.EncodeUserResponse) //使用go kit创建server传入我们之前定义的两个解析函数
r := mymux.NewRouter()
//r.Handle(`/user/{uid:\d+}`, serverHandler) //这种写法支持多种请求方式
r.Methods("GET", "DELETE").Path(`/user/{uid:\d+}`).Handler(serverHandler) //这种写法仅支持Get,限定只能Get请求
r.Methods("GET").Path("/health").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-type", "application/json")
writer.Write([]byte(`{"status":"ok"}`))
})
errChan := make(chan error)
go func() {
utils.RegService() //调用注册服务程序
err := http.ListenAndServe(":"+strconv.Itoa(utils.ServicePort), r) //启动http服务
if err != nil {
log.Println(err)
errChan <- err
}
}()
go func() {
sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
errChan <- fmt.Errorf("%s", <-sigChan)
}()
getErr := <-errChan
utils.UnRegService()
log.Println(getErr)
}