Эх сурвалжийг харах

服务器性能监控上报完成

Tao Zhang 5 жил өмнө
parent
commit
6dbed8888b
12 өөрчлөгдсөн 523 нэмэгдсэн , 7 устгасан
  1. 4 0
      .gitignore
  2. 4 1
      README.md
  3. 8 0
      go.mod
  4. 76 0
      go.sum
  5. 28 4
      main.go
  6. 20 1
      online.go
  7. 254 0
      proto/serverload.pb.go
  8. 25 0
      proto_src/serverload.proto
  9. 26 0
      serverLoadService.go
  10. 1 0
      sma/README.md
  11. 50 0
      sma/agent.go
  12. 27 1
      snycmap.go

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+##
+.idea
+sma/agent
+main

+ 4 - 1
README.md

@@ -26,4 +26,7 @@
    - 申请服务
    - 释放归还服务
    - 调用服务
-   
+   
+```shell script
+protoc -I ./proto_src --go_out=plugins=grpc:./proto proto_src/serverload.proto
+```

+ 8 - 0
go.mod

@@ -1,3 +1,11 @@
 module app.yhyue.com/BP/servicerd
 
 go 1.14
+
+require (
+	github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
+	github.com/go-ole/go-ole v1.2.4 // indirect
+	github.com/golang/protobuf v1.4.2
+	github.com/shirou/gopsutil v2.20.6+incompatible
+	google.golang.org/grpc v1.30.0
+)

+ 76 - 0
go.sum

@@ -0,0 +1,76 @@
+cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
+github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
+github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
+github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
+github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
+github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
+github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
+github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
+github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
+github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
+github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
+github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
+github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
+github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
+github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
+github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
+github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
+github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
+github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
+github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
+github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
+github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
+github.com/shirou/gopsutil v2.20.6+incompatible h1:P37G9YH8M4vqkKcwBosp+URN5O8Tay67D2MbR361ioY=
+github.com/shirou/gopsutil v2.20.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
+golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
+golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
+golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
+golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
+golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
+google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
+google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
+google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE=
+google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
+google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
+google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
+google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
+google.golang.org/grpc v1.30.0 h1:M5a8xTlYTxwMn5ZFkwhRabsygDY5G8TYLyQDBxJNAxE=
+google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
+google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
+google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
+google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
+google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
+google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
+google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
+google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
+honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
+honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

+ 28 - 4
main.go

@@ -1,9 +1,33 @@
 package main
 
-import "fmt"
+import (
+	"app.yhyue.com/BP/servicerd/proto"
+	"flag"
+	"google.golang.org/grpc"
+	"log"
+	"net"
+)
 
+var (
+	addr = flag.String("addr", ":10021", "监听地址")
+)
+
+func init() {
+	flag.Parse()
+}
+
+//
 func main() {
-	sm := NewSyncMap()
-	sm.Put("name", "1234")
-	fmt.Println(sm.Get("name"))
+	//监听端口
+	lis, err := net.Listen("tcp", *addr)
+	if err != nil {
+		log.Fatalln(err.Error())
+		return
+	}
+	//创建一个grpc 服务器
+	s := grpc.NewServer()
+	//注册事件
+	proto.RegisterServerLoadServer(s, &ServerLoad{})
+	//处理链接
+	_ = s.Serve(lis)
 }

+ 20 - 1
online.go

