Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
SkyAPM
SkyAPM-php-sdk
提交
d4589586
S
SkyAPM-php-sdk
项目概览
SkyAPM
/
SkyAPM-php-sdk
通知
14
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyAPM-php-sdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d4589586
编写于
6月 25, 2019
作者:
H
heyanlong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
agent
上级
6a0c2a50
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
49 addition
and
30 deletion
+49
-30
src/agent/agent.go
src/agent/agent.go
+46
-30
src/go.mod
src/go.mod
+1
-0
src/go.sum
src/go.sum
+2
-0
未找到文件。
src/agent/agent.go
浏览文件 @
d4589586
...
...
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"google.golang.org/grpc"
"io"
"log"
...
...
@@ -18,9 +19,10 @@ import (
)
type
PHPSkyBind
struct
{
version
int
appId
int32
instanceId
int32
Version
int
AppId
int32
InstanceId
int32
Uuid
string
}
type
Register
struct
{
...
...
@@ -32,6 +34,21 @@ var registerMapLock = new(sync.Mutex)
var
registerMap
sync
.
Map
var
grpcConn
*
grpc
.
ClientConn
func
ip4s
()
[]
string
{
ipv4s
,
addErr
:=
net
.
InterfaceAddrs
()
var
ips
[]
string
if
addErr
==
nil
{
for
_
,
i
:=
range
ipv4s
{
if
ipnet
,
ok
:=
i
.
(
*
net
.
IPNet
);
ok
&&
!
ipnet
.
IP
.
IsLoopback
()
{
if
ipnet
.
IP
.
To4
()
!=
nil
{
ips
=
append
(
ips
,
ipnet
.
IP
.
String
())
}
}
}
}
return
ips
}
func
register
(
c
net
.
Conn
,
j
string
)
{
defer
func
()
{
err
:=
recover
()
...
...
@@ -40,23 +57,22 @@ func register(c net.Conn, j string) {
}
}()
// todo php pid form json
info
:=
Register
{}
err
:=
json
.
Unmarshal
([]
byte
(
j
),
&
info
)
if
err
!=
nil
{
log
.
Println
(
"register => "
,
err
)
c
.
Write
([]
byte
(
"
-100000,-100000
"
))
c
.
Write
([]
byte
(
""
))
return
}
pid
:=
info
.
Pid
if
value
,
ok
:=
registerMap
.
Load
(
pid
);
ok
{
bind
:=
value
.
(
PHPSkyBind
)
log
.
Printf
(
"register => pid %d appid %d insId %d
\n
"
,
pid
,
bind
.
appId
,
bind
.
i
nstanceId
)
c
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
bind
.
appId
),
10
)
+
","
+
strconv
.
FormatInt
(
int64
(
bind
.
i
nstanceId
),
10
)))
log
.
Printf
(
"register => pid %d appid %d insId %d
\n
"
,
pid
,
bind
.
AppId
,
bind
.
I
nstanceId
)
c
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
bind
.
AppId
),
10
)
+
","
+
strconv
.
FormatInt
(
int64
(
bind
.
I
nstanceId
),
10
)))
return
}
else
{
c
.
Write
([]
byte
(
"
-100000,-100000
"
))
c
.
Write
([]
byte
(
""
))
}
registerMapLock
.
Lock
()
...
...
@@ -68,8 +84,12 @@ func register(c net.Conn, j string) {
c
:=
pb5
.
NewApplicationRegisterServiceClient
(
grpcConn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
3
)
defer
cancel
()
var
regErr
error
var
regResp
*
pb5
.
ApplicationMapping
var
regAppStatus
=
false
// loop register
for
{
regResp
,
regErr
=
c
.
ApplicationCodeRegister
(
ctx
,
&
pb5
.
Application
{
ApplicationCode
:
info
.
AppCode
,
...
...
@@ -78,12 +98,13 @@ func register(c net.Conn, j string) {
break
}
if
regResp
.
GetApplication
()
!=
nil
{
regAppStatus
=
true
break
}
time
.
Sleep
(
time
.
Second
)
}
if
reg
Err
==
nil
&&
regResp
.
GetApplication
()
!=
nil
&&
regResp
.
GetApplication
()
.
GetKey
()
==
info
.
AppCode
{
if
reg
AppStatus
{
// start reg instance
instanceClient
:=
pb5
.
NewInstanceDiscoveryServiceClient
(
grpcConn
)
instanceCtx
,
instanceCancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
3
)
...
...
@@ -92,27 +113,19 @@ func register(c net.Conn, j string) {
var
instanceErr
error
var
instanceResp
*
pb5
.
ApplicationInstanceMapping
hostName
,
_
:=
os
.
Hostname
()
ipv4s
,
addErr
:=
net
.
InterfaceAddrs
()
var
ips
[]
string
if
addErr
==
nil
{
for
_
,
i
:=
range
ipv4s
{
if
ipnet
,
ok
:=
i
.
(
*
net
.
IPNet
);
ok
&&
!
ipnet
.
IP
.
IsLoopback
()
{
if
ipnet
.
IP
.
To4
()
!=
nil
{
ips
=
append
(
ips
,
ipnet
.
IP
.
String
())
}
}
}
}
agentUUID
:=
uuid
.
New
()
.
String
()
fmt
.
Println
(
agentUUID
)
instanceReq
:=
&
pb5
.
ApplicationInstance
{
ApplicationId
:
regResp
.
Application
.
GetValue
(),
AgentUUID
:
""
,
ApplicationId
:
regResp
.
GetApplication
()
.
GetValue
(),
AgentUUID
:
agentUUID
,
RegisterTime
:
time
.
Now
()
.
UnixNano
(),
Osinfo
:
&
pb5
.
OSInfo
{
OsName
:
runtime
.
GOOS
,
Hostname
:
hostName
,
ProcessNo
:
int32
(
pid
),
Ipv4S
:
ip
s
,
Ipv4S
:
ip
4s
()
,
},
}
for
{
...
...
@@ -120,18 +133,21 @@ func register(c net.Conn, j string) {
if
instanceErr
!=
nil
{
break
}
if
instanceResp
.
GetApplicationI
d
()
==
regResp
.
GetApplication
()
.
GetValue
()
{
if
instanceResp
.
GetApplicationI
nstanceId
()
!=
0
{
break
}
time
.
Sleep
(
time
.
Second
)
}
registerMap
.
Store
(
pid
,
PHPSkyBind
{
5
,
regResp
.
GetApplication
()
.
GetValue
(),
instanceResp
.
GetApplicationInstanceId
(),
})
log
.
Println
(
"register => Start register end..."
)
if
instanceResp
!=
nil
&&
instanceResp
.
GetApplicationInstanceId
()
!=
0
{
registerMap
.
Store
(
pid
,
PHPSkyBind
{
Version
:
5
,
AppId
:
regResp
.
GetApplication
()
.
GetValue
(),
InstanceId
:
instanceResp
.
GetApplicationInstanceId
(),
Uuid
:
agentUUID
,
})
log
.
Println
(
"register => Start register end..."
)
}
}
else
{
log
.
Println
(
"register => "
,
err
)
log
.
Println
(
"register => Start register error..."
)
...
...
src/go.mod
浏览文件 @
d4589586
...
...
@@ -2,5 +2,6 @@ module agent
require (
github.com/golang/protobuf v1.3.1
github.com/google/uuid v1.1.1 // indirect
google.golang.org/grpc v1.21.1
)
src/go.sum
浏览文件 @
d4589586
...
...
@@ -8,6 +8,8 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录