tianxiaoliang commented on a change in pull request #735:
URL: 
https://github.com/apache/servicecomb-service-center/pull/735#discussion_r515759377



##########
File path: datasource/mongo/client/mongo.go
##########
@@ -0,0 +1,231 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package client
+
+import (
+       "context"
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/gopool"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "go.mongodb.org/mongo-driver/mongo"
+       "go.mongodb.org/mongo-driver/mongo/options"
+       "time"
+)
+
+const (
+       MongoDB             string = "servicecenter"
+       MongoCheckDelay            = 2 * time.Second
+       HeathChekRetryTimes        = 3
+)
+
+var (
+       mc *MongoClient
+)
+
+type MongoClient struct {
+       client      *mongo.Client
+       collections map[string]*mongo.Collection
+       db          *mongo.Database
+
+       clusterIps string
+
+       err       chan error
+       ready     chan struct{}
+       goroutine *gopool.Pool
+}
+
+func init() {
+       NewMongoClient()
+}
+
+func GetMongoClient() *MongoClient {
+       return mc
+}
+
+func NewMongoClient() {
+       inst := &MongoClient{}
+       if err := inst.Initialize(); err != nil {
+               inst.err <- err
+       }
+       mc = inst
+}
+
+func (mc *MongoClient) Initialize() (err error) {
+       mc.err = make(chan error, 1)
+       mc.ready = make(chan struct{})
+       mc.goroutine = gopool.New(context.Background())
+       mc.clusterIps = ("mongodb://localhost:27017")

Review comment:
       不要写死本地地址

##########
File path: datasource/mongo/client/mongo.go
##########
@@ -0,0 +1,231 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package client
+
+import (
+       "context"
+       "errors"
+       "github.com/apache/servicecomb-service-center/pkg/gopool"
+       "github.com/apache/servicecomb-service-center/pkg/log"
+       "go.mongodb.org/mongo-driver/mongo"
+       "go.mongodb.org/mongo-driver/mongo/options"
+       "time"
+)
+
+const (
+       MongoDB             string = "servicecenter"
+       MongoCheckDelay            = 2 * time.Second
+       HeathChekRetryTimes        = 3
+)
+
+var (
+       mc *MongoClient
+)
+
+type MongoClient struct {
+       client      *mongo.Client
+       collections map[string]*mongo.Collection
+       db          *mongo.Database
+
+       clusterIps string
+
+       err       chan error
+       ready     chan struct{}
+       goroutine *gopool.Pool
+}
+
+func init() {
+       NewMongoClient()
+}
+
+func GetMongoClient() *MongoClient {
+       return mc
+}
+
+func NewMongoClient() {
+       inst := &MongoClient{}
+       if err := inst.Initialize(); err != nil {
+               inst.err <- err
+       }
+       mc = inst
+}
+
+func (mc *MongoClient) Initialize() (err error) {
+       mc.err = make(chan error, 1)
+       mc.ready = make(chan struct{})
+       mc.goroutine = gopool.New(context.Background())
+       mc.clusterIps = ("mongodb://localhost:27017")
+       err = mc.newClient(context.Background())
+       if err != nil {
+               return
+       }
+       mc.StartHealthCheck()
+       close(mc.ready)
+       return nil
+}
+
+func (mc *MongoClient) Err() <-chan error {
+       return mc.err
+}
+
+func (mc *MongoClient) Ready() <-chan struct{} {
+       return mc.ready
+}
+
+func (mc *MongoClient) Close() {
+       if mc.client != nil {
+               mc.client.Disconnect(context.TODO())
+       }
+}
+
+func (mc *MongoClient) StartHealthCheck() {
+       mc.goroutine.Do(mc.HealthCheck)
+}
+
+func (mc *MongoClient) HealthCheck(ctx context.Context) {
+       for {
+               select {
+               case <-ctx.Done():
+                       mc.Close()
+                       return
+               case <-time.After(MongoCheckDelay):
+                       for i := 0; i < HeathChekRetryTimes; i++ {
+                               err := mc.client.Ping(context.Background(), nil)
+                               if err == nil {
+                                       break
+                               }
+                               log.Errorf(err, "retry to connect to mongodb %s 
after %s", mc.clusterIps, MongoCheckDelay)
+                               select {
+                               case <-ctx.Done():
+                                       mc.Close()
+                                       return
+                               case <-time.After(MongoCheckDelay):
+                               }
+                       }
+               }
+       }
+}
+
+func (mc *MongoClient) newClient(ctx context.Context) (err error) {
+       clientOptions := options.Client().ApplyURI(mc.clusterIps)

Review comment:
       sc的mongodb的配置要完全和kie配置文件要一模一样,模型代码要用一个

##########
File path: go.mod
##########
@@ -27,22 +28,23 @@ require (
        github.com/onsi/gomega v1.7.0
        github.com/opentracing/opentracing-go v1.1.0
        github.com/openzipkin/zipkin-go-opentracing 
v0.3.3-0.20180123190626-6bb822a7f15f
-       github.com/pkg/errors v0.8.1
+       github.com/pkg/errors v0.9.1
        github.com/prometheus/client_golang v0.9.1
        github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f
        github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1
        github.com/rs/cors v0.0.0-20170608165155-8dd4211afb5d // v1.1
        github.com/satori/go.uuid v1.1.0
-       github.com/spf13/cobra v0.0.0-20170624150100-4d647c8944eb
+       github.com/spf13/cobra v0.0.3

Review comment:
       why introduce cobra

##########
File path: go.mod
##########
@@ -10,11 +10,12 @@ require (
        github.com/coreos/pkg v0.0.0-20180108230652-97fdf19511ea // v4
        github.com/dgrijalva/jwt-go v3.2.0+incompatible
        github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3
-       github.com/go-chassis/go-archaius v1.3.2
-       github.com/go-chassis/go-chassis v0.0.0-20200826064053-d90be848aa10
+       github.com/go-chassis/go-archaius v1.3.6-0.20200917065837-57a2bca2b7ff
+       github.com/go-chassis/go-chassis v1.8.2-0.20200725085511-c410e5a49eb3 
// indirect

Review comment:
       github.com/go-chassis/go-chassis 
和github.com/go-chassis/go-chassis/v2不能同时引入,否则会出现非常大的问题,想办法把老版本清理干净




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to