This is an automated email from the ASF dual-hosted git repository.

ztelur pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu.git


The following commit(s) were added to refs/heads/develop by this push:
     new 0a54226f [feature] direct dubbo invoke (#434)
0a54226f is described below

commit 0a54226fce5bf308218e0ebcd312174ffd892f46
Author: randy <[email protected]>
AuthorDate: Sun Jun 5 15:06:26 2022 +0800

    [feature] direct dubbo invoke (#434)
    
    * http to dubbo with direct generic call
    
    * http to dubbo with direct generic call
    
    * fix comment
    
    * fix comment
    
    * fix comment
---
 pkg/common/constant/key.go                         |  43 ++--
 pkg/filter/http/dubboproxy/dubbo.go                | 244 ++++++++++++++++++
 pkg/pluginregistry/registry.go                     |   1 +
 pkg/tracing/driver.go                              |   2 +-
 .../simple/direct/docker/docker-compose.yml        |  27 ++
 samples/dubbogo/simple/direct/pixiu/conf.yaml      |  48 ++++
 samples/dubbogo/simple/direct/server/app/server.go |  65 +++++
 samples/dubbogo/simple/direct/server/app/user.go   | 275 +++++++++++++++++++++
 .../simple/direct/server/profiles/dev/log.yml      |  45 ++++
 .../simple/direct/server/profiles/dev/server.yml   |  39 +++
 samples/dubbogo/simple/direct/test/pixiu_test.go   |  88 +++++++
 11 files changed, 855 insertions(+), 22 deletions(-)

diff --git a/pkg/common/constant/key.go b/pkg/common/constant/key.go
index 8a92736a..f0d11349 100644
--- a/pkg/common/constant/key.go
+++ b/pkg/common/constant/key.go
@@ -22,27 +22,28 @@ const (
        GRPCConnectManagerFilter  = "dgp.filter.grpcconnectionmanager"
        DubboConnectManagerFilter = "dgp.filter.network.dubboconnectionmanager"
 
-       HTTPAuthorityFilter      = "dgp.filter.http.authority"
-       HTTPProxyFilter          = "dgp.filter.http.httpproxy"
-       HTTPHeaderFilter         = "dgp.filter.http.header"
-       HTTPHostFilter           = "dgp.filter.http.host"
-       HTTPMetricFilter         = "dgp.filter.http.metric"
-       HTTPRecoveryFilter       = "dgp.filter.http.recovery"
-       HTTPResponseFilter       = "dgp.filter.http.response"
-       HTTPAccessLogFilter      = "dgp.filter.http.accesslog"
-       HTTPRateLimitFilter      = "dgp.filter.http.ratelimit"
-       HTTPGrpcProxyFilter      = "dgp.filter.http.grpcproxy"
-       HTTPDubboProxyFilter     = "dgp.filter.http.dubboproxy"
-       HTTPApiConfigFilter      = "dgp.filter.http.apiconfig"
-       HTTPTimeoutFilter        = "dgp.filter.http.timeout"
-       TracingFilter            = "dgp.filters.tracing"
-       HTTPCircuitBreakerFilter = "dgp.filter.http.circuitbreaker"
-       HTTPAuthJwtFilter        = "dgp.filter.http.auth.jwt"
-       HTTPCorsFilter           = "dgp.filter.http.cors"
-       HTTPCsrfFilter           = "dgp.filter.http.csrf"
-       HTTPProxyRewriteFilter   = "dgp.filter.http.proxyrewrite"
-       HTTPLoadBalanceFilter    = "dgp.filter.http.loadbalance"
-       HTTPEventFilter          = "dgp.filter.http.event"
+       HTTPAuthorityFilter        = "dgp.filter.http.authority"
+       HTTPProxyFilter            = "dgp.filter.http.httpproxy"
+       HTTPHeaderFilter           = "dgp.filter.http.header"
+       HTTPHostFilter             = "dgp.filter.http.host"
+       HTTPMetricFilter           = "dgp.filter.http.metric"
+       HTTPRecoveryFilter         = "dgp.filter.http.recovery"
+       HTTPResponseFilter         = "dgp.filter.http.response"
+       HTTPAccessLogFilter        = "dgp.filter.http.accesslog"
+       HTTPRateLimitFilter        = "dgp.filter.http.ratelimit"
+       HTTPGrpcProxyFilter        = "dgp.filter.http.grpcproxy"
+       HTTPDubboProxyFilter       = "dgp.filter.http.dubboproxy"
+       HTTPDirectDubboProxyFilter = "dgp.filter.http.directdubboproxy"
+       HTTPApiConfigFilter        = "dgp.filter.http.apiconfig"
+       HTTPTimeoutFilter          = "dgp.filter.http.timeout"
+       TracingFilter              = "dgp.filters.tracing"
+       HTTPCircuitBreakerFilter   = "dgp.filter.http.circuitbreaker"
+       HTTPAuthJwtFilter          = "dgp.filter.http.auth.jwt"
+       HTTPCorsFilter             = "dgp.filter.http.cors"
+       HTTPCsrfFilter             = "dgp.filter.http.csrf"
+       HTTPProxyRewriteFilter     = "dgp.filter.http.proxyrewrite"
+       HTTPLoadBalanceFilter      = "dgp.filter.http.loadbalance"
+       HTTPEventFilter            = "dgp.filter.http.event"
 
        DubboHttpFilter  = "dgp.filter.dubbo.http"
        DubboProxyFilter = "dgp.filter.dubbo.proxy"
diff --git a/pkg/filter/http/dubboproxy/dubbo.go 
b/pkg/filter/http/dubboproxy/dubbo.go
new file mode 100644
index 00000000..e358d24c
--- /dev/null
+++ b/pkg/filter/http/dubboproxy/dubbo.go
@@ -0,0 +1,244 @@
+/*
+ * 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 dubboproxy
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "io/ioutil"
+       "net/http"
+       "reflect"
+       "strings"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       dubboConstant "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
+       "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
+       hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+       pixiuHttp "github.com/apache/dubbo-go-pixiu/pkg/context/http"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+       "github.com/apache/dubbo-go-pixiu/pkg/server"
+)
+
+const (
+       // Kind is the kind of plugin.
+       Kind = constant.HTTPDirectDubboProxyFilter
+)
+
+func init() {
+       filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+
+       // Plugin is http to dubbo with direct generic call filter plugin.
+       Plugin struct{}
+
+       // FilterFactory is http to dubbo with direct generic call filter 
instance
+       FilterFactory struct {
+               cfg *Config
+       }
+
+       // Filter http to dubbo with direct generic call
+       Filter struct{}
+
+       // Config http to dubbo with direct generic call config
+       Config struct{}
+)
+
+// Kind return plugin kind
+func (p *Plugin) Kind() string {
+       return Kind
+}
+
+// CreateFilterFactory create filter factory instance
+func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) {
+       return &FilterFactory{cfg: &Config{}}, nil
+}
+
+// Config return filter facotry config, now is empty
+func (factory *FilterFactory) Config() interface{} {
+       return factory.cfg
+}
+
+// Apply init filter factory, now is empty
+func (factory *FilterFactory) Apply() error {
+       return nil
+}
+
+// PrepareFilterChain prepare filter chain
+func (factory *FilterFactory) PrepareFilterChain(ctx *pixiuHttp.HttpContext, 
chain filter.FilterChain) error {
+       f := &Filter{}
+       chain.AppendDecodeFilters(f)
+       return nil
+}
+
+// Decode handle http request to dubbo direct generic call and return http 
response
+func (f *Filter) Decode(hc *pixiuHttp.HttpContext) filter.FilterStatus {
+       rEntry := hc.GetRouteEntry()
+       if rEntry == nil {
+               logger.Info("[dubbo-go-pixiu] http not match route")
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: "not match 
route"})
+               hc.SendLocalReply(http.StatusNotFound, bt)
+               return filter.Stop
+       }
+       logger.Debugf("[dubbo-go-pixiu] client choose endpoint from cluster 
:%v", rEntry.Cluster)
+
+       clusterName := rEntry.Cluster
+       clusterManager := server.GetClusterManager()
+       endpoint := clusterManager.PickEndpoint(clusterName)
+       if endpoint == nil {
+               logger.Info("[dubbo-go-pixiu] cluster not found endpoint")
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: "cluster 
not found endpoint"})
+               hc.SendLocalReply(http.StatusServiceUnavailable, bt)
+               return filter.Stop
+       }
+
+       // http://host/{application}/{service}/{method} or 
https://host/{application}/{service}/{method}
+       rawPath := hc.Request.URL.Path
+       rawPath = strings.Trim(rawPath, "/")
+       splits := strings.Split(rawPath, "/")
+
+       if len(splits) != 3 {
+               logger.Info("[dubbo-go-pixiu] http path pattern error. path 
pattern should be http://127.0.0.1/{application}/{service}/{method}";)
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: "http path 
pattern error"})
+               hc.SendLocalReply(http.StatusBadRequest, bt)
+               return filter.Stop
+       }
+
+       service := splits[1]
+       method := splits[2]
+       interfaceKey := service
+
+       groupKey := hc.Request.Header.Get(constant.DubboGroup)
+       if groupKey == "" {
+               groupKey = "default"
+       }
+       versionKey := hc.Request.Header.Get(constant.DubboServiceVersion)
+       if versionKey == "" {
+               versionKey = "1.0.0"
+       }
+
+       rawBody, err := ioutil.ReadAll(hc.Request.Body)
+       if err != nil {
+               logger.Infof("[dubbo-go-pixiu] read request body error %v", err)
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: 
fmt.Sprintf("read request body error %v", err)})
+               hc.SendLocalReply(http.StatusBadRequest, bt)
+               return filter.Stop
+       }
+
+       mapBody := map[string]interface{}{}
+       if err := json.Unmarshal(rawBody, &mapBody); err != nil {
+               logger.Infof("[dubbo-go-pixiu] unmarshal request body error 
%v", err)
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: 
fmt.Sprintf("unmarshal request body error %v", err)})
+               hc.SendLocalReply(http.StatusBadRequest, bt)
+               return filter.Stop
+       }
+
+       inIArr := make([]interface{}, 3)
+       inVArr := make([]reflect.Value, 3)
+       inIArr[0] = method
+
+       var (
+               typesList  []string
+               valuesList []hessian.Object
+       )
+
+       types := mapBody["types"]
+       if typesString, ok := types.(string); ok {
+               typesList = strings.Split(typesString, ",")
+       } else if _, ok = types.([]string); ok {
+               typesList = types.([]string)
+       }
+
+       values := mapBody["values"]
+       if _, ok := values.([]interface{}); ok {
+               for _, v := range values.([]interface{}) {
+                       valuesList = append(valuesList, v)
+               }
+       } else {
+               valuesList = append(valuesList, values)
+       }
+
+       inIArr[1] = typesList
+       inIArr[2] = valuesList
+
+       inVArr[0] = reflect.ValueOf(inIArr[0])
+       inVArr[1] = reflect.ValueOf(inIArr[1])
+       inVArr[2] = reflect.ValueOf(inIArr[2])
+
+       invoc := 
invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("$invoke"),
+               invocation.WithArguments(inIArr),
+               invocation.WithParameterValues(inVArr))
+
+       url, err := common.NewURL(endpoint.Address.GetAddress(),
+               common.WithProtocol(dubbo.DUBBO), 
common.WithParamsValue(dubboConstant.SerializationKey, 
dubboConstant.Hessian2Serialization),
+               common.WithParamsValue(dubboConstant.GenericFilterKey, "true"),
+               common.WithParamsValue(dubboConstant.InterfaceKey, 
interfaceKey),
+               common.WithParamsValue(dubboConstant.ReferenceFilterKey, 
"generic,filter"),
+               // dubboAttachment must contains group and version info
+               common.WithParamsValue(dubboConstant.GroupKey, groupKey),
+               common.WithParamsValue(dubboConstant.VersionKey, versionKey),
+               common.WithPath(interfaceKey),
+       )
+       if err != nil {
+               logger.Infof("[dubbo-go-pixiu] newURL error %v", err)
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: 
fmt.Sprintf("newURL error %v", err)})
+               hc.SendLocalReply(http.StatusServiceUnavailable, bt)
+               return filter.Stop
+       }
+
+       dubboProtocol := dubbo.NewDubboProtocol()
+
+       // TODO: will print many Error when failed to connect server
+       invoker := dubboProtocol.Refer(url)
+       if invoker == nil {
+               logger.Info("[dubbo-go-pixiu] dubbo protocol refer error")
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: "dubbo 
protocol refer error"})
+               hc.SendLocalReply(http.StatusServiceUnavailable, bt)
+               return filter.Stop
+       }
+       var resp interface{}
+       invoc.SetReply(&resp)
+
+       invCtx := context.Background()
+       result := invoker.Invoke(invCtx, invoc)
+       result.SetAttachments(invoc.Attachments())
+
+       if result.Error() != nil {
+               logger.Debugf("[dubbo-go-pixiu] invoke result error %v", 
result.Error())
+               bt, _ := json.Marshal(pixiuHttp.ErrResponse{Message: 
fmt.Sprintf("invoke result error %v", result.Error())})
+               hc.SendLocalReply(http.StatusServiceUnavailable, bt)
+               return filter.Stop
+       }
+
+       value := reflect.ValueOf(result.Result())
+       result.SetResult(value.Elem().Interface())
+       hc.SourceResp = resp
+       invoker.Destroy()
+       // response write in hcm
+       return filter.Continue
+}
diff --git a/pkg/pluginregistry/registry.go b/pkg/pluginregistry/registry.go
index 64650677..56902032 100644
--- a/pkg/pluginregistry/registry.go
+++ b/pkg/pluginregistry/registry.go
@@ -30,6 +30,7 @@ import (
        _ "github.com/apache/dubbo-go-pixiu/pkg/filter/header"
        _ "github.com/apache/dubbo-go-pixiu/pkg/filter/host"
        _ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/apiconfig"
+       _ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/dubboproxy"
        _ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/grpcproxy"
        _ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/httpproxy"
        _ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/loadbalancer"
diff --git a/pkg/tracing/driver.go b/pkg/tracing/driver.go
index fb685d95..253f2d5f 100644
--- a/pkg/tracing/driver.go
+++ b/pkg/tracing/driver.go
@@ -77,7 +77,7 @@ func NewTraceDriver() *TraceDriver {
 func InitDriver(bs *model.Bootstrap) *TraceDriver {
        config := bs.Trace
        if config == nil {
-               logger.Warnf("[dubbo-go-pixiu] no trace configuration in 
conf.yaml")
+               logger.Info("[dubbo-go-pixiu] no trace configuration in 
conf.yaml")
                return nil
        }
        ctx := context.Background()
diff --git a/samples/dubbogo/simple/direct/docker/docker-compose.yml 
b/samples/dubbogo/simple/direct/docker/docker-compose.yml
new file mode 100644
index 00000000..a3d294f3
--- /dev/null
+++ b/samples/dubbogo/simple/direct/docker/docker-compose.yml
@@ -0,0 +1,27 @@
+#
+# Licensed to 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. Apache Software Foundation (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.
+#
+
+version: '3'
+
+services:
+  zookeeper:
+    image: zookeeper
+    ports:
+      - 2181:2181
+    restart: on-failure
\ No newline at end of file
diff --git a/samples/dubbogo/simple/direct/pixiu/conf.yaml 
b/samples/dubbogo/simple/direct/pixiu/conf.yaml
new file mode 100644
index 00000000..556d7408
--- /dev/null
+++ b/samples/dubbogo/simple/direct/pixiu/conf.yaml
@@ -0,0 +1,48 @@
+#
+# 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.
+#
+---
+static_resources:
+  listeners:
+    - name: "net/http"
+      protocol_type: "HTTP"
+      address:
+        socket_address:
+          address: "0.0.0.0"
+          port: 8883
+      filter_chains:
+          filters:
+            - name: dgp.filter.httpconnectionmanager
+              config:
+                route_config:
+                  routes:
+                    - match:
+                        prefix: "/UserService"
+                      route:
+                        cluster: "user"
+                http_filters:
+                  - name: dgp.filter.http.directdubboproxy
+                    config:
+  clusters:
+    - name: "user"
+      lb_policy: "lb"
+      endpoints:
+        - id: 1
+          socket_address:
+            address: 127.0.0.1
+            port: 20000
\ No newline at end of file
diff --git a/samples/dubbogo/simple/direct/server/app/server.go 
b/samples/dubbogo/simple/direct/server/app/server.go
new file mode 100644
index 00000000..d49e5e06
--- /dev/null
+++ b/samples/dubbogo/simple/direct/server/app/server.go
@@ -0,0 +1,65 @@
+/*
+ * 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 main
+
+import (
+       "fmt"
+       "os"
+       "os/signal"
+       "syscall"
+       "time"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/logger"
+       "dubbo.apache.org/dubbo-go/v3/config"
+       _ "dubbo.apache.org/dubbo-go/v3/imports"
+)
+
+var survivalTimeout = int(3e9)
+
+// they are necessary:
+// export DUBBO_GO_CONFIG_PATH="../profiles/dev/server.yml"
+// export APP_LOG_CONF_FILE="../profiles/dev/log.yml"
+func main() {
+       config.Load()
+       initSignal()
+}
+
+func initSignal() {
+       signals := make(chan os.Signal, 1)
+       // It is not possible to block SIGKILL or syscall.SIGSTOP
+       signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, 
syscall.SIGTERM, syscall.SIGINT)
+       for {
+               sig := <-signals
+               logger.Infof("get signal %s", sig.String())
+               switch sig {
+               case syscall.SIGHUP:
+                       // reload()
+               default:
+                       time.AfterFunc(time.Duration(survivalTimeout), func() {
+                               logger.Warnf("app exit now by force...")
+                               os.Exit(1)
+                       })
+
+                       // The program exits normally or timeout forcibly exits.
+                       fmt.Println("provider app exit now...")
+                       return
+               }
+       }
+}
diff --git a/samples/dubbogo/simple/direct/server/app/user.go 
b/samples/dubbogo/simple/direct/server/app/user.go
new file mode 100644
index 00000000..6df7c480
--- /dev/null
+++ b/samples/dubbogo/simple/direct/server/app/user.go
@@ -0,0 +1,275 @@
+/*
+ * 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 main
+
+import (
+       "context"
+       "errors"
+       "fmt"
+       "sync"
+       "time"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/config"
+
+       hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+func init() {
+       config.SetProviderService(new(UserProvider))
+       // ------for hessian2------
+       hessian.RegisterPOJO(&User{})
+
+       cache = newUserDB()
+
+       t1, _ := time.Parse(
+               time.RFC3339,
+               "2021-08-01T10:08:41+00:00")
+
+       cache.Add(&User{ID: "0001", Code: 1, Name: "tc", Age: 18, Time: t1})
+       cache.Add(&User{ID: "0002", Code: 2, Name: "ic", Age: 88, Time: t1})
+}
+
+var cache *userDB
+
+// userDB cache user.
+type userDB struct {
+       // key is name, value is user obj
+       nameIndex map[string]*User
+       // key is code, value is user obj
+       codeIndex map[int64]*User
+       lock      sync.Mutex
+}
+
+// userDB create func
+func newUserDB() *userDB {
+       return &userDB{
+               nameIndex: make(map[string]*User, 16),
+               codeIndex: make(map[int64]*User, 16),
+               lock:      sync.Mutex{},
+       }
+}
+
+// nolint
+func (db *userDB) Add(u *User) bool {
+       db.lock.Lock()
+       defer db.lock.Unlock()
+
+       if u.Name == "" || u.Code <= 0 {
+               return false
+       }
+
+       if !db.existName(u.Name) && !db.existCode(u.Code) {
+               return db.AddForName(u) && db.AddForCode(u)
+       }
+
+       return false
+}
+
+// nolint
+func (db *userDB) AddForName(u *User) bool {
+       if len(u.Name) == 0 {
+               return false
+       }
+
+       if _, ok := db.nameIndex[u.Name]; ok {
+               return false
+       }
+
+       db.nameIndex[u.Name] = u
+       return true
+}
+
+// nolint
+func (db *userDB) AddForCode(u *User) bool {
+       if u.Code <= 0 {
+               return false
+       }
+
+       if _, ok := db.codeIndex[u.Code]; ok {
+               return false
+       }
+
+       db.codeIndex[u.Code] = u
+       return true
+}
+
+// nolint
+func (db *userDB) GetByName(n string) (*User, bool) {
+       db.lock.Lock()
+       defer db.lock.Unlock()
+
+       r, ok := db.nameIndex[n]
+       return r, ok
+}
+
+// nolint
+func (db *userDB) GetByCode(n int64) (*User, bool) {
+       db.lock.Lock()
+       defer db.lock.Unlock()
+
+       r, ok := db.codeIndex[n]
+       return r, ok
+}
+
+func (db *userDB) existName(name string) bool {
+       if len(name) <= 0 {
+               return false
+       }
+
+       _, ok := db.nameIndex[name]
+       if ok {
+               return true
+       }
+
+       return false
+}
+
+func (db *userDB) existCode(code int64) bool {
+       if code <= 0 {
+               return false
+       }
+
+       _, ok := db.codeIndex[code]
+       if ok {
+               return true
+       }
+
+       return false
+}
+
+// User user obj.
+type User struct {
+       ID   string    `json:"id,omitempty"`
+       Code int64     `json:"code,omitempty"`
+       Name string    `json:"name,omitempty"`
+       Age  int32     `json:"age,omitempty"`
+       Time time.Time `json:"time,omitempty"`
+}
+
+// UserProvider the dubbo provider.
+// like: version: 1.0.0 group: test
+type UserProvider struct{}
+
+// CreateUser new user, PX config POST.
+func (u *UserProvider) CreateUser(ctx context.Context, user *User) (*User, 
error) {
+       fmt.Printf("Req CreateUser data: %#v \n", user)
+       if user == nil {
+               return nil, errors.New("not found")
+       }
+       _, ok := cache.GetByName(user.Name)
+       if ok {
+               return nil, errors.New("data is exist")
+       }
+
+       b := cache.Add(user)
+       if b {
+               return user, nil
+       }
+
+       return nil, errors.New("add error")
+}
+
+// GetUserByName query by name, single param, PX config GET.
+func (u *UserProvider) GetUserByName(ctx context.Context, name string) (*User, 
error) {
+       fmt.Printf("Req GetUserByName name: %#v \n", name)
+       r, ok := cache.GetByName(name)
+       if ok {
+               fmt.Printf("Req GetUserByName result: %#v \n", r)
+               return r, nil
+       }
+       return nil, nil
+}
+
+// GetUserByCode query by code, single param, PX config GET.
+func (u *UserProvider) GetUserByCode(ctx context.Context, code int64) (*User, 
error) {
+       fmt.Printf("Req GetUserByCode name: %#v \n", code)
+       r, ok := cache.GetByCode(code)
+       if ok {
+               fmt.Printf("Req GetUserByCode result: %#v \n", r)
+               return r, nil
+       }
+       return nil, nil
+}
+
+// GetUserTimeout query by name, will timeout for pixiu.
+func (u *UserProvider) GetUserTimeout(ctx context.Context, name string) 
(*User, error) {
+       fmt.Printf("Req GetUserByName name: %#v \n", name)
+       // sleep 10s, pixiu config less than 10s.
+       time.Sleep(10 * time.Second)
+       r, ok := cache.GetByName(name)
+       if ok {
+               fmt.Printf("Req GetUserByName result: %#v \n", r)
+               return r, nil
+       }
+       return nil, nil
+}
+
+// GetUserByNameAndAge query by name and age, two params, PX config GET.
+func (u *UserProvider) GetUserByNameAndAge(ctx context.Context, name string, 
age int32) (*User, error) {
+       fmt.Printf("Req GetUserByNameAndAge name: %s, age: %d \n", name, age)
+       r, ok := cache.GetByName(name)
+       if ok && r.Age == age {
+               fmt.Printf("Req GetUserByNameAndAge result: %#v \n", r)
+               return r, nil
+       }
+       return r, nil
+}
+
+// UpdateUser update by user struct, my be another struct, PX config POST or 
PUT.
+func (u *UserProvider) UpdateUser(ctx context.Context, user *User) (bool, 
error) {
+       fmt.Printf("Req UpdateUser data: %#v \n", user)
+       r, ok := cache.GetByName(user.Name)
+       if ok {
+               if user.ID != "" {
+                       r.ID = user.ID
+               }
+               if user.Age >= 0 {
+                       r.Age = user.Age
+               }
+               return true, nil
+       }
+       return false, errors.New("not found")
+}
+
+// UpdateUserByName update by user struct, my be another struct, PX config 
POST or PUT.
+func (u *UserProvider) UpdateUserByName(ctx context.Context, name string, user 
*User) (bool, error) {
+       fmt.Printf("Req UpdateUserByName data: %#v \n", user)
+       r, ok := cache.GetByName(name)
+       if ok {
+               if user.ID != "" {
+                       r.ID = user.ID
+               }
+               if user.Age >= 0 {
+                       r.Age = user.Age
+               }
+               return true, nil
+       }
+       return false, errors.New("not found")
+}
+
+// nolint
+func (u *UserProvider) Reference() string {
+       return "UserProvider"
+}
+
+// nolint
+func (u User) JavaClassName() string {
+       return "com.dubbogo.pixiu.User"
+}
diff --git a/samples/dubbogo/simple/direct/server/profiles/dev/log.yml 
b/samples/dubbogo/simple/direct/server/profiles/dev/log.yml
new file mode 100644
index 00000000..9330cda1
--- /dev/null
+++ b/samples/dubbogo/simple/direct/server/profiles/dev/log.yml
@@ -0,0 +1,45 @@
+#
+# 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.
+#
+level: "debug"
+development: true
+disableCaller: false
+disableStacktrace: false
+sampling:
+encoding: "console"
+
+# encoder
+encoderConfig:
+  messageKey: "message"
+  levelKey: "level"
+  timeKey: "time"
+  nameKey: "logger"
+  callerKey: "caller"
+  stacktraceKey: "stacktrace"
+  lineEnding: ""
+  levelEncoder: "capitalColor"
+  timeEncoder: "iso8601"
+  durationEncoder: "seconds"
+  callerEncoder: "short"
+  nameEncoder: ""
+
+outputPaths:
+  - "stderr"
+errorOutputPaths:
+  - "stderr"
+initialFields:
diff --git a/samples/dubbogo/simple/direct/server/profiles/dev/server.yml 
b/samples/dubbogo/simple/direct/server/profiles/dev/server.yml
new file mode 100644
index 00000000..c93feb2f
--- /dev/null
+++ b/samples/dubbogo/simple/direct/server/profiles/dev/server.yml
@@ -0,0 +1,39 @@
+#
+# 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.
+#
+# dubbo server yaml configure file
+# application config
+dubbo:
+  registries:
+    zk:
+      protocol: zookeeper
+      timeout: 3s
+      address: 127.0.0.1:2181
+  protocols:
+    dubbo:
+      name: dubbo
+      port: 20000
+  provider:
+    registry-ids: zk
+    services:
+      UserProvider:
+        group: test
+        version: 1.0.0
+        cluster: test_dubbo
+        serialization: hessian2
+        interface: com.dubbogo.pixiu.UserService
\ No newline at end of file
diff --git a/samples/dubbogo/simple/direct/test/pixiu_test.go 
b/samples/dubbogo/simple/direct/test/pixiu_test.go
new file mode 100644
index 00000000..2301613d
--- /dev/null
+++ b/samples/dubbogo/simple/direct/test/pixiu_test.go
@@ -0,0 +1,88 @@
+/*
+ * 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 test
+
+import (
+       "io/ioutil"
+       "net/http"
+       "strings"
+       "testing"
+       "time"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)
+
+func TestPost1(t *testing.T) {
+       url := 
"http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/GetUserByName";
+       data := "{\"types\":\"string\",\"values\":\"tc\"}"
+       client := &http.Client{Timeout: 5 * time.Second}
+       req, err := http.NewRequest("POST", url, strings.NewReader(data))
+       req.Header.Set("x-dubbo-http1.1-dubbo-version", "1.0.0")
+       req.Header.Set("x-dubbo-service-protocol", "dubbo")
+       req.Header.Set("x-dubbo-service-version", "1.0.0")
+       req.Header.Set("x-dubbo-service-group", "test")
+
+       assert.NoError(t, err)
+       req.Header.Add("Content-Type", "application/json")
+       resp, err := client.Do(req)
+       assert.NoError(t, err)
+       assert.NotNil(t, resp)
+       assert.Equal(t, 200, resp.StatusCode)
+       s, _ := ioutil.ReadAll(resp.Body)
+       assert.True(t, strings.Contains(string(s), "0001"))
+}
+
+func TestPost2(t *testing.T) {
+       url := 
"http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/UpdateUserByName";
+       data := 
"{\"types\":\"string,object\",\"values\":[\"tc\",{\"id\":\"0001\",\"code\":1,\"name\":\"tc\",\"age\":15}]}"
+       client := &http.Client{Timeout: 5 * time.Second}
+       req, err := http.NewRequest("POST", url, strings.NewReader(data))
+       req.Header.Set("x-dubbo-http1.1-dubbo-version", "1.0.0")
+       req.Header.Set("x-dubbo-service-protocol", "dubbo")
+       req.Header.Set("x-dubbo-service-version", "1.0.0")
+       req.Header.Set("x-dubbo-service-group", "test")
+       assert.NoError(t, err)
+       req.Header.Add("Content-Type", "application/json")
+       resp, err := client.Do(req)
+       assert.NoError(t, err)
+       assert.NotNil(t, resp)
+       assert.Equal(t, 200, resp.StatusCode)
+       s, _ := ioutil.ReadAll(resp.Body)
+       assert.Equal(t, "true", string(s))
+}
+
+func TestPost3(t *testing.T) {
+       url := 
"http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/GetUserByCode";
+       data := "{\"types\":\"int\",\"values\":1}"
+       client := &http.Client{Timeout: 5 * time.Second}
+       req, err := http.NewRequest("POST", url, strings.NewReader(data))
+       req.Header.Set("x-dubbo-http1.1-dubbo-version", "1.0.0")
+       req.Header.Set("x-dubbo-service-protocol", "dubbo")
+       req.Header.Set("x-dubbo-service-version", "1.0.0")
+       req.Header.Set("x-dubbo-service-group", "test")
+       assert.NoError(t, err)
+       req.Header.Add("Content-Type", "application/json")
+       resp, err := client.Do(req)
+       assert.NoError(t, err)
+       assert.NotNil(t, resp)
+       assert.Equal(t, 200, resp.StatusCode)
+       s, _ := ioutil.ReadAll(resp.Body)
+       assert.True(t, strings.Contains(string(s), "0001"))
+}

Reply via email to