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

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


The following commit(s) were added to refs/heads/main by this push:
     new 778c1bd1b feat:support dubbo.load() (#2523)
778c1bd1b is described below

commit 778c1bd1b58a6167c2f99d6b66c8a04a7b373ac5
Author: finalt <[email protected]>
AuthorDate: Wed Nov 29 21:39:27 2023 +0800

    feat:support dubbo.load() (#2523)
    
    * add dubbo.yml
    
    * add lock
    
    * modify logic
    
    * add log
    
    * modify import
---
 client/client.go                                   |  5 ++
 dubbo.go                                           | 90 ++++++++++++++++++++++
 loader.go                                          |  4 +-
 .../internal/client/cmd_with_yml/cmd/main.go       | 38 +++++++++
 .../internal/client/cmd_with_yml/conf/dubbogo.yml  |  7 ++
 .../proto/triple_gen/greettriple/greet.triple.go   | 12 ++-
 .../internal/server/cmd_with_yml/cmd/main.go       | 33 ++++++++
 .../internal/server/cmd_with_yml/conf/dubbogo.yml  | 10 +++
 server/options.go                                  |  6 ++
 server/server.go                                   |  1 +
 10 files changed, 203 insertions(+), 3 deletions(-)

diff --git a/client/client.go b/client/client.go
index a3b540e94..b905c64a1 100644
--- a/client/client.go
+++ b/client/client.go
@@ -48,6 +48,11 @@ type ClientInfo struct {
        Meta             map[string]interface{}
 }
 
+type ClientDefinition struct {
+       Svc  interface{}
+       Info *ClientInfo
+}
+
 func (cli *Client) call(ctx context.Context, paramsRawVals []interface{}, 
interfaceName, methodName, callType string, opts ...CallOption) 
(protocol.Result, error) {
        // get a default CallOptions
        // apply CallOption
diff --git a/dubbo.go b/dubbo.go
index 155a17bd6..2e6c2e198 100644
--- a/dubbo.go
+++ b/dubbo.go
@@ -18,14 +18,28 @@
 package dubbo
 
 import (
+       "sync"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
        "github.com/pkg/errors"
 )
 
 import (
        "dubbo.apache.org/dubbo-go/v3/client"
+       "dubbo.apache.org/dubbo-go/v3/common"
        "dubbo.apache.org/dubbo-go/v3/server"
 )
 
+var (
+       consumerServices = map[string]*client.ClientDefinition{}
+       conLock          sync.RWMutex
+       providerServices = map[string]*server.ServiceDefinition{}
+       proLock          sync.RWMutex
+       startOnce        sync.Once
+)
+
 // Instance is the highest layer conception that user could touch. It is 
mapped from RootConfig.
 // When users want to inject global configurations and configure common 
modules for client layer
 // and server layer, user-side code would be like this:
@@ -156,3 +170,79 @@ func (ins *Instance) NewServer(opts 
...server.ServerOption) (*server.Server, err
        }
        return srv, nil
 }
+
+func (ins *Instance) start() (err error) {
+       startOnce.Do(func() {
+               if err = ins.loadConsumer(); err != nil {
+                       return
+               }
+               if err = ins.loadProvider(); err != nil {
+                       return
+               }
+       })
+       return err
+}
+
+// loadProvider loads the service provider.
+func (ins *Instance) loadProvider() error {
+       var srvOpts []server.ServerOption
+       if ins.insOpts.Provider != nil {
+               srvOpts = append(srvOpts, 
server.SetServerProvider(ins.insOpts.Provider))
+       }
+       srv, err := ins.NewServer(srvOpts...)
+       if err != nil {
+               return err
+       }
+       // register services
+       proLock.RLock()
+       defer proLock.RUnlock()
+       for _, definition := range providerServices {
+               if err = srv.Register(definition.Handler, definition.Info, 
definition.Opts...); err != nil {
+                       return err
+               }
+       }
+       go func() {
+               if err = srv.Serve(); err != nil {
+                       logger.Fatalf("Failed to start server, err: %v", err)
+               }
+       }()
+       return err
+}
+
+// loadConsumer loads the service consumer.
+func (ins *Instance) loadConsumer() error {
+       cli, err := ins.NewClient()
+       if err != nil {
+               return err
+       }
+       // refer services
+       conLock.RLock()
+       defer conLock.RUnlock()
+       for _, definition := range consumerServices {
+               if _, _, err = cli.Init(definition.Info); err != nil {
+                       return err
+               }
+               definition.Info.ClientInjectFunc(definition.Svc, cli)
+       }
+       return err
+}
+
+// SetConsumerServiceWithInfo sets the consumer service with the client 
information.
+func SetConsumerServiceWithInfo(svc common.RPCService, info 
*client.ClientInfo) {
+       conLock.Lock()
+       defer conLock.Unlock()
+       consumerServices[info.InterfaceName] = &client.ClientDefinition{
+               Svc:  svc,
+               Info: info,
+       }
+}
+
+// SetProviderServiceWithInfo sets the provider service with the server 
information.
+func SetProviderServiceWithInfo(svc common.RPCService, info 
*server.ServiceInfo) {
+       proLock.Lock()
+       defer proLock.Unlock()
+       providerServices[info.InterfaceName] = &server.ServiceDefinition{
+               Handler: svc,
+               Info:    info,
+       }
+}
diff --git a/loader.go b/loader.go
index 5679c25e1..43c005303 100644
--- a/loader.go
+++ b/loader.go
@@ -67,8 +67,8 @@ func Load(opts ...LoaderConfOption) error {
                return err
        }
 
-       // todo(DMwangnima): use independent Consumer and Provider logic
-       return nil
+       instance := &Instance{insOpts: instanceOptions}
+       return instance.start()
 }
 
 type loaderConf struct {
diff --git a/protocol/triple/internal/client/cmd_with_yml/cmd/main.go 
b/protocol/triple/internal/client/cmd_with_yml/cmd/main.go
new file mode 100644
index 000000000..55ed88451
--- /dev/null
+++ b/protocol/triple/internal/client/cmd_with_yml/cmd/main.go
@@ -0,0 +1,38 @@
+/*
+ * 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 (
+       "dubbo.apache.org/dubbo-go/v3"
+       _ "dubbo.apache.org/dubbo-go/v3/imports"
+       "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/client/common"
+       
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/triple_gen/greettriple"
+)
+
+var svc = &greettriple.GreetServiceImpl{}
+
+func init() {
+       greettriple.SetConsumerService(svc)
+}
+
+func main() {
+       if err := dubbo.Load(); err != nil {
+               panic(err)
+       }
+       common.TestClient(svc)
+}
diff --git a/protocol/triple/internal/client/cmd_with_yml/conf/dubbogo.yml 
b/protocol/triple/internal/client/cmd_with_yml/conf/dubbogo.yml
new file mode 100644
index 000000000..7b634aa31
--- /dev/null
+++ b/protocol/triple/internal/client/cmd_with_yml/conf/dubbogo.yml
@@ -0,0 +1,7 @@
+# dubbo client yaml configure file
+dubbo:
+  registries:
+    demoZK:
+      protocol: zookeeper
+      timeout: 3s
+      address: 127.0.0.1:2181
\ No newline at end of file
diff --git 
a/protocol/triple/internal/proto/triple_gen/greettriple/greet.triple.go 
b/protocol/triple/internal/proto/triple_gen/greettriple/greet.triple.go
index a88f9a4b3..45d3275ab 100644
--- a/protocol/triple/internal/proto/triple_gen/greettriple/greet.triple.go
+++ b/protocol/triple/internal/proto/triple_gen/greettriple/greet.triple.go
@@ -27,7 +27,9 @@ import (
 )
 
 import (
+       "dubbo.apache.org/dubbo-go/v3"
        client "dubbo.apache.org/dubbo-go/v3/client"
+       "dubbo.apache.org/dubbo-go/v3/common"
        "dubbo.apache.org/dubbo-go/v3/common/constant"
        "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto"
        triple_protocol 
"dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
@@ -100,6 +102,10 @@ func NewGreetService(cli *client.Client, opts 
...client.ReferenceOption) (GreetS
        }, nil
 }
 
+func SetConsumerService(srv common.RPCService) {
+       dubbo.SetConsumerServiceWithInfo(srv, &GreetService_ClientInfo)
+}
+
 // GreetServiceImpl implements GreetService.
 type GreetServiceImpl struct {
        cli     *client.Client
@@ -245,7 +251,7 @@ var GreetService_ClientInfo = client.ClientInfo{
        InterfaceName: "greet.GreetService",
        MethodNames:   []string{"Greet", "GreetStream", "GreetClientStream", 
"GreetServerStream"},
        ClientInjectFunc: func(dubboCliRaw interface{}, cli *client.Client) {
-               dubboCli := dubboCliRaw.(GreetServiceImpl)
+               dubboCli := dubboCliRaw.(*GreetServiceImpl)
                dubboCli.cli = cli
        },
 }
@@ -258,6 +264,10 @@ type GreetServiceHandler interface {
        GreetServerStream(context.Context, *proto.GreetServerStreamRequest, 
GreetService_GreetServerStreamServer) error
 }
 
+func SetProviderService(srv common.RPCService) {
+       dubbo.SetProviderServiceWithInfo(srv, &GreetService_ServiceInfo)
+}
+
 func RegisterGreetServiceHandler(srv *server.Server, hdlr GreetServiceHandler, 
opts ...server.ServiceOption) error {
        return srv.Register(hdlr, &GreetService_ServiceInfo, opts...)
 }
diff --git a/protocol/triple/internal/server/cmd_with_yml/cmd/main.go 
b/protocol/triple/internal/server/cmd_with_yml/cmd/main.go
new file mode 100644
index 000000000..4847d32a4
--- /dev/null
+++ b/protocol/triple/internal/server/cmd_with_yml/cmd/main.go
@@ -0,0 +1,33 @@
+/*
+ * 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 (
+       "dubbo.apache.org/dubbo-go/v3"
+       _ "dubbo.apache.org/dubbo-go/v3/imports"
+       
"dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/triple_gen/greettriple"
+       "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/server/api"
+)
+
+func main() {
+       greettriple.SetProviderService(&api.GreetTripleServer{})
+       if err := dubbo.Load(); err != nil {
+               panic(err)
+       }
+       select {}
+}
diff --git a/protocol/triple/internal/server/cmd_with_yml/conf/dubbogo.yml 
b/protocol/triple/internal/server/cmd_with_yml/conf/dubbogo.yml
new file mode 100644
index 000000000..af32f36b6
--- /dev/null
+++ b/protocol/triple/internal/server/cmd_with_yml/conf/dubbogo.yml
@@ -0,0 +1,10 @@
+# dubbo server yaml configure file
+dubbo:
+  registries:
+    demoZK:
+      protocol: zookeeper
+      address: 127.0.0.1:2181
+  protocols:
+    tripleProtocol:
+      name: tri
+      port: 20000
\ No newline at end of file
diff --git a/server/options.go b/server/options.go
index 1798b5b0e..ca84f4c46 100644
--- a/server/options.go
+++ b/server/options.go
@@ -440,6 +440,12 @@ func SetServerOtel(otel *global.OtelConfig) ServerOption {
        }
 }
 
+func SetServerProvider(provider *global.ProviderConfig) ServerOption {
+       return func(opts *ServerOptions) {
+               opts.Provider = provider
+       }
+}
+
 type ServiceOptions struct {
        Application *global.ApplicationConfig
        Provider    *global.ProviderConfig
diff --git a/server/server.go b/server/server.go
index 711aca5f6..2cd0150b0 100644
--- a/server/server.go
+++ b/server/server.go
@@ -35,6 +35,7 @@ import (
        registry_exposed "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp"
 )
 
+// proServices are for internal services
 var proServices = map[string]*ServiceDefinition{}
 
 type Server struct {

Reply via email to