@@ -12,7 +12,7 @@ import (
 const (
 	BALANCE_RANDOM = iota //随机
 	BALANCE_LOAD          //按服务器压力分配
-	BALANCE_seq           //顺序执行 ,根据服务支持的执行单元数
+	BALANCE_SEQ           //顺序执行 ,根据服务支持的执行单元数
 
 )
 
@@ -64,3 +64,22 @@ func DestoryService(serviceName string, address string) {
 }
 
 //发现服务
+func Discover(serviceName string) (string, string) {
+	//获取服务的负载策略
+	balance := balanceTable.GetWithDefault(serviceName, 0)
+	//获取可用服务节点
+	services := onlineService.Get(serviceName).(SyncMap)
+	switch balance {
+	case BALANCE_RANDOM:
+		return services.GetRandomKey().(string), ""
+	case BALANCE_LOAD:
+
+	case BALANCE_SEQ:
+	}
+	return "", ""
+}
+
+//更新服务器负载
+func UpdateServerLoad(serverIp string, load float64) {
+	serverLoad.Put(serverIp, load)
+}

+ 254 - 0
proto/serverload.pb.go

@@ -0,0 +1,254 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: serverload.proto
+
+//声明 包名
+
+package proto
+
+import (
+	context "context"
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	grpc "google.golang.org/grpc"
+	codes "google.golang.org/grpc/codes"
+	status "google.golang.org/grpc/status"
+	math "math"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+//stream请求结构
+type ServerLoadStreamReqData struct {
+	Ip                   string   `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"`
+	Load                 float64  `protobuf:"fixed64,2,opt,name=load,proto3" json:"load,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *ServerLoadStreamReqData) Reset()         { *m = ServerLoadStreamReqData{} }
+func (m *ServerLoadStreamReqData) String() string { return proto.CompactTextString(m) }
+func (*ServerLoadStreamReqData) ProtoMessage()    {}
+func (*ServerLoadStreamReqData) Descriptor() ([]byte, []int) {
+	return fileDescriptor_eb77c27475e52076, []int{0}
+}
+
+func (m *ServerLoadStreamReqData) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_ServerLoadStreamReqData.Unmarshal(m, b)
+}
+func (m *ServerLoadStreamReqData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_ServerLoadStreamReqData.Marshal(b, m, deterministic)
+}
+func (m *ServerLoadStreamReqData) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_ServerLoadStreamReqData.Merge(m, src)
+}
+func (m *ServerLoadStreamReqData) XXX_Size() int {
+	return xxx_messageInfo_ServerLoadStreamReqData.Size(m)
+}
+func (m *ServerLoadStreamReqData) XXX_DiscardUnknown() {
+	xxx_messageInfo_ServerLoadStreamReqData.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ServerLoadStreamReqData proto.InternalMessageInfo
+
+func (m *ServerLoadStreamReqData) GetIp() string {
+	if m != nil {
+		return m.Ip
+	}
+	return ""
+}
+
+func (m *ServerLoadStreamReqData) GetLoad() float64 {
+	if m != nil {
+		return m.Load
+	}
+	return 0
+}
+
+//stream返回结构
+type ServerLoadStreamResData struct {
+	Code                 string   `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *ServerLoadStreamResData) Reset()         { *m = ServerLoadStreamResData{} }
+func (m *ServerLoadStreamResData) String() string { return proto.CompactTextString(m) }
+func (*ServerLoadStreamResData) ProtoMessage()    {}
+func (*ServerLoadStreamResData) Descriptor() ([]byte, []int) {
+	return fileDescriptor_eb77c27475e52076, []int{1}
+}
+
+func (m *ServerLoadStreamResData) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_ServerLoadStreamResData.Unmarshal(m, b)
+}
+func (m *ServerLoadStreamResData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_ServerLoadStreamResData.Marshal(b, m, deterministic)
+}
+func (m *ServerLoadStreamResData) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_ServerLoadStreamResData.Merge(m, src)
+}
+func (m *ServerLoadStreamResData) XXX_Size() int {
+	return xxx_messageInfo_ServerLoadStreamResData.Size(m)
+}
+func (m *ServerLoadStreamResData) XXX_DiscardUnknown() {
+	xxx_messageInfo_ServerLoadStreamResData.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ServerLoadStreamResData proto.InternalMessageInfo
+
+func (m *ServerLoadStreamResData) GetCode() string {
+	if m != nil {
+		return m.Code
+	}
+	return ""
+}
+
+func init() {
+	proto.RegisterType((*ServerLoadStreamReqData)(nil), "proto.ServerLoadStreamReqData")
+	proto.RegisterType((*ServerLoadStreamResData)(nil), "proto.ServerLoadStreamResData")
+}
+
+func init() {
+	proto.RegisterFile("serverload.proto", fileDescriptor_eb77c27475e52076)
+}
+
+var fileDescriptor_eb77c27475e52076 = []byte{
+	// 155 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x28, 0x4e, 0x2d, 0x2a,
+	0x4b, 0x2d, 0xca, 0xc9, 0x4f, 0x4c, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x53,
+	0x4a, 0xb6, 0x5c, 0xe2, 0xc1, 0x60, 0x29, 0x9f, 0xfc, 0xc4, 0x94, 0xe0, 0x92, 0xa2, 0xd4, 0xc4,
+	0xdc, 0xa0, 0xd4, 0x42, 0x97, 0xc4, 0x92, 0x44, 0x21, 0x3e, 0x2e, 0xa6, 0xcc, 0x02, 0x09, 0x46,
+	0x05, 0x46, 0x0d, 0xce, 0x20, 0xa6, 0xcc, 0x02, 0x21, 0x21, 0x2e, 0x16, 0x90, 0x7e, 0x09, 0x26,
+	0x05, 0x46, 0x0d, 0xc6, 0x20, 0x30, 0x5b, 0x49, 0x17, 0x9b, 0xf6, 0x62, 0xb0, 0x76, 0x21, 0x2e,
+	0x96, 0xe4, 0xfc, 0x94, 0x54, 0xa8, 0x01, 0x60, 0xb6, 0x51, 0x2c, 0x17, 0x17, 0x42, 0xb9, 0x90,
+	0x3f, 0x17, 0x67, 0x40, 0x69, 0x09, 0x44, 0x97, 0x90, 0x1c, 0xc4, 0x5d, 0x7a, 0x38, 0x5c, 0x23,
+	0x85, 0x5b, 0x1e, 0x6c, 0x9d, 0x12, 0x83, 0x06, 0x63, 0x12, 0x1b, 0x58, 0x89, 0x31, 0x20, 0x00,
+	0x00, 0xff, 0xff, 0x6a, 0xae, 0x41, 0xc4, 0xee, 0x00, 0x00, 0x00,
+}
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ context.Context
+var _ grpc.ClientConnInterface
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the grpc package it is being compiled against.
+const _ = grpc.SupportPackageIsVersion6
+
+// ServerLoadClient is the client API for ServerLoad service.
+//
+// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
+type ServerLoadClient interface {
+	//rpc GetStream (StreamReqData) returns (stream StreamResData){}
+	PutStream(ctx context.Context, opts ...grpc.CallOption) (ServerLoad_PutStreamClient, error)
+}
+
+type serverLoadClient struct {
+	cc grpc.ClientConnInterface
+}
+
+func NewServerLoadClient(cc grpc.ClientConnInterface) ServerLoadClient {
+	return &serverLoadClient{cc}
+}
+
+func (c *serverLoadClient) PutStream(ctx context.Context, opts ...grpc.CallOption) (ServerLoad_PutStreamClient, error) {
+	stream, err := c.cc.NewStream(ctx, &_ServerLoad_serviceDesc.Streams[0], "/proto.ServerLoad/PutStream", opts...)
+	if err != nil {
+		return nil, err
+	}
+	x := &serverLoadPutStreamClient{stream}
+	return x, nil
+}
+
+type ServerLoad_PutStreamClient interface {
+	Send(*ServerLoadStreamReqData) error
+	CloseAndRecv() (*ServerLoadStreamResData, error)
+	grpc.ClientStream
+}
+
+type serverLoadPutStreamClient struct {
+	grpc.ClientStream
+}
+
+func (x *serverLoadPutStreamClient) Send(m *ServerLoadStreamReqData) error {
+	return x.ClientStream.SendMsg(m)
+}
+
+func (x *serverLoadPutStreamClient) CloseAndRecv() (*ServerLoadStreamResData, error) {
+	if err := x.ClientStream.CloseSend(); err != nil {
+		return nil, err
+	}
+	m := new(ServerLoadStreamResData)
+	if err := x.ClientStream.RecvMsg(m); err != nil {
+		return nil, err
+	}
+	return m, nil
+}
+
+// ServerLoadServer is the server API for ServerLoad service.
+type ServerLoadServer interface {
+	//rpc GetStream (StreamReqData) returns (stream StreamResData){}
+	PutStream(ServerLoad_PutStreamServer) error
+}
+
+// UnimplementedServerLoadServer can be embedded to have forward compatible implementations.
+type UnimplementedServerLoadServer struct {
+}
+
+func (*UnimplementedServerLoadServer) PutStream(srv ServerLoad_PutStreamServer) error {
+	return status.Errorf(codes.Unimplemented, "method PutStream not implemented")
+}
+
+func RegisterServerLoadServer(s *grpc.Server, srv ServerLoadServer) {
+	s.RegisterService(&_ServerLoad_serviceDesc, srv)
+}
+
+func _ServerLoad_PutStream_Handler(srv interface{}, stream grpc.ServerStream) error {
+	return srv.(ServerLoadServer).PutStream(&serverLoadPutStreamServer{stream})
+}
+
+type ServerLoad_PutStreamServer interface {
+	SendAndClose(*ServerLoadStreamResData) error
+	Recv() (*ServerLoadStreamReqData, error)
+	grpc.ServerStream
+}
+
+type serverLoadPutStreamServer struct {
+	grpc.ServerStream
+}
+
+func (x *serverLoadPutStreamServer) SendAndClose(m *ServerLoadStreamResData) error {
+	return x.ServerStream.SendMsg(m)
+}
+
+func (x *serverLoadPutStreamServer) Recv() (*ServerLoadStreamReqData, error) {
+	m := new(ServerLoadStreamReqData)
+	if err := x.ServerStream.RecvMsg(m); err != nil {
+		return nil, err
+	}
+	return m, nil
+}
+
+var _ServerLoad_serviceDesc = grpc.ServiceDesc{
+	ServiceName: "proto.ServerLoad",
+	HandlerType: (*ServerLoadServer)(nil),
+	Methods:     []grpc.MethodDesc{},
+	Streams: []grpc.StreamDesc{
+		{
+			StreamName:    "PutStream",
+			Handler:       _ServerLoad_PutStream_Handler,
+			ClientStreams: true,
+		},
+	},
+	Metadata: "serverload.proto",
+}

+ 25 - 0
proto_src/serverload.proto

@@ -0,0 +1,25 @@
+syntax = "proto3";//声明proto的版本 只能 是3,才支持 grpc
+
+//声明 包名
+package proto;
+
+//服务器负载数据上报
+service ServerLoad {
+  /*
+  以下 分别是 服务端 推送流, 客户端 推送流 ,双向流。
+  */
+  //rpc GetStream (StreamReqData) returns (stream StreamResData){}
+  rpc PutStream (stream ServerLoadStreamReqData) returns (ServerLoadStreamResData){}
+  //rpc AllStream (stream StreamReqData) returns (stream StreamResData){}
+}
+
+
+//stream请求结构
+message ServerLoadStreamReqData {
+  string ip = 1;
+  double load = 2;
+}
+//stream返回结构
+message ServerLoadStreamResData {
+  string code = 1;
+}

+ 26 - 0
serverLoadService.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+	"app.yhyue.com/BP/servicerd/proto"
+	"log"
+)
+
+/**
+服务器load监控服务
+*/
+type ServerLoad struct {
+}
+
+//客户端单向流,作为服务端,仅仅需要持续接受数据即可
+func (s *ServerLoad) PutStream(cliStr proto.ServerLoad_PutStreamServer) error {
+	for {
+		if tem, err := cliStr.Recv(); err == nil {
+			log.Printf("%s--%f \n", tem.Ip, tem.Load)
+			serverLoad.Put(tem.Ip, tem.Load)
+		} else {
+			log.Println("break, err :", err)
+			break
+		}
+	}
+	return nil
+}

