Skip to content

Commit 02b84f6

Browse files
authored
Feature/add apollo sample (#873)
* feat: add apollo sample
1 parent 739a6fb commit 02b84f6

File tree

11 files changed

+822
-26
lines changed

11 files changed

+822
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* compatibility/apisix: Example integrating apache/apisix with Dubbo-go
1111
* compatibility/async: Callback (asynchronous) and one-way RPC example
1212
* compatibility/config-api: How to use Dubbo-go by APIs without configuration files
13-
* compatibility/configcenter: Usage of different config centers, including zookeeper and nacos
13+
* compatibility/configcenter: Usage of different config centers, including zookeeper, nacos and apollo
1414
* compatibility/context: How to transfer request context between multiple producers/consumers
1515
* compatibility/direct: Direct invocation example
1616
* compatibility/error: Error handling and triple protocol examples

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* compatibility/apisix:apache/apisix 与 Dubbo-go 整合示例
77
* compatibility/async:通过 callback 方式进行异步 RPC 及单向调用示例
88
* compatibility/config-api:无需配置文件,使用 API 启动 Dubbo-go 服务
9-
* compatibility/configcenter:多种配置中心(如 zookeeper、nacos)用法示例
9+
* compatibility/configcenter:多种配置中心(如 zookeeper、nacos、apollo)用法示例
1010
* compatibility/context:多生产者/消费者间传递请求 context 示例
1111
* compatibility/direct:直连调用示例,无需注册中心
1212
* compatibility/error:错误处理与 triple 协议示例

config_center/apollo/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Dubbo-go Config-Center Sample
2+
3+
## 1. Introduction
4+
5+
This example shows dubbo-go's dynamic configuration feature with Apollo as config-center.
6+
7+
## 2. How to run
8+
9+
### Configure the configuration file into apollo
10+
11+
```yaml
12+
dubbo:
13+
registries:
14+
demoZK:
15+
protocol: zookeeper
16+
timeout: 3s
17+
address: '127.0.0.1:2181'
18+
protocols:
19+
triple:
20+
name: tri
21+
port: 20000
22+
provider:
23+
services:
24+
GreeterProvider:
25+
interface: com.apache.dubbo.sample.basic.IGreeter
26+
```
27+
28+
Open `https://localhost:8070` with browser, make sure the relevant configuration is already in place in apollo.
29+
30+
### Start an instance with apollo as the configuration center
31+
32+
```go
33+
ins, err := dubbo.NewInstance(
34+
dubbo.WithConfigCenter(
35+
config_center.WithApollo(),
36+
config_center.WithAddress("127.0.0.1:8080"),
37+
config_center.WithNamespace("dubbo.yml"),
38+
config_center.WithDataID("dubbo.yml"),
39+
config_center.WithAppID("SampleApp"),
40+
config_center.WithCluster("default"),
41+
config_center.WithFileExtProperties(),
42+
),
43+
)
44+
if err != nil {
45+
logger.Fatal(err)
46+
}
47+
```
48+
49+
### Start server and register for the service
50+
51+
```go
52+
srv, err := ins.NewServer()
53+
if err != nil {
54+
logger.Fatal(err)
55+
}
56+
57+
if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
58+
logger.Fatal(err)
59+
}
60+
61+
if err := srv.Serve(); err != nil {
62+
logger.Error(err)
63+
}
64+
```
65+
66+
### Run client
67+
68+
```shell
69+
$ go run ./go-client/cmd/main.go
70+
```
71+
72+
### Expect output
73+
74+
```
75+
Greet response: greeting:"Hello, Apollo"
76+
```

config_center/apollo/README_zn.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Dubbo-go Config-Center Sample
2+
3+
## 1. 介绍
4+
5+
本示例演示Dubbo-Go以apollo为配置中心来实现动态配置功能
6+
7+
## 2. 如何运行
8+
9+
### 把配置文件配置到apollo中
10+
11+
```yaml
12+
dubbo:
13+
registries:
14+
demoZK:
15+
protocol: zookeeper
16+
timeout: 3s
17+
address: '127.0.0.1:2181'
18+
protocols:
19+
triple:
20+
name: tri
21+
port: 20000
22+
provider:
23+
services:
24+
GreeterProvider:
25+
interface: com.apache.dubbo.sample.basic.IGreeter
26+
```
27+
28+
使用浏览器打开`https://localhost:8070` ,确保apollo中已有相关配置。
29+
30+
### 以apollo作为配置中心启动一个实例
31+
32+
```go
33+
ins, err := dubbo.NewInstance(
34+
dubbo.WithConfigCenter(
35+
config_center.WithApollo(),
36+
config_center.WithAddress("127.0.0.1:8080"),
37+
config_center.WithNamespace("dubbo.yml"),
38+
config_center.WithDataID("dubbo.yml"),
39+
config_center.WithAppID("SampleApp"),
40+
config_center.WithCluster("default"),
41+
config_center.WithFileExtProperties(),
42+
),
43+
)
44+
if err != nil {
45+
logger.Fatal(err)
46+
}
47+
```
48+
49+
### 启动服务端并注册服务
50+
51+
```go
52+
srv, err := ins.NewServer()
53+
if err != nil {
54+
logger.Fatal(err)
55+
}
56+
57+
if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
58+
logger.Fatal(err)
59+
}
60+
61+
if err := srv.Serve(); err != nil {
62+
logger.Error(err)
63+
}
64+
```
65+
66+
### 启动客户端
67+
68+
```shell
69+
$ go run ./go-client/cmd/main.go
70+
```
71+
72+
### 预期的输出
73+
74+
```
75+
Greet response: greeting:"Hello, Apollo"
76+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import (
21+
"context"
22+
)
23+
24+
import (
25+
"dubbo.apache.org/dubbo-go/v3"
26+
"dubbo.apache.org/dubbo-go/v3/config_center"
27+
_ "dubbo.apache.org/dubbo-go/v3/imports"
28+
29+
"github.com/dubbogo/gost/log/logger"
30+
)
31+
32+
import (
33+
greet "github.com/apache/dubbo-go-samples/config_center/apollo/proto"
34+
)
35+
36+
// Apollo Configuration Center Parameters
37+
const (
38+
apolloMetaAddress = "127.0.0.1:8080"
39+
apolloAppID = "SampleApp"
40+
apolloCluster = "default"
41+
apolloNamespace = "dubbo.yml"
42+
)
43+
44+
func main() {
45+
// Initialize client using configuration center
46+
ins, err := dubbo.NewInstance(
47+
dubbo.WithConfigCenter(
48+
config_center.WithApollo(),
49+
config_center.WithAddress(apolloMetaAddress),
50+
config_center.WithNamespace(apolloNamespace),
51+
config_center.WithDataID(apolloNamespace),
52+
config_center.WithAppID(apolloAppID),
53+
config_center.WithCluster(apolloCluster),
54+
//config_center.WithFileExtProperties(),
55+
),
56+
)
57+
if err != nil {
58+
logger.Fatal(err)
59+
}
60+
61+
// Configure client parameters
62+
cli, err := ins.NewClient()
63+
if err != nil {
64+
logger.Fatal(err)
65+
}
66+
67+
// Create service client
68+
svc, err := greet.NewGreetService(cli)
69+
if err != nil {
70+
logger.Fatal(err)
71+
}
72+
73+
// Call remote service
74+
resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "Apollo"})
75+
if err != nil {
76+
logger.Error(err)
77+
}
78+
logger.Infof("Greet response: %s", resp)
79+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import (
21+
"context"
22+
)
23+
24+
import (
25+
"dubbo.apache.org/dubbo-go/v3"
26+
"dubbo.apache.org/dubbo-go/v3/config_center"
27+
_ "dubbo.apache.org/dubbo-go/v3/imports"
28+
29+
"github.com/dubbogo/gost/log/logger"
30+
)
31+
32+
import (
33+
greet "github.com/apache/dubbo-go-samples/config_center/apollo/proto"
34+
)
35+
36+
// Apollo Configuration Center Parameters
37+
const (
38+
apolloMetaAddress = "127.0.0.1:8080"
39+
apolloAppID = "SampleApp"
40+
apolloCluster = "default"
41+
apolloNamespace = "dubbo.yml"
42+
)
43+
44+
type GreetTripleServer struct {
45+
}
46+
47+
func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
48+
resp := &greet.GreetResponse{Greeting: "Hello, " + req.Name}
49+
return resp, nil
50+
}
51+
52+
func main() {
53+
ins, err := dubbo.NewInstance(
54+
dubbo.WithConfigCenter(
55+
config_center.WithApollo(),
56+
config_center.WithAddress(apolloMetaAddress),
57+
config_center.WithNamespace(apolloNamespace),
58+
config_center.WithDataID(apolloNamespace),
59+
config_center.WithAppID(apolloAppID),
60+
config_center.WithCluster(apolloCluster),
61+
//config_center.WithFileExtProperties(),
62+
),
63+
)
64+
if err != nil {
65+
logger.Fatal(err)
66+
}
67+
68+
srv, err := ins.NewServer()
69+
if err != nil {
70+
logger.Fatal(err)
71+
}
72+
73+
if err = greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
74+
logger.Fatal(err)
75+
}
76+
77+
if err = srv.Serve(); err != nil {
78+
logger.Error(err)
79+
}
80+
}

0 commit comments

Comments
 (0)