mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-12 20:42:27 +08:00
init commit
This commit is contained in:
195
air-api/client/discovery.go
Normal file
195
air-api/client/discovery.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var httpClient http.Client
|
||||
var airAddr string
|
||||
var airPort int
|
||||
|
||||
func init() {
|
||||
httpClient = http.Client{
|
||||
Transport: &http.Transport{
|
||||
DisableKeepAlives: true,
|
||||
},
|
||||
Timeout: time.Second * 60,
|
||||
}
|
||||
}
|
||||
|
||||
// 设置注册发现中心地址
|
||||
func SetAirAddr(addr string, port int) {
|
||||
airAddr = addr
|
||||
airPort = port
|
||||
}
|
||||
|
||||
// 获取某个HTTP服务的所有实例
|
||||
func FetchHttpService(name string) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/http/fetch" +
|
||||
"?name=" + name
|
||||
return getAirServiceCore(url)
|
||||
}
|
||||
|
||||
// 获取全部HTTP服务的实例
|
||||
func FetchAllHttpService() (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/http/fetch/all"
|
||||
return getAirServiceCore(url)
|
||||
}
|
||||
|
||||
// 长轮询某个HTTP服务的实例状态变化
|
||||
func PollHttpService(name string) (*ResponseData, error) {
|
||||
lastTime := int64(0)
|
||||
for {
|
||||
nowTime := time.Now().UnixNano()
|
||||
if time.Duration(nowTime-lastTime) < time.Second {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
continue
|
||||
}
|
||||
lastTime = time.Now().UnixNano()
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/poll/http" +
|
||||
"?name=" + name
|
||||
responseData, err := getAirServiceCore(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if responseData.Code != 0 {
|
||||
return nil, errors.New("response code error")
|
||||
}
|
||||
if responseData.Instance == nil {
|
||||
continue
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 长轮询全部HTTP服务的实例状态变化
|
||||
func PollAllHttpService() (*ResponseData, error) {
|
||||
lastTime := int64(0)
|
||||
for {
|
||||
nowTime := time.Now().UnixNano()
|
||||
if time.Duration(nowTime-lastTime) < time.Second {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
continue
|
||||
}
|
||||
lastTime = time.Now().UnixNano()
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/poll/http/all"
|
||||
responseData, err := getAirServiceCore(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if responseData.Code != 0 {
|
||||
return nil, errors.New("response code error")
|
||||
}
|
||||
if responseData.Service == nil {
|
||||
continue
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 获取某个RPC服务的所有实例
|
||||
func FetchRpcService(name string) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/rpc/fetch" +
|
||||
"?name=" + name
|
||||
return getAirServiceCore(url)
|
||||
}
|
||||
|
||||
// 获取全部RPC服务的实例
|
||||
func FetchAllRpcService() (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/rpc/fetch/all"
|
||||
return getAirServiceCore(url)
|
||||
}
|
||||
|
||||
// 长轮询某个RPC服务的实例状态变化
|
||||
func PollRpcService(name string) (*ResponseData, error) {
|
||||
lastTime := int64(0)
|
||||
for {
|
||||
nowTime := time.Now().UnixNano()
|
||||
if time.Duration(nowTime-lastTime) < time.Second {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
continue
|
||||
}
|
||||
lastTime = time.Now().UnixNano()
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/poll/rpc" +
|
||||
"?name=" + name
|
||||
responseData, err := getAirServiceCore(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if responseData.Code != 0 {
|
||||
return nil, errors.New("response code error")
|
||||
}
|
||||
if responseData.Instance == nil {
|
||||
continue
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 长轮询全部RPC服务的实例状态变化
|
||||
func PollAllRpcService() (*ResponseData, error) {
|
||||
lastTime := int64(0)
|
||||
for {
|
||||
nowTime := time.Now().UnixNano()
|
||||
if time.Duration(nowTime-lastTime) < time.Second {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
continue
|
||||
}
|
||||
lastTime = time.Now().UnixNano()
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/poll/rpc/all"
|
||||
responseData, err := getAirServiceCore(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if responseData.Code != 0 {
|
||||
return nil, errors.New("response code error")
|
||||
}
|
||||
if responseData.Service == nil {
|
||||
continue
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getAirServiceCore(url string) (*ResponseData, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := ioutil.ReadAll(rsp.Body)
|
||||
_ = rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
responseData := new(ResponseData)
|
||||
err = json.Unmarshal(data, responseData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
84
air-api/client/register.go
Normal file
84
air-api/client/register.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 注册HTTP服务
|
||||
func RegisterHttpService(inst Instance) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/http/reg"
|
||||
return postAirServiceCore(url, inst)
|
||||
}
|
||||
|
||||
// HTTP服务心跳保持
|
||||
func KeepaliveHttpService(inst Instance) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/http/ka"
|
||||
return postAirServiceCore(url, inst)
|
||||
}
|
||||
|
||||
// 取消注册HTTP服务
|
||||
func CancelHttpService(inst Instance) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/http/cancel"
|
||||
return postAirServiceCore(url, inst)
|
||||
}
|
||||
|
||||
// 注册RPC服务
|
||||
func RegisterRpcService(inst Instance) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/rpc/reg"
|
||||
return postAirServiceCore(url, inst)
|
||||
}
|
||||
|
||||
// RPC服务心跳保持
|
||||
func KeepaliveRpcService(inst Instance) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/rpc/ka"
|
||||
return postAirServiceCore(url, inst)
|
||||
}
|
||||
|
||||
// 取消注册RPC服务
|
||||
func CancelRpcService(inst Instance) (*ResponseData, error) {
|
||||
url := "http://" +
|
||||
airAddr + ":" + strconv.Itoa(airPort) +
|
||||
"/rpc/cancel"
|
||||
return postAirServiceCore(url, inst)
|
||||
}
|
||||
|
||||
func postAirServiceCore(url string, inst Instance) (*ResponseData, error) {
|
||||
reqData, err := json.Marshal(inst)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rsp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rspData, err := ioutil.ReadAll(rsp.Body)
|
||||
_ = rsp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
responseData := new(ResponseData)
|
||||
err = json.Unmarshal(rspData, responseData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
15
air-api/client/service.go
Normal file
15
air-api/client/service.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package client
|
||||
|
||||
// 服务实例
|
||||
type Instance struct {
|
||||
ServiceName string `json:"service_name"`
|
||||
InstanceName string `json:"instance_name"`
|
||||
InstanceAddr string `json:"instance_addr"`
|
||||
}
|
||||
|
||||
// 注册中心响应实体类
|
||||
type ResponseData struct {
|
||||
Code int `json:"code"`
|
||||
Service map[string][]Instance `json:"service"`
|
||||
Instance []Instance `json:"instance"`
|
||||
}
|
||||
3
air-api/go.mod
Normal file
3
air-api/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module air-api
|
||||
|
||||
go 1.19
|
||||
Reference in New Issue
Block a user