diff --git a/README.md b/README.md index 69ffaa147..cc7732e45 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ A collection of runnable Dubbo-go examples covering configuration, registries, o * `compatibility/proxyless`: Proxyless service mesh example for deploying Dubbo-go services on Kubernetes. * `compatibility/polaris`: Dubbo-go integrate with polaris samples. * `compatibility/polaris/limit`: Quickly experience Polaris' service current limiting capabilities in dubbogo - * `compatibility/polaris/registry`: Quickly experience Polaris' service registration and service discovery capabilities in dubbogo * `compatibility/polaris/router`: Quickly experience Polaris' service routing capabilities in dubbogo * `compatibility/rpc`: Dubbo protocol communication examples. * `compatibility/rpc/dubbo`: Dubbo-go 3.0 RPC example between Java and Go. diff --git a/README_CN.md b/README_CN.md index 568a71bcc..5bed6c3a8 100644 --- a/README_CN.md +++ b/README_CN.md @@ -55,7 +55,6 @@ * `compatibility/proxyless`:无 Sidecar 的服务网格示例,展示在 Kubernetes 上的部署方式。 * `compatibility/polaris`: Dubbo-go 与 polaris 集成示例. * `compatibility/polaris/limit`: 在 dubbogo 中快速体验北极星的服务限流能力 - * `compatibility/polaris/registry`: 在 dubbogo 中快速体验北极星的服务注册以及服务发现能力 * `compatibility/polaris/router`: 在 dubbogo 中快速体验北极星的服务路由能力 * `compatibility/rpc`:Dubbo 协议通信示例。 * `compatibility/rpc/dubbo`:Dubbo-go 3.0 下 Java 与 Go 的 RPC 示例。 diff --git a/compatibility/polaris/registry/README-zh.md b/compatibility/polaris/registry/README-zh.md deleted file mode 100644 index a13c2aab6..000000000 --- a/compatibility/polaris/registry/README-zh.md +++ /dev/null @@ -1,62 +0,0 @@ -# Dubbo Go & Polaris Registry Example - -English | [中文](README-zh.md) - -## 使用服务注册发现功能 - -在 dubbogo 中快速体验北极星的服务注册以及服务发现能力 - -## 北极星服务端安装 - -[北极星服务端单机版本安装文档](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E5%8D%95%E6%9C%BA%E7%89%88%E5%AE%89%E8%A3%85/) - -[北极星服务端集群版本安装文档](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E9%9B%86%E7%BE%A4%E7%89%88%E5%AE%89%E8%A3%85/) - -## 如何使用 - -### dubbogo.yaml 配置文件 - -当前 PolarisMesh 已实现了 dubbogo 的注册发现扩展点,因此你只需要调整你的 dubbogo.yaml 文件中的 registries 配置项,新增 protocol 为 polaris 的注册中心配置即可,可以参考下面的样例。 - -```yaml -dubbo: - registries: - polarisMesh: - protocol: polaris - address: ${北极星服务端IP}:8091 - namespace: ${北极星命名空间信息} - token: ${北极星资源鉴权 token} # 如果北极星服务端开启了针对客户端的鉴权,则需要配置该参数 -``` - -### 运行服务提供者 - -进入 go-server 的 cmd 目录,执行以下命令 - -``` - export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" - go run . -``` - -当看到以下日志时即表示 server 端启动成功 - -```log -INFO dubbo/dubbo_protocol.go:84 [DUBBO Protocol] Export service: -``` - - -### 运行服务调用者 - -进入 go-client 的 cmd 目录,执行以下命令 - - -``` - export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" - go run . -``` - -当看到以下日志时即表示 go-client 成功发现 go-server 并发起了 RPC 调用 - -```log -INFO cmd/main.go:75 response: &{A001 Alex Stocks 18 2022-11-19 12:52:38.092 +0800 CST} -``` - diff --git a/compatibility/polaris/registry/README.md b/compatibility/polaris/registry/README.md deleted file mode 100644 index e6ea563f7..000000000 --- a/compatibility/polaris/registry/README.md +++ /dev/null @@ -1,61 +0,0 @@ -# Dubbo Go & Polaris Registry Example - -English | [中文](README-zh.md) - -## Using the service registration discovery function - -Quickly experience Polaris' service registration and service discovery capabilities in dubbogo - -## Polaris server installation - -[Polaris Server Standalone Version Installation Documentation](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E5%8D%95%E6%9C%BA%E7%89%88%E5%AE%89%E8%A3%85/) - -[Polaris Server Cluster Version Installation Documentation](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E9%9B%86%E7%BE%A4%E7%89%88%E5%AE%89%E8%A3%85/) - -## how to use - -### dubbogo.yaml configuration file - -Currently PolarisMesh has implemented the registration discovery extension point of dubbogo, so you only need to adjust the registries configuration item in your dubbogo.yaml file, and add the registry configuration of polaris as the protocol. You can refer to the following example. - -````yaml -dubbo: - registries: - polarisMesh: - protocol: polaris - address: ${Polaris server IP}:8091 - namespace: ${Polaris namespace information} - token: ${Polaris resource authentication token} # If the Polaris server has enabled authentication for the client, you need to configure this parameter -```` - -### Running the service provider - -Enter the cmd directory of go-server and execute the following command - -```` - export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" - go run . -```` - -When you see the following log, it means that the server side started successfully - -````log -INFO dubbo/dubbo_protocol.go:84 [DUBBO Protocol] Export service: -```` - - -### Run the service caller - -Enter the cmd directory of go-client and execute the following command - - -```` - export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" - go run . -```` - -When you see the following log, it means that go-client successfully discovered go-server and made an RPC call - -````log -INFO cmd/main.go:75 response: &{A001 Alex Stocks 18 2022-11-19 12:52:38.092 +0800 CST} -```` \ No newline at end of file diff --git a/compatibility/polaris/registry/go-client/cmd/main.go b/compatibility/polaris/registry/go-client/cmd/main.go deleted file mode 100644 index e685d8a42..000000000 --- a/compatibility/polaris/registry/go-client/cmd/main.go +++ /dev/null @@ -1,81 +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. - */ - -package main - -import ( - "context" - "os" - "time" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/config" - _ "dubbo.apache.org/dubbo-go/v3/imports" - - hessian "github.com/apache/dubbo-go-hessian2" - - "github.com/dubbogo/gost/log/logger" -) - -type UserProviderWithCustomGroupAndVersion struct { - GetUser func(ctx context.Context, req *User) (rsp *User, err error) -} - -type UserProvider struct { - GetUser func(ctx context.Context, req *User) (rsp *User, err error) -} - -type User struct { - ID string - Name string - Age int32 - Time time.Time -} - -func (u *User) JavaClassName() string { - return "org.apache.dubbo.User" -} - -func main() { - var userProvider = &UserProvider{} - var userProviderWithCustomRegistryGroupAndVersion = &UserProviderWithCustomGroupAndVersion{} - config.SetConsumerService(userProvider) - config.SetConsumerService(userProviderWithCustomRegistryGroupAndVersion) - hessian.RegisterPOJO(&User{}) - err := config.Load() - if err != nil { - panic(err) - } - - logger.Infof("\n\n\nstart to test dubbo") - user, err := userProvider.GetUser(context.TODO(), &User{Name: "Alex001"}) - if err != nil { - logger.Errorf("error: %v\n", err) - os.Exit(1) - return - } - logger.Infof("response result: %v\n", user) - - user, err = userProviderWithCustomRegistryGroupAndVersion.GetUser(context.TODO(), &User{Name: "Alex001"}) - if err != nil { - logger.Errorf("error: %v\n", err) - os.Exit(1) - return - } - logger.Infof("response result: %v\n", user) -} diff --git a/compatibility/polaris/registry/go-client/conf/dubbogo.yml b/compatibility/polaris/registry/go-client/conf/dubbogo.yml deleted file mode 100644 index 068e6dd4c..000000000 --- a/compatibility/polaris/registry/go-client/conf/dubbogo.yml +++ /dev/null @@ -1,24 +0,0 @@ -dubbo: - application: - name: myApp # metadata: application=myApp; name=myApp - module: opensource #metadata: module=opensource - group: myAppGroup # no metadata record - organization: dubbo # metadata: organization=dubbo - owner: laurence # metadata: owner=laurence - version: myversion # metadata: app.version=myversion - environment: pro # metadata: environment=pro - registries: - polarisMesh: - protocol: polaris - address: 127.0.0.1:8091 - namespace: dubbogo - registry-type: interface - consumer: - references: - UserProvider: - protocol: dubbo - interface: org.apache.dubbo.UserProvider.Test - UserProviderWithCustomGroupAndVersion: - protocol: dubbo - interface: org.apache.dubbo.UserProvider.Test2 - version: myInterfaceVersion # dubbo interface version must be same with server \ No newline at end of file diff --git a/compatibility/polaris/registry/go-server/cmd/server.go b/compatibility/polaris/registry/go-server/cmd/server.go deleted file mode 100644 index 559886789..000000000 --- a/compatibility/polaris/registry/go-server/cmd/server.go +++ /dev/null @@ -1,110 +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. - */ - -package main - -import ( - "context" - "fmt" - "os" - "os/signal" - "syscall" - "time" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/config" - _ "dubbo.apache.org/dubbo-go/v3/imports" - - hessian "github.com/apache/dubbo-go-hessian2" - - "github.com/dubbogo/gost/log/logger" -) - -var ( - survivalTimeout = int(3e9) -) - -func init() { - config.SetProviderService(&UserProvider{}) - config.SetProviderService(&UserProviderWithCustomGroupAndVersion{}) - // ------for hessian2------ - hessian.RegisterPOJO(&User{}) -} - -type User struct { - ID string - Name string - Age int32 - Time time.Time -} - -type UserProvider struct { -} - -func (u *UserProvider) GetUser(ctx context.Context, req *User) (*User, error) { - logger.Infof("req:%#v", req) - rsp := User{"A001", "Alex Stocks", 18, time.Now()} - logger.Infof("rsp:%#v", rsp) - return &rsp, nil -} - -func (u *User) JavaClassName() string { - return "org.apache.dubbo.User" -} - -type UserProviderWithCustomGroupAndVersion struct { -} - -func (u *UserProviderWithCustomGroupAndVersion) GetUser(ctx context.Context, req *User) (*User, error) { - logger.Infof("req:%#v", req) - rsp := User{"A001", "Alex Stocks from UserProviderWithCustomGroupAndVersion", 18, time.Now()} - logger.Infof("rsp:%#v", rsp) - return &rsp, nil -} - -// need to setup environment variable "CONF_PROVIDER_FILE_PATH" to "conf/server.yml" before run -func main() { - if err := config.Load(); err != nil { - panic(err) - } - - 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) - 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/compatibility/polaris/registry/go-server/conf/dubbogo.yml b/compatibility/polaris/registry/go-server/conf/dubbogo.yml deleted file mode 100644 index 0c0c7f701..000000000 --- a/compatibility/polaris/registry/go-server/conf/dubbogo.yml +++ /dev/null @@ -1,27 +0,0 @@ -dubbo: - application: - name: myApp # metadata: application=myApp; name=myApp - module: opensource #metadata: module=opensource - group: myAppGroup # no metadata record - organization: dubbo # metadata: organization=dubbo - owner: laurence # metadata: owner=laurence - version: myversion # metadata: app.version=myversion - environment: pro # metadata: environment=pro - registries: - polarisMesh: - protocol: polaris - address: 127.0.0.1:8091 - namespace: dubbogo - registry-type: interface - protocols: - dubbo: - name: dubbo - port: 20000 - provider: - services: - UserProvider: - interface: org.apache.dubbo.UserProvider.Test - UserProviderWithCustomGroupAndVersion: - interface: org.apache.dubbo.UserProvider.Test2 - version: myInterfaceVersion # dubbo interface version must be same with client - group: myInterfaceGroup # dubbo interface group must be same with client \ No newline at end of file diff --git a/integrate_test/compatibility/polaris/registry/tests/integration/main_test.go b/integrate_test/registry/polaris/tests/integration/main_test.go similarity index 55% rename from integrate_test/compatibility/polaris/registry/tests/integration/main_test.go rename to integrate_test/registry/polaris/tests/integration/main_test.go index 5db1c6480..7455ae541 100644 --- a/integrate_test/compatibility/polaris/registry/tests/integration/main_test.go +++ b/integrate_test/registry/polaris/tests/integration/main_test.go @@ -18,48 +18,45 @@ package integration import ( - "context" + "os" "testing" - "time" ) import ( - "dubbo.apache.org/dubbo-go/v3/config" + "dubbo.apache.org/dubbo-go/v3" _ "dubbo.apache.org/dubbo-go/v3/imports" - - hessian "github.com/apache/dubbo-go-hessian2" + "dubbo.apache.org/dubbo-go/v3/registry" ) -type UserProviderWithCustomGroupAndVersion struct { - GetUser func(ctx context.Context, req *User) (rsp *User, err error) -} - -type UserProvider struct { - GetUser func(ctx context.Context, req *User) (rsp *User, err error) -} - -type User struct { - ID string - Name string - Age int32 - Time time.Time -} - -func (u *User) JavaClassName() string { - return "org.apache.dubbo.User" -} +import ( + greet "github.com/apache/dubbo-go-samples/registry/polaris/proto" +) -var userProvider = &UserProvider{} -var userProviderWithCustomRegistryGroupAndVersion = &UserProviderWithCustomGroupAndVersion{} +var greetService greet.GreetService func TestMain(m *testing.M) { + ins, err := dubbo.NewInstance( + dubbo.WithName("dubbo_registry_polaris_client"), + dubbo.WithRegistry( + registry.WithPolaris(), + registry.WithAddress("127.0.0.1:8091"), + registry.WithNamespace("dubbogo"), + registry.WithRegisterInterface(), + ), + ) + if err != nil { + panic(err) + } + // configure the params that only client layer cares + cli, err := ins.NewClient() + if err != nil { + panic(err) + } - config.SetConsumerService(userProvider) - config.SetConsumerService(userProviderWithCustomRegistryGroupAndVersion) - hessian.RegisterPOJO(&User{}) - err := config.Load() + greetService, err = greet.NewGreetService(cli) if err != nil { panic(err) } + os.Exit(m.Run()) } diff --git a/integrate_test/compatibility/polaris/registry/tests/integration/registry_test.go b/integrate_test/registry/polaris/tests/integration/registry_test.go similarity index 68% rename from integrate_test/compatibility/polaris/registry/tests/integration/registry_test.go rename to integrate_test/registry/polaris/tests/integration/registry_test.go index 76251f2c4..eb996de13 100644 --- a/integrate_test/compatibility/polaris/registry/tests/integration/registry_test.go +++ b/integrate_test/registry/polaris/tests/integration/registry_test.go @@ -26,16 +26,20 @@ import ( "github.com/stretchr/testify/assert" ) +import ( + greet "github.com/apache/dubbo-go-samples/registry/polaris/proto" +) + func TestPolarisRegistry(t *testing.T) { - user, err := userProvider.GetUser(context.TODO(), &User{Name: "Alex001"}) - assert.Nil(t, err) - assert.NotNil(t, user) - assert.Equal(t, user.ID, "A001") - assert.Equal(t, user.Name, "Alex Stocks") - assert.Equal(t, user.Age, 18) + req := &greet.GreetRequest{ + Name: "Dubbo", + } + + ctx := context.Background() + + reply, err := greetService.Greet(ctx, req) - user, err = userProviderWithCustomRegistryGroupAndVersion.GetUser(context.TODO(), &User{Name: "Alex001"}) assert.Nil(t, err) - assert.Equal(t, user.Name, "Alex Stocks from UserProviderWithCustomGroupAndVersion") - assert.Equal(t, user.Age, 18) + assert.NotNil(t, reply) + assert.Equal(t, "Dubbo", reply.Greeting) } diff --git a/registry/README.md b/registry/README.md index 4bebd01f1..814d2a41e 100644 --- a/registry/README.md +++ b/registry/README.md @@ -5,5 +5,6 @@ This directory contains a series of examples showing dubbo-go's service discover * [Zookeeper as registry](./zookeeper) * [Nacos as registry](./nacos) * [Etcd as registry](./etcd) +* [Polaris as registry](./polaris) [Service Discovery]: https://dubbo-next.staged.apache.org/zh-cn/overview/mannual/golang-sdk/tutorial/service-discovery \ No newline at end of file diff --git a/registry/polaris/README.md b/registry/polaris/README.md new file mode 100644 index 000000000..da90ec664 --- /dev/null +++ b/registry/polaris/README.md @@ -0,0 +1,22 @@ +# Polaris as registry + +[English](README.md) | [中文](README_zh.md) + +This example shows dubbo-go's service discovery feature with Polaris as registry. + +## How to run + +### Start Polaris server +Follow this instruction to [install and start Polaris server](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E5%8D%95%E6%9C%BA%E7%89%88%E5%AE%89%E8%A3%85/). + +### Run server +```shell +$ go run ./go-server/cmd/server.go +``` + +Open Polaris console, check service successfully registered into Polaris. + +### Run client +```shell +$ go run ./go-client/cmd/client.go +``` diff --git a/registry/polaris/README_zh.md b/registry/polaris/README_zh.md new file mode 100644 index 000000000..7e8d52e86 --- /dev/null +++ b/registry/polaris/README_zh.md @@ -0,0 +1,23 @@ +# 使用 Polaris 作为注册中心 + +[English](README.md) | [中文](README_zh.md) + +本示例展示 dubbo-go 使用 Polaris 作为注册中心的服务发现功能。 + +## 如何运行 + +### 启动 Polaris 服务器 +按照[安装文档](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E5%8D%95%E6%9C%BA%E7%89%88%E5%AE%89%E8%A3%85/)安装并启动 Polaris 服务器。 + +### 运行服务端 +```shell +$ go run ./go-server/cmd/server.go +``` + +打开 Polaris 控制台,检查服务是否成功注册到 Polaris。 + +### 运行客户端 +```shell +$ go run ./go-client/cmd/client.go +``` + diff --git a/registry/polaris/go-client/cmd/client.go b/registry/polaris/go-client/cmd/client.go new file mode 100644 index 000000000..7b0aab4a3 --- /dev/null +++ b/registry/polaris/go-client/cmd/client.go @@ -0,0 +1,69 @@ +/* + * 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" +) + +import ( + "dubbo.apache.org/dubbo-go/v3" + _ "dubbo.apache.org/dubbo-go/v3/imports" + "dubbo.apache.org/dubbo-go/v3/registry" + + "github.com/dubbogo/gost/log/logger" +) + +import ( + greet "github.com/apache/dubbo-go-samples/registry/polaris/proto" +) + +func main() { + ins, err := dubbo.NewInstance( + dubbo.WithName("dubbo_registry_polaris_client"), + dubbo.WithRegistry( + registry.WithPolaris(), + registry.WithAddress("127.0.0.1:8091"), + registry.WithNamespace("dubbogo"), + registry.WithRegisterInterface(), + ), + ) + if err != nil { + logger.Errorf("new dubbo instance failed: %v", err) + panic(err) + } + // configure the params that only client layer cares + cli, err := ins.NewClient() + if err != nil { + logger.Errorf("new client failed: %v", err) + panic(err) + } + + svc, err := greet.NewGreetService(cli) + if err != nil { + logger.Errorf("create greet service failed: %v", err) + panic(err) + } + + resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"}) + if err != nil { + logger.Error(err) + } + logger.Infof("Greet response: %s", resp) + select {} +} diff --git a/registry/polaris/go-server/cmd/server.go b/registry/polaris/go-server/cmd/server.go new file mode 100644 index 000000000..a1744ea52 --- /dev/null +++ b/registry/polaris/go-server/cmd/server.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 main + +import ( + "context" +) + +import ( + "dubbo.apache.org/dubbo-go/v3" + _ "dubbo.apache.org/dubbo-go/v3/imports" + "dubbo.apache.org/dubbo-go/v3/protocol" + "dubbo.apache.org/dubbo-go/v3/registry" + + "github.com/dubbogo/gost/log/logger" +) + +import ( + greet "github.com/apache/dubbo-go-samples/registry/polaris/proto" +) + +type GreetTripleServer struct { +} + +func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) { + resp := &greet.GreetResponse{Greeting: req.Name} + return resp, nil +} + +func main() { + polarisAddr := "127.0.0.1:8091" + + ins, err := dubbo.NewInstance( + dubbo.WithName("dubbo_registry_polaris_server"), + dubbo.WithRegistry( + registry.WithPolaris(), + registry.WithAddress(polarisAddr), + registry.WithNamespace("dubbogo"), + registry.WithRegisterInterface(), + ), + dubbo.WithProtocol( + protocol.WithTriple(), + protocol.WithPort(20000), + ), + ) + if err != nil { + logger.Errorf("new dubbo instance failed: %v", err) + panic(err) + } + srv, err := ins.NewServer() + if err != nil { + logger.Errorf("new server failed: %v", err) + panic(err) + } + if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil { + logger.Errorf("register greet handler failed: %v", err) + panic(err) + } + + if err := srv.Serve(); err != nil { + logger.Errorf("server serve failed: %v", err) + panic(err) + } +} diff --git a/registry/polaris/proto/greet.pb.go b/registry/polaris/proto/greet.pb.go new file mode 100644 index 000000000..acdcfa29b --- /dev/null +++ b/registry/polaris/proto/greet.pb.go @@ -0,0 +1,230 @@ +// +// 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. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v3.21.12 +// source: proto/greet.proto + +package greet + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type GreetRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *GreetRequest) Reset() { + *x = GreetRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_greet_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GreetRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GreetRequest) ProtoMessage() {} + +func (x *GreetRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_greet_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GreetRequest.ProtoReflect.Descriptor instead. +func (*GreetRequest) Descriptor() ([]byte, []int) { + return file_proto_greet_proto_rawDescGZIP(), []int{0} +} + +func (x *GreetRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type GreetResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Greeting string `protobuf:"bytes,1,opt,name=greeting,proto3" json:"greeting,omitempty"` +} + +func (x *GreetResponse) Reset() { + *x = GreetResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_greet_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GreetResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GreetResponse) ProtoMessage() {} + +func (x *GreetResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_greet_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GreetResponse.ProtoReflect.Descriptor instead. +func (*GreetResponse) Descriptor() ([]byte, []int) { + return file_proto_greet_proto_rawDescGZIP(), []int{1} +} + +func (x *GreetResponse) GetGreeting() string { + if x != nil { + return x.Greeting + } + return "" +} + +var File_proto_greet_proto protoreflect.FileDescriptor + +var file_proto_greet_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x67, 0x72, 0x65, 0x65, 0x74, 0x22, 0x22, 0x0a, 0x0c, 0x47, 0x72, + 0x65, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, + 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x2b, + 0x0a, 0x0d, 0x47, 0x72, 0x65, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, + 0x1a, 0x0a, 0x08, 0x67, 0x72, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x08, 0x67, 0x72, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x32, 0x44, 0x0a, 0x0c, 0x47, + 0x72, 0x65, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, 0x0a, 0x05, 0x47, + 0x72, 0x65, 0x65, 0x74, 0x12, 0x13, 0x2e, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2e, 0x47, 0x72, 0x65, + 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x67, 0x72, 0x65, 0x65, + 0x74, 0x2e, 0x47, 0x72, 0x65, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x00, 0x42, 0x41, 0x5a, 0x3f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x61, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2f, 0x64, 0x75, 0x62, 0x62, 0x6f, 0x2d, 0x67, 0x6f, 0x2d, + 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x72, 0x79, + 0x2f, 0x70, 0x6f, 0x6c, 0x61, 0x72, 0x69, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x67, + 0x72, 0x65, 0x65, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_greet_proto_rawDescOnce sync.Once + file_proto_greet_proto_rawDescData = file_proto_greet_proto_rawDesc +) + +func file_proto_greet_proto_rawDescGZIP() []byte { + file_proto_greet_proto_rawDescOnce.Do(func() { + file_proto_greet_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_greet_proto_rawDescData) + }) + return file_proto_greet_proto_rawDescData +} + +var file_proto_greet_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_greet_proto_goTypes = []interface{}{ + (*GreetRequest)(nil), // 0: greet.GreetRequest + (*GreetResponse)(nil), // 1: greet.GreetResponse +} +var file_proto_greet_proto_depIdxs = []int32{ + 0, // 0: greet.GreetService.Greet:input_type -> greet.GreetRequest + 1, // 1: greet.GreetService.Greet:output_type -> greet.GreetResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_proto_greet_proto_init() } +func file_proto_greet_proto_init() { + if File_proto_greet_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_greet_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GreetRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_greet_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GreetResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_greet_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_greet_proto_goTypes, + DependencyIndexes: file_proto_greet_proto_depIdxs, + MessageInfos: file_proto_greet_proto_msgTypes, + }.Build() + File_proto_greet_proto = out.File + file_proto_greet_proto_rawDesc = nil + file_proto_greet_proto_goTypes = nil + file_proto_greet_proto_depIdxs = nil +} diff --git a/registry/polaris/proto/greet.proto b/registry/polaris/proto/greet.proto new file mode 100644 index 000000000..541421606 --- /dev/null +++ b/registry/polaris/proto/greet.proto @@ -0,0 +1,36 @@ +/* + * 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. + */ + +syntax = "proto3"; + +package greet; + +option go_package = "github.com/apache/dubbo-go-samples/registry/polaris/proto;greet"; + +message GreetRequest { + string name = 1; +} + +message GreetResponse { + string greeting = 1; +} + +service GreetService { + rpc Greet(GreetRequest) returns (GreetResponse) {} +} + + diff --git a/registry/polaris/proto/greet.triple.go b/registry/polaris/proto/greet.triple.go new file mode 100644 index 000000000..1df734665 --- /dev/null +++ b/registry/polaris/proto/greet.triple.go @@ -0,0 +1,122 @@ +// Code generated by protoc-gen-triple. DO NOT EDIT. +// +// Source: proto/greet.proto +package greet + +import ( + "context" +) + +import ( + "dubbo.apache.org/dubbo-go/v3" + "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/triple_protocol" + "dubbo.apache.org/dubbo-go/v3/server" +) + +// This is a compile-time assertion to ensure that this generated file and the Triple package +// are compatible. If you get a compiler error that this constant is not defined, this code was +// generated with a version of Triple newer than the one compiled into your binary. You can fix the +// problem by either regenerating this code with an older version of Triple or updating the Triple +// version compiled into your binary. +const _ = triple_protocol.IsAtLeastVersion0_1_0 + +const ( + // GreetServiceName is the fully-qualified name of the GreetService service. + GreetServiceName = "greet.GreetService" +) + +// These constants are the fully-qualified names of the RPCs defined in this package. They're +// exposed at runtime as procedure and as the final two segments of the HTTP route. +// +// Note that these are different from the fully-qualified method names used by +// google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to +// reflection-formatted method names, remove the leading slash and convert the remaining slash to a +// period. +const ( + // GreetServiceGreetProcedure is the fully-qualified name of the GreetService's Greet RPC. + GreetServiceGreetProcedure = "/greet.GreetService/Greet" +) + +var ( + _ GreetService = (*GreetServiceImpl)(nil) +) + +// GreetService is a client for the greet.GreetService service. +type GreetService interface { + Greet(ctx context.Context, req *GreetRequest, opts ...client.CallOption) (*GreetResponse, error) +} + +// NewGreetService constructs a client for the greet.GreetService service. +func NewGreetService(cli *client.Client, opts ...client.ReferenceOption) (GreetService, error) { + conn, err := cli.DialWithInfo("greet.GreetService", &GreetService_ClientInfo, opts...) + if err != nil { + return nil, err + } + return &GreetServiceImpl{ + conn: conn, + }, nil +} + +func SetConsumerGreetService(srv common.RPCService) { + dubbo.SetConsumerServiceWithInfo(srv, &GreetService_ClientInfo) +} + +// GreetServiceImpl implements GreetService. +type GreetServiceImpl struct { + conn *client.Connection +} + +func (c *GreetServiceImpl) Greet(ctx context.Context, req *GreetRequest, opts ...client.CallOption) (*GreetResponse, error) { + resp := new(GreetResponse) + if err := c.conn.CallUnary(ctx, []interface{}{req}, resp, "Greet", opts...); err != nil { + return nil, err + } + return resp, nil +} + +var GreetService_ClientInfo = client.ClientInfo{ + InterfaceName: "greet.GreetService", + MethodNames: []string{"Greet"}, + ConnectionInjectFunc: func(dubboCliRaw interface{}, conn *client.Connection) { + dubboCli := dubboCliRaw.(*GreetServiceImpl) + dubboCli.conn = conn + }, +} + +// GreetServiceHandler is an implementation of the greet.GreetService service. +type GreetServiceHandler interface { + Greet(context.Context, *GreetRequest) (*GreetResponse, error) +} + +func RegisterGreetServiceHandler(srv *server.Server, hdlr GreetServiceHandler, opts ...server.ServiceOption) error { + return srv.Register(hdlr, &GreetService_ServiceInfo, opts...) +} + +func SetProviderGreetService(srv common.RPCService) { + dubbo.SetProviderServiceWithInfo(srv, &GreetService_ServiceInfo) +} + +var GreetService_ServiceInfo = server.ServiceInfo{ + InterfaceName: "greet.GreetService", + ServiceType: (*GreetServiceHandler)(nil), + Methods: []server.MethodInfo{ + { + Name: "Greet", + Type: constant.CallUnary, + ReqInitFunc: func() interface{} { + return new(GreetRequest) + }, + MethodFunc: func(ctx context.Context, args []interface{}, handler interface{}) (interface{}, error) { + req := args[0].(*GreetRequest) + res, err := handler.(GreetServiceHandler).Greet(ctx, req) + if err != nil { + return nil, err + } + return triple_protocol.NewResponse(res), nil + }, + }, + }, +} diff --git a/start_integrate_test.sh b/start_integrate_test.sh index 21393ad9a..bbbd519cf 100755 --- a/start_integrate_test.sh +++ b/start_integrate_test.sh @@ -42,6 +42,7 @@ array+=("filter/custom") # registry array+=("registry/zookeeper") array+=("registry/nacos") +array+=("registry/polaris") array+=("generic") @@ -80,7 +81,6 @@ array+=("tls") array+=("async") # polaris -array+=("compatibility/polaris/registry") array+=("compatibility/polaris/limit") # error