This is an automated email from the ASF dual-hosted git repository.
xuetaoli pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new 2fef526e9 refactor: remove config package from dubbogo-cli sample
generator (#3218)
2fef526e9 is described below
commit 2fef526e9bec70c37f653a1b0fb38b4c022a148d
Author: CAICAII <[email protected]>
AuthorDate: Thu Feb 19 09:59:20 2026 +0800
refactor: remove config package from dubbogo-cli sample generator (#3218)
* refactor: remove config package from dubbogo-cli sample generator
- Migrate sample generator from config-based to programmatic API
- Update client template to use client.NewClient()
- Update server template to use server.NewServer()
- Remove conf/dubbogo.yaml configuration files
- Disable config file generation (comment out init() in gen_c_conf.go and
gen_s_conf.go)
- Fix import order to match project standards
- Handle license suffix in constants.go using strings.TrimSuffix
- Update README.md and README_CN.md documentation
* fix: format constants.go imports to use multi-line style
* fix: move licenseForTemplates to var to allow function call
* fix: add blank line between license and generated code in api files
* fix: add blank line between license and proto file content
* fix: add blank line between license and generated code
Fix test failures by adding blank lines between the license header
and the generated code content in all template files:
- api.go: Add blank line at beginning of apiFile constant
- api_tripe.go: Add blank line at beginning of apiTripleFile constant
- proto.go: Add blank line at beginning of protoFile constant
- gen_client.go: Add blank line at beginning of clientCode constant and
add newline separator in concatenation
- gen_server.go: Add blank line at beginning of serverCode constant and
add newline separator in concatenation
All tests now pass with these changes.
---
tools/dubbogo-cli/README.md | 12 ++++------
tools/dubbogo-cli/README_CN.md | 12 ++++------
.../template/newDemo/go-client/cmd/client.go | 18 +++++++++------
.../template/newDemo/go-client/conf/dubbogo.yaml | 22 ------------------
.../template/newDemo/go-server/cmd/server.go | 22 ++++++++++++++----
.../template/newDemo/go-server/conf/dubbogo.yaml | 24 -------------------
tools/dubbogo-cli/generator/sample/api.go | 5 ++--
tools/dubbogo-cli/generator/sample/api_tripe.go | 5 ++--
tools/dubbogo-cli/generator/sample/constants.go | 10 +++++---
tools/dubbogo-cli/generator/sample/gen_c_conf.go | 16 +++++++------
tools/dubbogo-cli/generator/sample/gen_client.go | 23 ++++++++++--------
tools/dubbogo-cli/generator/sample/gen_s_conf.go | 16 +++++++------
tools/dubbogo-cli/generator/sample/gen_server.go | 27 ++++++++++++++++------
tools/dubbogo-cli/generator/sample/proto.go | 5 ++--
14 files changed, 104 insertions(+), 113 deletions(-)
diff --git a/tools/dubbogo-cli/README.md b/tools/dubbogo-cli/README.md
index 8d27d0ffc..4a12f1691 100644
--- a/tools/dubbogo-cli/README.md
+++ b/tools/dubbogo-cli/README.md
@@ -105,15 +105,11 @@ The Demo uses the direct connection mode, without relying
on the registration ce
│ ├── samples_api.proto
│ └── samples_api_triple.pb.go
├── go-client
-│ ├── cmd
-│ │ └── client.go
-│ └── conf
-│ └── dubbogo.yaml
+│ └── cmd
+│ └── client.go
├── go-server
-│ ├── cmd
-│ │ └── server.go
-│ └── conf
-│ └── dubbogo.yaml
+│ └── cmd
+│ └── server.go
└── go.mod
```
diff --git a/tools/dubbogo-cli/README_CN.md b/tools/dubbogo-cli/README_CN.md
index 241c525c3..cbeffbf23 100644
--- a/tools/dubbogo-cli/README_CN.md
+++ b/tools/dubbogo-cli/README_CN.md
@@ -109,15 +109,11 @@ dubbogo-cli newDemo .
│ ├── samples_api.proto
│ └── samples_api_triple.pb.go
├── go-client
-│ ├── cmd
-│ │ └── client.go
-│ └── conf
-│ └── dubbogo.yaml
+│ └── cmd
+│ └── client.go
├── go-server
-│ ├── cmd
-│ │ └── server.go
-│ └── conf
-│ └── dubbogo.yaml
+│ └── cmd
+│ └── server.go
└── go.mod
```
diff --git
a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/cmd/client.go
b/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/cmd/client.go
index 8a3b527df..9aa288b83 100644
--- a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/cmd/client.go
+++ b/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/cmd/client.go
@@ -26,17 +26,21 @@ import (
)
import (
+ "dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/common/logger"
- "dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
)
-var grpcGreeterImpl = new(api.GreeterClientImpl)
-
-// export DUBBO_GO_CONFIG_PATH=
PATH_TO_SAMPLES/helloworld/go-client/conf/dubbogo.yaml
func main() {
- config.SetConsumerService(grpcGreeterImpl)
- if err := config.Load(); err != nil {
+ cli, err := client.NewClient(
+ client.WithClientURL("tri://localhost:20000"),
+ )
+ if err != nil {
+ panic(err)
+ }
+
+ greeterClient, err := api.NewGreeterClient(cli)
+ if err != nil {
panic(err)
}
@@ -44,7 +48,7 @@ func main() {
req := &api.HelloRequest{
Name: "laurence",
}
- reply, err := grpcGreeterImpl.SayHello(context.Background(), req)
+ reply, err := greeterClient.SayHello(context.Background(), req)
if err != nil {
logger.Error(err)
}
diff --git
a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/conf/dubbogo.yaml
b/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/conf/dubbogo.yaml
deleted file mode 100644
index 4b78741ac..000000000
---
a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/conf/dubbogo.yaml
+++ /dev/null
@@ -1,22 +0,0 @@
-# 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:
- consumer:
- references:
- GreeterClientImpl:
- protocol: tri
- url: "tri://localhost:20000"
- interface: "" # read from pb
\ No newline at end of file
diff --git
a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/cmd/server.go
b/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/cmd/server.go
index 3d0c3941e..cbfff282a 100644
--- a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/cmd/server.go
+++ b/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/cmd/server.go
@@ -27,8 +27,9 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common/logger"
- "dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
+ "dubbo.apache.org/dubbo-go/v3/protocol"
+ "dubbo.apache.org/dubbo-go/v3/server"
)
type GreeterProvider struct {
@@ -40,11 +41,22 @@ func (s *GreeterProvider) SayHello(ctx context.Context, in
*api.HelloRequest) (*
return &api.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil
}
-// export DUBBO_GO_CONFIG_PATH=
PATH_TO_SAMPLES/helloworld/go-server/conf/dubbogo.yaml
func main() {
- config.SetProviderService(&GreeterProvider{})
- if err := config.Load(); err != nil {
+ srv, err := server.NewServer(
+ server.WithServerProtocol(
+ protocol.WithPort(20000),
+ protocol.WithTriple(),
+ ),
+ )
+ if err != nil {
+ panic(err)
+ }
+
+ if err := api.RegisterGreeterServer(srv, &GreeterProvider{}); err !=
nil {
+ panic(err)
+ }
+
+ if err := srv.Serve(); err != nil {
panic(err)
}
- select {}
}
diff --git
a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/conf/dubbogo.yaml
b/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/conf/dubbogo.yaml
deleted file mode 100644
index ad005fa00..000000000
---
a/tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/conf/dubbogo.yaml
+++ /dev/null
@@ -1,24 +0,0 @@
-# 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:
- protocols:
- triple:
- name: tri
- port: 20000
- provider:
- services:
- GreeterProvider:
- interface: "" # read from pb
\ No newline at end of file
diff --git a/tools/dubbogo-cli/generator/sample/api.go
b/tools/dubbogo-cli/generator/sample/api.go
index 5944522af..c7ef01a46 100644
--- a/tools/dubbogo-cli/generator/sample/api.go
+++ b/tools/dubbogo-cli/generator/sample/api.go
@@ -18,7 +18,8 @@
package sample
const (
- apiFile = `// Code generated by protoc-gen-go. DO NOT EDIT.
+ apiFile = `
+// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.14.0
@@ -263,6 +264,6 @@ func init() {
fileMap["apiFile"] = &fileGenerator{
path: "./api",
file: "samples_api.pb.go",
- context: license + apiFile,
+ context: license + "\n" + apiFile,
}
}
diff --git a/tools/dubbogo-cli/generator/sample/api_tripe.go
b/tools/dubbogo-cli/generator/sample/api_tripe.go
index d2caa239b..802fe7414 100644
--- a/tools/dubbogo-cli/generator/sample/api_tripe.go
+++ b/tools/dubbogo-cli/generator/sample/api_tripe.go
@@ -18,7 +18,8 @@
package sample
const (
- apiTripleFile = `// Code generated by protoc-gen-go-triple. DO NOT EDIT.
+ apiTripleFile = `
+// Code generated by protoc-gen-go-triple. DO NOT EDIT.
// versions:
// - protoc-gen-go-triple v1.0.8
// - protoc v3.14.0
@@ -269,6 +270,6 @@ func init() {
fileMap["apiTripleFile"] = &fileGenerator{
path: "./api",
file: "samples_api_triple.pb.go",
- context: license + apiTripleFile,
+ context: license + "\n" + apiTripleFile,
}
}
diff --git a/tools/dubbogo-cli/generator/sample/constants.go
b/tools/dubbogo-cli/generator/sample/constants.go
index 5277cd655..61f837480 100644
--- a/tools/dubbogo-cli/generator/sample/constants.go
+++ b/tools/dubbogo-cli/generator/sample/constants.go
@@ -17,6 +17,10 @@
package sample
+import (
+ "strings"
+)
+
const (
license = `/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -33,7 +37,7 @@ const (
* 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.
- */
-
-`
+ */`
)
+
+var licenseForTemplates = strings.TrimSuffix(license, " \n")
diff --git a/tools/dubbogo-cli/generator/sample/gen_c_conf.go
b/tools/dubbogo-cli/generator/sample/gen_c_conf.go
index 1b34720ea..d6d34926d 100644
--- a/tools/dubbogo-cli/generator/sample/gen_c_conf.go
+++ b/tools/dubbogo-cli/generator/sample/gen_c_conf.go
@@ -42,10 +42,12 @@ dubbo:
interface: "" # read from pb`
)
-func init() {
- fileMap["clientConfigFile"] = &fileGenerator{
- path: "./go-client/conf",
- file: "dubbogo.yaml",
- context: clientConfigFile,
- }
-}
+// Config files are no longer needed in the new config-free pattern
+// The client now uses programmatic configuration via client.NewClient()
+// func init() {
+// fileMap["clientConfigFile"] = &fileGenerator{
+// path: "./go-client/conf",
+// file: "dubbogo.yaml",
+// context: clientConfigFile,
+// }
+// }
diff --git a/tools/dubbogo-cli/generator/sample/gen_client.go
b/tools/dubbogo-cli/generator/sample/gen_client.go
index 0ff47d1b1..c828f9d06 100644
--- a/tools/dubbogo-cli/generator/sample/gen_client.go
+++ b/tools/dubbogo-cli/generator/sample/gen_client.go
@@ -18,7 +18,8 @@
package sample
const (
- clientCode = `package main
+ clientCode = `
+package main
import (
"context"
@@ -29,17 +30,21 @@ import (
)
import (
+ "dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/common/logger"
- "dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
)
-var grpcGreeterImpl = new(api.GreeterClientImpl)
-
-// export DUBBO_GO_CONFIG_PATH=
PATH_TO_SAMPLES/helloworld/go-client/conf/dubbogo.yaml
func main() {
- config.SetConsumerService(grpcGreeterImpl)
- if err := config.Load(); err != nil {
+ cli, err := client.NewClient(
+ client.WithClientURL("tri://localhost:20000"),
+ )
+ if err != nil {
+ panic(err)
+ }
+
+ greeterClient, err := api.NewGreeterClient(cli)
+ if err != nil {
panic(err)
}
@@ -47,7 +52,7 @@ func main() {
req := &api.HelloRequest{
Name: "laurence",
}
- reply, err := grpcGreeterImpl.SayHello(context.Background(), req)
+ reply, err := greeterClient.SayHello(context.Background(), req)
if err != nil {
logger.Error(err)
}
@@ -60,6 +65,6 @@ func init() {
fileMap["clientGenerator"] = &fileGenerator{
path: "./go-client/cmd",
file: "client.go",
- context: license + clientCode,
+ context: licenseForTemplates + "\n" + clientCode,
}
}
diff --git a/tools/dubbogo-cli/generator/sample/gen_s_conf.go
b/tools/dubbogo-cli/generator/sample/gen_s_conf.go
index 0ced9ffbe..21a950944 100644
--- a/tools/dubbogo-cli/generator/sample/gen_s_conf.go
+++ b/tools/dubbogo-cli/generator/sample/gen_s_conf.go
@@ -44,10 +44,12 @@ dubbo:
interface: "" # read from pb`
)
-func init() {
- fileMap["srvConfGenerator"] = &fileGenerator{
- path: "./go-server/conf",
- file: "dubbogo.yaml",
- context: serverConfigFile,
- }
-}
+// Config files are no longer needed in the new config-free pattern
+// The server now uses programmatic configuration via server.NewServer()
+// func init() {
+// fileMap["srvConfGenerator"] = &fileGenerator{
+// path: "./go-server/conf",
+// file: "dubbogo.yaml",
+// context: serverConfigFile,
+// }
+// }
diff --git a/tools/dubbogo-cli/generator/sample/gen_server.go
b/tools/dubbogo-cli/generator/sample/gen_server.go
index 42754b779..beb9c5735 100644
--- a/tools/dubbogo-cli/generator/sample/gen_server.go
+++ b/tools/dubbogo-cli/generator/sample/gen_server.go
@@ -18,7 +18,8 @@
package sample
const (
- serverCode = `package main
+ serverCode = `
+package main
import (
"context"
@@ -30,7 +31,8 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common/logger"
- "dubbo.apache.org/dubbo-go/v3/config"
+ "dubbo.apache.org/dubbo-go/v3/server"
+ "dubbo.apache.org/dubbo-go/v3/protocol"
_ "dubbo.apache.org/dubbo-go/v3/imports"
)
@@ -43,13 +45,24 @@ func (s *GreeterProvider) SayHello(ctx context.Context, in
*api.HelloRequest) (*
return &api.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil
}
-// export DUBBO_GO_CONFIG_PATH=
PATH_TO_SAMPLES/helloworld/go-server/conf/dubbogo.yaml
func main() {
- config.SetProviderService(&GreeterProvider{})
- if err := config.Load(); err != nil {
+ srv, err := server.NewServer(
+ server.WithServerProtocol(
+ protocol.WithPort(20000),
+ protocol.WithTriple(),
+ ),
+ )
+ if err != nil {
+ panic(err)
+ }
+
+ if err := api.RegisterGreeterServer(srv, &GreeterProvider{}); err !=
nil {
+ panic(err)
+ }
+
+ if err := srv.Serve(); err != nil {
panic(err)
}
- select {}
}
`
)
@@ -58,7 +71,7 @@ func init() {
fileMap["srvGenerator"] = &fileGenerator{
path: "./go-server/cmd",
file: "server.go",
- context: license + serverCode,
+ context: licenseForTemplates + "\n" + serverCode,
}
//var str = "`" + `protobuf:"bytes,1,opt,name=name,proto3"
json:"name,omitempty"` + "`"
}
diff --git a/tools/dubbogo-cli/generator/sample/proto.go
b/tools/dubbogo-cli/generator/sample/proto.go
index 9a86dd6c5..353e1330e 100644
--- a/tools/dubbogo-cli/generator/sample/proto.go
+++ b/tools/dubbogo-cli/generator/sample/proto.go
@@ -18,7 +18,8 @@
package sample
const (
- protoFile = `syntax = "proto3";
+ protoFile = `
+syntax = "proto3";
package api;
option go_package = "./;api";
@@ -48,6 +49,6 @@ func init() {
fileMap[protoFile] = &fileGenerator{
path: "./api",
file: "samples_api.proto",
- context: license + protoFile,
+ context: license + "\n" + protoFile,
}
}