You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2020/05/25 03:09:52 UTC
[servicecomb-mesher] branch master updated: 修改mesher适配最新的go-chassis (#118)
This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-mesher.git
The following commit(s) were added to refs/heads/master by this push:
new 182828d 修改mesher适配最新的go-chassis (#118)
182828d is described below
commit 182828d3f1f5db98fa4951296ad458aaf71bd104
Author: t-xinlin <t_...@sina.com>
AuthorDate: Mon May 25 11:09:42 2020 +0800
修改mesher适配最新的go-chassis (#118)
* Remove ratelimiter handler
* Add unit test for bootstrap
* 修改bootstrap适配新的go-chassis
* 修改go.mod适配新的go-chassis
* UPdate travis.yaml
* golint change
* Fix: GoSecure Checker
* Fix: travis.yam;
* Fix bootstrap unit test
* Fix: rm unused package
* Fix: unit test error
* Add unit test to dubbo protocl and http protocol modile.
* Fix: 日志打印.
Co-authored-by: “t_xinlin@sina.com <Happy100>
---
.travis.yml | 6 +-
cmd/mesher/mesher.go | 3 +
go.mod | 4 +-
proxy/bootstrap/bootstrap.go | 8 +-
proxy/bootstrap/bootstrap_test.go | 133 +++++++++++++++++++++
proxy/config/config.go | 5 +-
proxy/handler/port_rewrite.go | 5 +-
proxy/handler/skywalking_handler.go | 10 +-
proxy/pkg/egress/archaius/egress_manager.go | 11 +-
proxy/pkg/egress/egress_test.go | 1 +
proxy/protocol/dubbo/client/client_conn.go | 15 ++-
.../protocol/dubbo/client/dubbo_client_test.go | 39 +++---
proxy/protocol/dubbo/dubbo/request_test.go | 79 ++++++++++++
proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go | 3 +-
proxy/protocol/dubbo/proxy/rest2dubbo.go | 25 +++-
proxy/protocol/dubbo/server/server.go | 5 +-
proxy/protocol/http/gateway_test.go | 2 +-
proxy/protocol/http/sidecar.go | 5 +-
proxy/protocol/http/sidercar_test.go | 95 +++++++++++++++
proxy/resource/v1/route.go | 11 +-
proxy/resource/v1/status.go | 17 ++-
proxy/server/server.go | 19 ++-
22 files changed, 434 insertions(+), 67 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 53e9d90..6a56d3c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,7 @@
language: go
sudo: required
go:
- - 1.11
+ - 1.14.2
install: true
services:
@@ -35,7 +35,7 @@ jobs:
- bash -x scripts/travis/goConstChecker.sh
- stage: GoLint Checker
script:
- - go get -u github.com/golang/lint/golint
+ - go get -u golang.org/x/lint/golint
- bash -x scripts/travis/goLintChecker.sh
- stage: GoCyclo Checker
script:
@@ -48,13 +48,11 @@ jobs:
- stage: Build
script:
- GO111MODULE=on go mod download
- - GO111MODULE=on go mod vendor
- go build github.com/apache/servicecomb-mesher/cmd/mesher
- stage: Unit Test
script:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
- GO111MODULE=on go mod download
- - GO111MODULE=on go mod vendor
- bash -x scripts/travis/unit_test.sh && $HOME/gopath/bin/goveralls -coverprofile=coverage.txt -service=travis-ci
diff --git a/cmd/mesher/mesher.go b/cmd/mesher/mesher.go
index 2e062e8..8c05b6a 100644
--- a/cmd/mesher/mesher.go
+++ b/cmd/mesher/mesher.go
@@ -38,6 +38,9 @@ import (
_ "github.com/apache/servicecomb-mesher/proxy/handler/oauth2"
_ "github.com/go-chassis/go-chassis/middleware/circuit"
+
+ // rate limiter handler
+ _ "github.com/go-chassis/go-chassis/middleware/ratelimiter"
)
func main() {
diff --git a/go.mod b/go.mod
index a5a1f5b..c4ca0c0 100644
--- a/go.mod
+++ b/go.mod
@@ -4,8 +4,8 @@ require (
github.com/envoyproxy/go-control-plane v0.6.0
github.com/ghodss/yaml v1.0.0
github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4
- github.com/go-chassis/go-archaius v1.2.1-0.20200309104817-8c3d4e87d33c
- github.com/go-chassis/go-chassis v1.8.2-0.20200317131017-6cbcbc9ae9e6
+ github.com/go-chassis/go-archaius v1.3.0
+ github.com/go-chassis/go-chassis v1.8.2-0.20200505090058-f61a697f667a
github.com/go-chassis/gohessian v0.0.0-20180702061429-e5130c25af55
github.com/go-mesh/openlogging v1.0.1
github.com/gogo/googleapis v1.3.1 // indirect
diff --git a/proxy/bootstrap/bootstrap.go b/proxy/bootstrap/bootstrap.go
index 8a397ff..5295043 100644
--- a/proxy/bootstrap/bootstrap.go
+++ b/proxy/bootstrap/bootstrap.go
@@ -55,7 +55,9 @@ func Start() error {
if err := DecideMode(); err != nil {
return err
}
- metrics.Init()
+ if err := metrics.Init(); err != nil {
+ lager.Logger.Infof("metrics init error", err)
+ }
if err := v1.Init(); err != nil {
log.Println("Error occurred in starting admin server", err)
}
@@ -113,7 +115,7 @@ func GetVersion() string {
func SetHandlers() {
consumerChain := strings.Join([]string{
chassisHandler.Router,
- chassisHandler.RateLimiterConsumer,
+ "ratelimiter-consumer",
"bizkeeper-consumer",
chassisHandler.Loadbalance,
chassisHandler.Transport,
@@ -136,7 +138,7 @@ func SetHandlers() {
//InitEgressChain init the egress handler chain
func InitEgressChain() error {
egresschain := strings.Join([]string{
- handler.RateLimiterConsumer,
+ "ratelimiter-consumer",
handler.Transport,
}, ",")
diff --git a/proxy/bootstrap/bootstrap_test.go b/proxy/bootstrap/bootstrap_test.go
new file mode 100644
index 0000000..e853751
--- /dev/null
+++ b/proxy/bootstrap/bootstrap_test.go
@@ -0,0 +1,133 @@
+/*
+ * 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 bootstrap
+
+import (
+ "github.com/apache/servicecomb-mesher/proxy/cmd"
+ "github.com/apache/servicecomb-mesher/proxy/common"
+ _ "github.com/apache/servicecomb-mesher/proxy/pkg/egress/archaius"
+ "github.com/go-chassis/go-archaius"
+ "github.com/go-chassis/go-chassis/core/config"
+ "github.com/go-chassis/go-chassis/core/config/model"
+ "github.com/go-chassis/go-chassis/core/lager"
+ "github.com/go-chassis/go-chassis/pkg/util/fileutil"
+ "github.com/stretchr/testify/assert"
+ "io"
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+ // rate limiter handler
+ _ "github.com/go-chassis/go-chassis/middleware/ratelimiter"
+)
+
+var o sync.Once = sync.Once{}
+
+var yamlContent = `---
+egress:
+ infra: cse # pilot or cse
+ address: http://istio-pilot.istio-system:15010
+egressRule:
+ google-ext:
+ - hosts:
+ - "www.google.com"
+ - "*.yahoo.com"
+ ports:
+ - port: 80
+ protocol: HTTP
+ facebook-ext:
+ - hosts:
+ - "www.facebook.com"
+ ports:
+ - port: 80
+ protocol: HTTP`
+
+func TestBootstrap(t *testing.T) {
+ lager.Init(&lager.Options{LoggerLevel: "DEBUG"})
+
+ // init work dir
+ os.Setenv(fileutil.ChassisHome, filepath.Join("...", "..."))
+ os.Setenv(fileutil.ChassisConfDir, filepath.Join("...", "...", "conf"))
+ t.Log(os.Getenv("CHASSIS_HOME"))
+
+ // init archaius
+ archaius.Init(archaius.WithENVSource())
+
+ //ini config
+ config.Init()
+
+ protoMap := make(map[string]model.Protocol)
+ protoMap["http"] = model.Protocol{
+ Listen: "127.0.0.1:90909",
+ }
+ config.GlobalDefinition = &model.GlobalCfg{
+ Cse: model.CseStruct{
+ Protocols: protoMap,
+ },
+ }
+
+ configMesher := "../../conf/mesher.yaml"
+ os.Args = []string{"test", "--config", configMesher}
+ if err := cmd.Init(); err != nil {
+ panic(err)
+ }
+ if err := cmd.Configs.GeneratePortsMap(); err != nil {
+ panic(err)
+ }
+
+ // init egress.yaml file
+ d, _ := os.Getwd()
+ os.Mkdir(filepath.Join(d, "conf"), os.ModePerm)
+ filename := filepath.Join(d, "conf", "egress.yaml")
+ os.Remove(filename)
+ f1, err := os.Create(filename)
+ assert.NoError(t, err)
+ defer f1.Close()
+ _, err = io.WriteString(f1, yamlContent)
+ assert.NoError(t, err)
+
+ t.Run("Test RegisterFramework", func(t *testing.T) {
+ // case cmd.Configs.Role is empty
+ cmd.Configs.Role = ""
+ RegisterFramework()
+ // case cmd.Configs.Role == common.RoleSidecar
+ cmd.Configs.Role = common.RoleSidecar
+ RegisterFramework()
+ })
+
+ t.Run("Test Start", func(t *testing.T) {
+ // case Protocols is empty
+ config.GlobalDefinition.Cse.Protocols = map[string]model.Protocol{}
+ err := Start()
+ assert.Error(t, err)
+
+ // cmd.Configs.LocalServicePorts = "http:9090"
+ cmd.Configs.LocalServicePorts = "http:9090"
+ err = Start()
+
+ cmd.Configs.LocalServicePorts = ""
+ RegisterFramework()
+ SetHandlers()
+ err = InitEgressChain()
+ assert.NoError(t, err)
+
+ err = Start()
+ assert.NoError(t, err)
+
+ })
+}
diff --git a/proxy/config/config.go b/proxy/config/config.go
index 07ceef1..1c05f57 100644
--- a/proxy/config/config.go
+++ b/proxy/config/config.go
@@ -146,7 +146,10 @@ func SetKeyValueByFile(key, f string) string {
return ""
}
contents = string(b)
- archaius.Set(key, contents)
+ err = archaius.Set(key, contents)
+ if err != nil {
+ lager.Logger.Error("Archaius set error: " + err.Error())
+ }
return contents
}
diff --git a/proxy/handler/port_rewrite.go b/proxy/handler/port_rewrite.go
index 2555275..e00e0eb 100644
--- a/proxy/handler/port_rewrite.go
+++ b/proxy/handler/port_rewrite.go
@@ -45,7 +45,10 @@ func (ps *PortSelectionHandler) Handle(chain *handler.Chain, inv *invocation.Inv
r := &invocation.Response{
Err: err,
}
- cb(r)
+
+ if err := cb(r); err != nil {
+ openlogging.Error("Response callBack error: " + err.Error())
+ }
return
}
diff --git a/proxy/handler/skywalking_handler.go b/proxy/handler/skywalking_handler.go
index afaaec5..c0006d8 100644
--- a/proxy/handler/skywalking_handler.go
+++ b/proxy/handler/skywalking_handler.go
@@ -117,6 +117,12 @@ func NewSkyWalkingConsumer() handler.Handler {
}
func init() {
- handler.RegisterHandler(skywalking.SkyWalkingProvider, NewSkyWalkingProvier)
- handler.RegisterHandler(skywalking.SkyWalkingConsumer, NewSkyWalkingConsumer)
+ err := handler.RegisterHandler(skywalking.SkyWalkingProvider, NewSkyWalkingProvier)
+ if err != nil {
+ openlogging.GetLogger().Errorf("Handler [%s] register error: ", skywalking.SkyWalkingProvider, err.Error())
+ }
+ err = handler.RegisterHandler(skywalking.SkyWalkingConsumer, NewSkyWalkingConsumer)
+ if err != nil {
+ openlogging.GetLogger().Errorf("Handler [%s] register error: ", skywalking.SkyWalkingConsumer, err.Error())
+ }
}
diff --git a/proxy/pkg/egress/archaius/egress_manager.go b/proxy/pkg/egress/archaius/egress_manager.go
index eac986c..fb16c41 100644
--- a/proxy/pkg/egress/archaius/egress_manager.go
+++ b/proxy/pkg/egress/archaius/egress_manager.go
@@ -76,8 +76,13 @@ func (r *egressRuleEventListener) Event(e *event.Event) {
// initialize the config mgr and add several sources
func initEgressManager() error {
egressListener := &egressRuleEventListener{}
- archaius.AddFile(filepath.Join(fileutil.GetConfDir(), EgressYaml), archaius.WithFileHandler(util.UseFileNameAsKeyContentAsValue))
- archaius.RegisterListener(egressListener, ".*")
-
+ err := archaius.AddFile(filepath.Join(fileutil.GetConfDir(), EgressYaml), archaius.WithFileHandler(util.UseFileNameAsKeyContentAsValue))
+ if err != nil {
+ lager.Logger.Infof("Archaius add file failed: ", err)
+ }
+ err = archaius.RegisterListener(egressListener, ".*")
+ if err != nil {
+ lager.Logger.Infof("Archaius add file failed: ", err)
+ }
return nil
}
diff --git a/proxy/pkg/egress/egress_test.go b/proxy/pkg/egress/egress_test.go
index 05b70e8..850f77f 100644
--- a/proxy/pkg/egress/egress_test.go
+++ b/proxy/pkg/egress/egress_test.go
@@ -32,6 +32,7 @@ import (
"github.com/apache/servicecomb-mesher/proxy/pkg/egress"
"github.com/apache/servicecomb-mesher/proxy/pkg/egress/archaius"
"github.com/go-chassis/go-chassis/control"
+ _ "github.com/go-chassis/go-chassis/control/servicecomb"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/lager"
"gopkg.in/yaml.v2"
diff --git a/proxy/protocol/dubbo/client/client_conn.go b/proxy/protocol/dubbo/client/client_conn.go
index 5df4707..4f0c0d0 100644
--- a/proxy/protocol/dubbo/client/client_conn.go
+++ b/proxy/protocol/dubbo/client/client_conn.go
@@ -77,7 +77,10 @@ type DubboClientConnection struct {
//NewDubboClientConnetction is a function which create new dubbo client connection
func NewDubboClientConnetction(conn *net.TCPConn, client *DubboClient, routineMgr *util.RoutineManager) *DubboClientConnection {
tmp := new(DubboClientConnection)
- conn.SetKeepAlive(true)
+ err := conn.SetKeepAlive(true)
+ if err != nil {
+ lager.Logger.Error("TCPConn SetKeepAlive error:" + err.Error())
+ }
tmp.conn = conn
tmp.codec = dubbo.DubboCodec{}
tmp.client = client
@@ -104,7 +107,10 @@ func (this *DubboClientConnection) Close() {
}
this.closed = true
this.msgque.Deavtive()
- this.conn.Close()
+ err := this.conn.Close()
+ if err != nil {
+ lager.Logger.Error("Dubbo client connection close error:" + err.Error())
+ }
}
//MsgRecvLoop is a method which receives message
@@ -174,7 +180,10 @@ func (this *DubboClientConnection) HandleMsg(rsp *dubbo.DubboRsp) {
//SendMsg is a method which send a request
func (this *DubboClientConnection) SendMsg(req *dubbo.Request) {
//这里发送Rest请求以及收发送应答
- this.msgque.Enqueue(req)
+ err := this.msgque.Enqueue(req)
+ if err != nil {
+ lager.Logger.Error("Msg Enqueue:" + err.Error())
+ }
}
//MsgSndLoop is a method which send data
diff --git a/cmd/mesher/mesher.go b/proxy/protocol/dubbo/client/dubbo_client_test.go
similarity index 51%
copy from cmd/mesher/mesher.go
copy to proxy/protocol/dubbo/client/dubbo_client_test.go
index 2e062e8..3fb9fec 100644
--- a/cmd/mesher/mesher.go
+++ b/proxy/protocol/dubbo/client/dubbo_client_test.go
@@ -15,31 +15,26 @@
* limitations under the License.
*/
-package main
+package dubboclient
import (
- _ "net/http/pprof"
+ "testing"
+ "time"
- _ "github.com/apache/servicecomb-mesher/proxy/resolver/authority"
-
- _ "github.com/apache/servicecomb-mesher/proxy/handler"
- //protocols
- _ "github.com/apache/servicecomb-mesher/proxy/protocol/grpc"
- _ "github.com/apache/servicecomb-mesher/proxy/protocol/http"
- //ingress rule fetcher
- _ "github.com/apache/servicecomb-mesher/proxy/ingress/servicecomb"
- "github.com/apache/servicecomb-mesher/proxy/server"
-
- _ "github.com/apache/servicecomb-mesher/proxy/pkg/egress/archaius"
- _ "github.com/apache/servicecomb-mesher/proxy/pkg/egress/pilot"
-
- _ "github.com/apache/servicecomb-mesher/proxy/control/istio"
-
- _ "github.com/apache/servicecomb-mesher/proxy/handler/oauth2"
-
- _ "github.com/go-chassis/go-chassis/middleware/circuit"
+ "github.com/go-chassis/go-chassis/core/lager"
+ "github.com/stretchr/testify/assert"
)
-func main() {
- server.Run()
+func init() {
+ lager.Init(&lager.Options{
+ LoggerLevel: "INFO",
+ RollingPolicy: "size",
+ })
+}
+
+func TestClientMgr_GetClient(t *testing.T) {
+ clientMgr := NewClientMgr()
+ c, err := clientMgr.GetClient("127.0.0.1:30101", time.Second*5)
+ assert.Error(t, err)
+ assert.Nil(t, c)
}
diff --git a/proxy/protocol/dubbo/dubbo/request_test.go b/proxy/protocol/dubbo/dubbo/request_test.go
new file mode 100644
index 0000000..53a4c39
--- /dev/null
+++ b/proxy/protocol/dubbo/dubbo/request_test.go
@@ -0,0 +1,79 @@
+/*
+ * 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 dubbo
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestGenerateMsgID(t *testing.T) {
+ assert.Equal(t, int64(1), GenerateMsgID())
+ assert.Equal(t, int64(2), GenerateMsgID())
+ assert.Equal(t, int64(3), GenerateMsgID())
+}
+
+func Test_Request(t *testing.T) {
+ req := NewDubboRequest()
+
+ // broken
+ req.SetBroken(true)
+ assert.Equal(t, true, req.IsBroken())
+
+ // data
+ req.SetData("info")
+ assert.Equal(t, "info", req.GetData())
+
+ // msg id
+ req.SetMsgID(int64(101))
+ assert.Equal(t, int64(101), req.GetMsgID())
+
+ // status
+ assert.Equal(t, Ok, req.GetStatus())
+
+ // event
+ req.SetEvent("event happend")
+ assert.Equal(t, true, req.IsEvent())
+
+ // twoway
+ req.SetTwoWay(false)
+ assert.Equal(t, false, req.IsTwoWay())
+
+ // version
+ req.SetVersion("1.0.0")
+ assert.Equal(t, "1.0.0", req.mVersion)
+
+ // Attachments
+ m := make(map[string]string)
+ m["key_01"] = "value_01"
+ m["key_02"] = "value_02"
+ m["key_03"] = "value_03"
+
+ req.SetAttachments(m)
+ attch := req.GetAttachments()
+ assert.NotNil(t, attch)
+ assert.Equal(t, "value_03", attch["key_03"])
+ assert.Equal(t, "value_03", req.GetAttachment("key_03", "defaultValue"))
+ assert.Equal(t, "defaultValue", req.GetAttachment("key_04", "defaultValue"))
+
+ // method name
+ req.SetMethodName("methodname")
+ assert.Equal(t, "methodname", req.GetMethodName())
+
+}
diff --git a/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go b/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go
index a6c2bfc..48bee85 100755
--- a/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go
+++ b/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go
@@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"github.com/apache/servicecomb-mesher/proxy/cmd"
- stringutil "github.com/go-chassis/foundation/string"
"net/http"
"net/url"
@@ -42,7 +41,7 @@ import (
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/core/loadbalancer"
"github.com/go-chassis/go-chassis/pkg/runtime"
- "github.com/go-chassis/go-chassis/pkg/string"
+ stringutil "github.com/go-chassis/go-chassis/pkg/string"
"github.com/go-chassis/go-chassis/pkg/util/httputil"
"github.com/go-chassis/go-chassis/pkg/util/tags"
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix"
diff --git a/proxy/protocol/dubbo/proxy/rest2dubbo.go b/proxy/protocol/dubbo/proxy/rest2dubbo.go
index 0ccdef9..ade84d9 100755
--- a/proxy/protocol/dubbo/proxy/rest2dubbo.go
+++ b/proxy/protocol/dubbo/proxy/rest2dubbo.go
@@ -53,7 +53,9 @@ func ConvertDubboRspToRestRsp(dubboRsp *dubbo.DubboRsp, w http.ResponseWriter, c
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
- w.Write([]byte(v))
+ if _, err := w.Write([]byte(v)); err != nil {
+ return err
+ }
}
} else {
w.WriteHeader(http.StatusInternalServerError)
@@ -202,6 +204,7 @@ func TransparentForwardHandler(w http.ResponseWriter, r *http.Request) {
func handleRequestForDubbo(w http.ResponseWriter, inv *invocation.Invocation, ir *invocation.Response) error {
if ir != nil {
+ var err error
if ir.Err != nil {
switch ir.Err.(type) {
case hystrix.FallbackNullError:
@@ -210,27 +213,37 @@ func handleRequestForDubbo(w http.ResponseWriter, inv *invocation.Invocation, ir
case hystrix.CircuitError:
w.WriteHeader(http.StatusServiceUnavailable)
ir.Status = http.StatusServiceUnavailable
- w.Write([]byte(ir.Err.Error()))
+ _, err = w.Write([]byte(ir.Err.Error()))
case loadbalancer.LBError:
w.WriteHeader(http.StatusBadGateway)
ir.Status = http.StatusBadGateway
- w.Write([]byte(ir.Err.Error()))
+ _, err = w.Write([]byte(ir.Err.Error()))
default:
w.WriteHeader(http.StatusInternalServerError)
ir.Status = http.StatusInternalServerError
- w.Write([]byte(ir.Err.Error()))
+ _, err = w.Write([]byte(ir.Err.Error()))
}
+ if err != nil {
+ return err
+ }
+
return ir.Err
}
if inv.Endpoint == "" {
w.WriteHeader(http.StatusInternalServerError)
ir.Status = http.StatusInternalServerError
- w.Write([]byte(protocol.ErrUnknown.Error()))
+ _, err = w.Write([]byte(protocol.ErrUnknown.Error()))
+ if err != nil {
+ return err
+ }
return protocol.ErrUnknown
}
} else {
w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte(protocol.ErrUnExpectedHandlerChainResponse.Error()))
+ _, err := w.Write([]byte(protocol.ErrUnExpectedHandlerChainResponse.Error()))
+ if err != nil {
+ return err
+ }
return protocol.ErrUnExpectedHandlerChainResponse
}
diff --git a/proxy/protocol/dubbo/server/server.go b/proxy/protocol/dubbo/server/server.go
index 7fd8942..9f7063d 100644
--- a/proxy/protocol/dubbo/server/server.go
+++ b/proxy/protocol/dubbo/server/server.go
@@ -199,5 +199,8 @@ func initSchema() {
m[inter] = string(b)
}
- schema.SetSchemaInfoByMap(m)
+ err := schema.SetSchemaInfoByMap(m)
+ if err != nil {
+ openlogging.Error("Set schemaInfo failed: " + err.Error())
+ }
}
diff --git a/proxy/protocol/http/gateway_test.go b/proxy/protocol/http/gateway_test.go
index c27930b..2b0b6e3 100644
--- a/proxy/protocol/http/gateway_test.go
+++ b/proxy/protocol/http/gateway_test.go
@@ -6,7 +6,7 @@ import (
"github.com/apache/servicecomb-mesher/proxy/pkg/metrics"
"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-chassis/control"
- _ "github.com/go-chassis/go-chassis/control/archaius"
+ _ "github.com/go-chassis/go-chassis/control/servicecomb"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/config/model"
diff --git a/proxy/protocol/http/sidecar.go b/proxy/protocol/http/sidecar.go
index a6e1b1d..4b3a3ad 100755
--- a/proxy/protocol/http/sidecar.go
+++ b/proxy/protocol/http/sidecar.go
@@ -221,7 +221,10 @@ func copyChassisResp2HttpResp(w http.ResponseWriter, resp *http.Response) {
if err != nil {
openlogging.Error("can not copy: " + err.Error())
}
- resp.Body.Close()
+ err = resp.Body.Close()
+ if err != nil {
+ openlogging.Error("Http response close error: " + err.Error())
+ }
}
func handleRequest(w http.ResponseWriter, inv *invocation.Invocation, ir *invocation.Response) (*http.Response, error) {
if ir != nil {
diff --git a/proxy/protocol/http/sidercar_test.go b/proxy/protocol/http/sidercar_test.go
new file mode 100644
index 0000000..3a37432
--- /dev/null
+++ b/proxy/protocol/http/sidercar_test.go
@@ -0,0 +1,95 @@
+/*
+ * 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 http
+
+import (
+ "bytes"
+ "github.com/apache/servicecomb-mesher/proxy/cmd"
+ "github.com/go-chassis/go-chassis/client/rest"
+ "github.com/go-chassis/go-chassis/core/common"
+ "github.com/go-chassis/go-chassis/core/handler"
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/apache/servicecomb-mesher/proxy/pkg/metrics"
+ "github.com/go-chassis/go-chassis/core/lager"
+)
+
+func init() {
+ lager.Init(&lager.Options{
+ LoggerLevel: "INFO",
+ RollingPolicy: "size",
+ })
+
+ cmd.Init()
+
+ metrics.Init()
+
+}
+
+func TestLocalRequestHandler(t *testing.T) {
+ svr := httptest.NewServer(http.HandlerFunc(LocalRequestHandler))
+ api := svr.URL
+ rsp, err := http.Get(api)
+ if err != nil {
+ return
+ }
+ defer rsp.Body.Close()
+}
+
+func TestRemoteRequestHandler(t *testing.T) {
+ handler.CreateChains(
+ common.Provider, map[string]string{
+ "incoming": strings.Join([]string{}, ","),
+ },
+ )
+
+ handler.CreateChains(
+ common.Consumer, map[string]string{
+ "outgoing": strings.Join([]string{}, ","),
+ },
+ )
+
+ svr := httptest.NewServer(http.HandlerFunc(RemoteRequestHandler))
+ api := svr.URL
+ rsp, err := http.Get(api)
+ if err != nil {
+ return
+ }
+ defer rsp.Body.Close()
+}
+
+func TestCopyChassisResp2HttpResp(t *testing.T) {
+ svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ resp := rest.NewResponse()
+ resp.StatusCode = 200
+ b, _ := ioutil.ReadAll(r.Body)
+ resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
+ copyChassisResp2HttpResp(w, resp)
+
+ }))
+ api := svr.URL
+ rsp, err := http.Get(api)
+ if err != nil {
+ return
+ }
+ defer rsp.Body.Close()
+}
diff --git a/proxy/resource/v1/route.go b/proxy/resource/v1/route.go
index fc16b6c..7c264a4 100644
--- a/proxy/resource/v1/route.go
+++ b/proxy/resource/v1/route.go
@@ -19,6 +19,7 @@ package v1
import (
"fmt"
+ "github.com/go-mesh/openlogging"
"net/http"
"github.com/go-chassis/go-chassis/core/common"
@@ -34,10 +35,16 @@ func (a *RouteResource) RouteRuleByService(context *restful.Context) {
serviceName := context.ReadPathParameter("serviceName")
routeRule := router.DefaultRouter.FetchRouteRuleByServiceName(serviceName)
if routeRule == nil {
- context.WriteHeaderAndJSON(http.StatusNotFound, fmt.Sprintf("%s routeRule not found", serviceName), common.JSON)
+ err := context.WriteHeaderAndJSON(http.StatusNotFound, fmt.Sprintf("%s routeRule not found", serviceName), common.JSON)
+ if err != nil {
+ openlogging.Error(fmt.Sprintf("Write HeaderAndJSON error %s: ", err.Error()))
+ }
return
}
- context.WriteHeaderAndJSON(http.StatusOK, routeRule, "text/vnd.yaml")
+ err := context.WriteHeaderAndJSON(http.StatusOK, routeRule, "text/vnd.yaml")
+ if err != nil {
+ openlogging.Error(fmt.Sprintf("Write HeaderAndJSON error %s: ", err.Error()))
+ }
}
//URLPatterns helps to respond for Admin API calls
diff --git a/proxy/resource/v1/status.go b/proxy/resource/v1/status.go
index feb1181..34dcec7 100644
--- a/proxy/resource/v1/status.go
+++ b/proxy/resource/v1/status.go
@@ -18,11 +18,13 @@
package v1
import (
+ "fmt"
"github.com/apache/servicecomb-mesher/proxy/resource/v1/health"
"github.com/apache/servicecomb-mesher/proxy/resource/v1/version"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/pkg/metrics"
"github.com/go-chassis/go-chassis/server/restful"
+ "github.com/go-mesh/openlogging"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
@@ -34,10 +36,16 @@ type StatusResource struct{}
func (a *StatusResource) Health(context *restful.Context) {
healthResp := health.GetMesherHealth()
if healthResp.Status == health.Red {
- context.WriteHeaderAndJSON(http.StatusInternalServerError, healthResp, common.JSON)
+ err := context.WriteHeaderAndJSON(http.StatusInternalServerError, healthResp, common.JSON)
+ if err != nil {
+ openlogging.Error(fmt.Sprintf("Write HeaderAndJSON error %s: ", err.Error()))
+ }
return
}
- context.WriteHeaderAndJSON(http.StatusOK, healthResp, common.JSON)
+ err := context.WriteHeaderAndJSON(http.StatusOK, healthResp, common.JSON)
+ if err != nil {
+ openlogging.Error(fmt.Sprintf("Write HeaderAndJSON error %s: ", err.Error()))
+ }
}
//GetMetrics returns metrics data
@@ -50,7 +58,10 @@ func (a *StatusResource) GetMetrics(context *restful.Context) {
//GetVersion writes version in response header
func (a *StatusResource) GetVersion(context *restful.Context) {
versions := version.Ver()
- context.WriteHeaderAndJSON(http.StatusOK, versions, common.JSON)
+ err := context.WriteHeaderAndJSON(http.StatusOK, versions, common.JSON)
+ if err != nil {
+ openlogging.Error(fmt.Sprintf("Write HeaderAndJSON error %s: ", err.Error()))
+ }
}
//URLPatterns helps to respond for Admin API calls
diff --git a/proxy/server/server.go b/proxy/server/server.go
index 717f7f7..e8a895c 100644
--- a/proxy/server/server.go
+++ b/proxy/server/server.go
@@ -34,37 +34,36 @@ import (
func Run() {
// server init
if err := cmd.Init(); err != nil {
- panic(err)
+ openlogging.Fatal(err.Error())
}
if err := cmd.Configs.GeneratePortsMap(); err != nil {
- panic(err)
+ openlogging.Fatal(err.Error())
}
bootstrap.RegisterFramework()
bootstrap.SetHandlers()
if err := chassis.Init(); err != nil {
- openlogging.Error("Go chassis init failed, Mesher is not available: " + err.Error())
- panic(err)
+ openlogging.Fatal("Go chassis init failed, Mesher is not available: " + err.Error())
}
if err := bootstrap.InitEgressChain(); err != nil {
openlogging.Error("egress chain int failed: %s", openlogging.WithTags(openlogging.Tags{
"err": err.Error(),
}))
- panic(err)
+ openlogging.Fatal(err.Error())
}
if err := bootstrap.Start(); err != nil {
- openlogging.Error("Bootstrap failed: " + err.Error())
- panic(err)
+ openlogging.Fatal("Bootstrap failed: " + err.Error())
}
openlogging.Info("server start complete", openlogging.WithTags(openlogging.Tags{
"version": version.Ver().Version,
}))
if err := health.Run(); err != nil {
- openlogging.Error("Health manager start failed: " + err.Error())
- panic(err)
+ openlogging.Fatal("Health manager start failed: " + err.Error())
}
profile()
- chassis.Run()
+ if err := chassis.Run(); err != nil {
+ openlogging.Fatal("Chassis failed: " + err.Error())
+ }
}
func profile() {