Skip to content

Commit b711ed9

Browse files
authored
Merge pull request #69 from humingcheng/http-probe
使用http做地址探测
2 parents 52023d6 + fb48f22 commit b711ed9

File tree

6 files changed

+44
-808
lines changed

6 files changed

+44
-808
lines changed

.github/workflows/static_check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jobs:
55
name: Merge check
66
runs-on: ubuntu-latest
77
steps:
8-
- name: Set up Go 1.13
8+
- name: Set up Go 1.20
99
uses: actions/setup-go@v1
1010
with:
11-
go-version: 1.13
11+
go-version: 1.20
1212
id: go
1313
- name: Check out code into the Go module directory
1414
uses: actions/checkout@v1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
*.out
1515
glide.lock
1616
.idea/
17+
vendor/

client.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
DependencyPath = "/dependencies"
3939
PropertiesPath = "/properties"
4040
TokenPath = "/v4/token"
41+
ReadinessPath = "/health/readiness"
4142
HeaderContentType = "Content-Type"
4243
HeaderUserAgent = "User-Agent"
4344
HeaderAuth = "Authorization"
@@ -147,7 +148,13 @@ func NewClient(opt Options) (*Client, error) {
147148
}
148149
// Update the API Base Path based on the project
149150
c.updateAPIPath()
150-
c.pool = addresspool.NewPool(opt.Endpoints)
151+
c.pool = addresspool.NewPool(opt.Endpoints, addresspool.Options{
152+
HttpProbeOptions: &addresspool.HttpProbeOptions{
153+
Client: c.client,
154+
Protocol: c.protocol,
155+
Path: MSAPIPath + ReadinessPath,
156+
},
157+
})
151158
return c, nil
152159
}
153160

client_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,18 @@ func TestClient_DataRace(t *testing.T) {
335335
}
336336

337337
func TestClient_SyncEndpoints(t *testing.T) {
338-
mockHealthApiServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
338+
os.Setenv("CHASSIS_SC_HEALTH_CHECK_INTERVAL", "1")
339+
340+
anotherScServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
341+
writer.WriteHeader(http.StatusOK)
342+
return
343+
}))
344+
345+
scServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
339346
resp := &discovery.GetInstancesResponse{
340347
Instances: []*discovery.MicroServiceInstance{
341348
{
342-
Endpoints: []string{"rest://127.0.0.1:3000"},
349+
Endpoints: []string{"rest://" + anotherScServer.Listener.Addr().String()},
343350
HostName: "test",
344351
Status: sc.MSInstanceUP,
345352
DataCenterInfo: &discovery.DataCenterInfo{
@@ -362,12 +369,16 @@ func TestClient_SyncEndpoints(t *testing.T) {
362369

363370
c, err := sc.NewClient(
364371
sc.Options{
365-
Endpoints: []string{mockHealthApiServer.Listener.Addr().String()},
372+
Endpoints: []string{scServer.Listener.Addr().String()},
366373
})
367374
assert.NoError(t, err)
375+
assert.Equal(t, scServer.Listener.Addr().String(), c.GetAddress()) // default
368376

369-
// should use the synced address
370377
err = c.SyncEndpoints()
371-
assert.NoError(t, err)
372-
assert.Equal(t, "127.0.0.1:3000", c.GetAddress())
378+
assert.Equal(t, scServer.Listener.Addr().String(), c.GetAddress())
379+
380+
scServer.Close()
381+
time.Sleep(3*time.Second + 100*time.Millisecond)
382+
// sc stopped, should use the synced address
383+
assert.Equal(t, anotherScServer.Listener.Addr().String(), c.GetAddress())
373384
}

go.mod

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
module github.com/go-chassis/sc-client
22

3+
go 1.20
4+
35
require (
46
github.com/cenkalti/backoff/v4 v4.1.1
5-
github.com/go-chassis/cari v0.9.1-0.20241220092204-f30abdb125d9
7+
github.com/go-chassis/cari v0.9.1-0.20250118080951-ce3e155fb54e
68
github.com/go-chassis/foundation v0.4.0
79
github.com/go-chassis/openlog v1.1.3
810
github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7
911
github.com/patrickmn/go-cache v2.1.0+incompatible
1012
github.com/stretchr/testify v1.7.2
1113
)
1214

13-
go 1.13
15+
require (
16+
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/deckarep/golang-set v1.7.1 // indirect
18+
github.com/gogo/protobuf v1.3.2 // indirect
19+
github.com/karlseguin/ccache/v2 v2.0.8 // indirect
20+
github.com/kr/pretty v0.1.0 // indirect
21+
github.com/kr/text v0.2.0 // indirect
22+
github.com/pmezard/go-difflib v1.0.0 // indirect
23+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
24+
gopkg.in/yaml.v3 v3.0.1 // indirect
25+
)

0 commit comments

Comments
 (0)