Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* compatibility/apisix: Example integrating apache/apisix with Dubbo-go
* compatibility/async: Callback (asynchronous) and one-way RPC example
* compatibility/config-api: How to use Dubbo-go by APIs without configuration files
* compatibility/configcenter: Usage of different config centers, including zookeeper and nacos
* compatibility/configcenter: Usage of different config centers, including zookeeper, nacos and apollo
* compatibility/context: How to transfer request context between multiple producers/consumers
* compatibility/direct: Direct invocation example
* compatibility/error: Error handling and triple protocol examples
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatibility/apisix:apache/apisix 与 Dubbo-go 整合示例
* compatibility/async:通过 callback 方式进行异步 RPC 及单向调用示例
* compatibility/config-api:无需配置文件,使用 API 启动 Dubbo-go 服务
* compatibility/configcenter:多种配置中心(如 zookeeper、nacos)用法示例
* compatibility/configcenter:多种配置中心(如 zookeeper、nacos、apollo)用法示例
* compatibility/context:多生产者/消费者间传递请求 context 示例
* compatibility/direct:直连调用示例,无需注册中心
* compatibility/error:错误处理与 triple 协议示例
Expand Down
76 changes: 76 additions & 0 deletions config_center/apollo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Dubbo-go Config-Center Sample

## 1. Introduction

This example shows dubbo-go's dynamic configuration feature with Apollo as config-center.

## 2. How to run

### Configure the configuration file into apollo

```yaml
dubbo:
registries:
demoZK:
protocol: zookeeper
timeout: 3s
address: '127.0.0.1:2181'
protocols:
triple:
name: tri
port: 20000
provider:
services:
GreeterProvider:
interface: com.apache.dubbo.sample.basic.IGreeter
```

Open `https://localhost:8070` with browser, make sure the relevant configuration is already in place in apollo.

### Start an instance with apollo as the configuration center

```go
ins, err := dubbo.NewInstance(
dubbo.WithConfigCenter(
config_center.WithApollo(),
config_center.WithAddress("127.0.0.1:8080"),
config_center.WithNamespace("dubbo.yml"),
config_center.WithDataID("dubbo.yml"),
config_center.WithAppID("SampleApp"),
config_center.WithCluster("default"),
config_center.WithFileExtProperties(),
),
)
if err != nil {
logger.Fatal(err)
}
```

### Start server and register for the service

```go
srv, err := ins.NewServer()
if err != nil {
logger.Fatal(err)
}

if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
logger.Fatal(err)
}

if err := srv.Serve(); err != nil {
logger.Error(err)
}
```

### Run client

```shell
$ go run ./go-client/cmd/main.go
```

### Expect output

```
Greet response: greeting:"Hello, Apollo"
```
76 changes: 76 additions & 0 deletions config_center/apollo/README_zn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Dubbo-go Config-Center Sample

## 1. 介绍

本示例演示Dubbo-Go以apollo为配置中心来实现动态配置功能

## 2. 如何运行

### 把配置文件配置到apollo中

```yaml
dubbo:
registries:
demoZK:
protocol: zookeeper
timeout: 3s
address: '127.0.0.1:2181'
protocols:
triple:
name: tri
port: 20000
provider:
services:
GreeterProvider:
interface: com.apache.dubbo.sample.basic.IGreeter
```

使用浏览器打开`https://localhost:8070` ,确保apollo中已有相关配置。

### 以apollo作为配置中心启动一个实例

```go
ins, err := dubbo.NewInstance(
dubbo.WithConfigCenter(
config_center.WithApollo(),
config_center.WithAddress("127.0.0.1:8080"),
config_center.WithNamespace("dubbo.yml"),
config_center.WithDataID("dubbo.yml"),
config_center.WithAppID("SampleApp"),
config_center.WithCluster("default"),
config_center.WithFileExtProperties(),
),
)
if err != nil {
logger.Fatal(err)
}
```

### 启动服务端并注册服务

```go
srv, err := ins.NewServer()
if err != nil {
logger.Fatal(err)
}

if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
logger.Fatal(err)
}

if err := srv.Serve(); err != nil {
logger.Error(err)
}
```

### 启动客户端

```shell
$ go run ./go-client/cmd/main.go
```

### 预期的输出

```
Greet response: greeting:"Hello, Apollo"
```
79 changes: 79 additions & 0 deletions config_center/apollo/go-client/cmd/main.go
Original file line number Diff line number Diff line change
@@ -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/config_center"
_ "dubbo.apache.org/dubbo-go/v3/imports"

"github.com/dubbogo/gost/log/logger"
)

import (
greet "github.com/apache/dubbo-go-samples/config_center/apollo/proto"
)

// Apollo Configuration Center Parameters
const (
apolloMetaAddress = "127.0.0.1:8080"
apolloAppID = "SampleApp"
apolloCluster = "default"
apolloNamespace = "dubbo.yml"
)

func main() {
// Initialize client using configuration center
ins, err := dubbo.NewInstance(
dubbo.WithConfigCenter(
config_center.WithApollo(),
config_center.WithAddress(apolloMetaAddress),
config_center.WithNamespace(apolloNamespace),
config_center.WithDataID(apolloNamespace),
config_center.WithAppID(apolloAppID),
config_center.WithCluster(apolloCluster),
//config_center.WithFileExtProperties(),
),
)
if err != nil {
logger.Fatal(err)
}

// Configure client parameters
cli, err := ins.NewClient()
if err != nil {
logger.Fatal(err)
}

// Create service client
svc, err := greet.NewGreetService(cli)
if err != nil {
logger.Fatal(err)
}

// Call remote service
resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "Apollo"})
if err != nil {
logger.Error(err)
}
logger.Infof("Greet response: %s", resp)
}
80 changes: 80 additions & 0 deletions config_center/apollo/go-server/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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/config_center"
_ "dubbo.apache.org/dubbo-go/v3/imports"

"github.com/dubbogo/gost/log/logger"
)

import (
greet "github.com/apache/dubbo-go-samples/config_center/apollo/proto"
)

// Apollo Configuration Center Parameters
const (
apolloMetaAddress = "127.0.0.1:8080"
apolloAppID = "SampleApp"
apolloCluster = "default"
apolloNamespace = "dubbo.yml"
)

type GreetTripleServer struct {
}

func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
resp := &greet.GreetResponse{Greeting: "Hello, " + req.Name}
return resp, nil
}

func main() {
ins, err := dubbo.NewInstance(
dubbo.WithConfigCenter(
config_center.WithApollo(),
config_center.WithAddress(apolloMetaAddress),
config_center.WithNamespace(apolloNamespace),
config_center.WithDataID(apolloNamespace),
config_center.WithAppID(apolloAppID),
config_center.WithCluster(apolloCluster),
//config_center.WithFileExtProperties(),
),
)
if err != nil {
logger.Fatal(err)
}

srv, err := ins.NewServer()
if err != nil {
logger.Fatal(err)
}

if err = greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
logger.Fatal(err)
}

if err = srv.Serve(); err != nil {
logger.Error(err)
}
}
Loading
Loading