+ 1 - 0
sma/README.md

@@ -0,0 +1 @@
+服务端性能监控工具

+ 50 - 0
sma/agent.go

@@ -0,0 +1,50 @@
+/**
+服务器端性能监控
+*/
+package main
+
+import (
+	"app.yhyue.com/BP/servicerd/proto"
+	"context"
+	"flag"
+	"github.com/shirou/gopsutil/load"
+	"google.golang.org/grpc"
+	_ "google.golang.org/grpc/balancer/grpclb"
+	"time"
+)
+
+var (
+	rdServer = flag.String("rd", "127.0.0.1:10021", "服务治理服务器地址")
+	localIp  = flag.String("lip", "192.168.3.100", "注册到服务的本机IP")
+	interval = flag.Int64("interval", 60, "上报时间间隔(Load1=1分钟,太小无意义)")
+)
+
+func init() {
+	flag.Parse()
+}
+
+//上传服务器负载数据到服务治理中心
+func uploadServerLoad() {
+	conn, err := grpc.Dial(*rdServer, grpc.WithInsecure())
+	if err != nil {
+		return
+	}
+	defer conn.Close()
+	client := proto.NewServerLoadClient(conn)
+	//调用服务端推送流
+	resp, _ := client.PutStream(context.Background())
+	tm := time.NewTicker(time.Duration(*interval) * time.Second)
+	for {
+		select {
+		case <-tm.C:
+			l, _ := load.Avg()
+			reqstreamData := &proto.ServerLoadStreamReqData{Ip: *localIp, Load: l.Load1}
+			_ = resp.Send(reqstreamData)
+		}
+	}
+}
+
+//
+func main() {
+	uploadServerLoad()
+}

+ 27 - 1
snycmap.go

@@ -3,7 +3,10 @@
 */
 package main
 
-import "sync"
+import (
+	"math/rand"
+	"sync"
+)
 
 type SyncMap struct {
 	dict map[string]interface{}
@@ -33,6 +36,29 @@ func (sm *SyncMap) Get(key string) interface{} {
 	return sm.dict[key]
 }
 
+//支持默认值
+func (sm *SyncMap) GetWithDefault(key string, defaultValue interface{}) interface{} {
+	sm.l.RLock()
+	defer sm.l.RUnlock()
+	if v, ok := sm.dict[key]; ok {
+		return v
+	} else {
+		return defaultValue
+	}
+}
+
+//
+func (sm *SyncMap) GetRandomKey() interface{} {
+	sm.l.RLock()
+	defer sm.l.RUnlock()
+	var arr = make([]interface{}, 0, 0)
+	for k, _ := range sm.dict {
+		arr = append(arr, k)
+	}
+	index := rand.Intn(len(arr))
+	return arr[index]
+}
+
 //
 func (sm *SyncMap) Has(key string) bool {
 	sm.l.RLock()