Skip to content

Commit

Permalink
feat: specify callback ip in config.json (#685)
Browse files Browse the repository at this point in the history
* callback

Signed-off-by: seeflood <[email protected]>

* improve quickstart

Signed-off-by: seeflood <[email protected]>
  • Loading branch information
seeflood authored Jun 30, 2022
1 parent ce71ce2 commit 98c60b2
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 67 deletions.
13 changes: 6 additions & 7 deletions demo/pubsub/client/publish_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
client "mosn.io/layotto/sdk/go-sdk/client"
)

const topicName = "topic1"

var storeName string

func init() {
Expand All @@ -43,16 +41,17 @@ func main() {
panic(err)
}
// 2. publish a new event
testPublish(cli)
testPublish(cli, "hello", "world")
testPublish(cli, "topic1", "value1")
cli.Close()
}

func testPublish(cli client.Client) error {
data := []byte("value1")
err := cli.PublishEvent(context.Background(), storeName, topicName, data)
func testPublish(cli client.Client, topic string, value string) error {
data := []byte(value)
err := cli.PublishEvent(context.Background(), storeName, topic, data)
if err != nil {
panic(err)
}
fmt.Printf("Published a new event.Topic: %s ,Data: %s \n", topicName, data)
fmt.Printf("Published a new event.Topic: %s ,Data: %s \n", topic, data)
return err
}
11 changes: 7 additions & 4 deletions demo/pubsub/server/subscribe_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
runtimev1pb "mosn.io/layotto/spec/proto/runtime/v1"
)

const topicName = "topic1"

var storeName string

func init() {
Expand Down Expand Up @@ -68,14 +66,19 @@ func (a *AppCallbackServerImpl) ListTopicSubscriptions(ctx context.Context, empt
result := &runtimev1pb.ListTopicSubscriptionsResponse{}
ts := &runtimev1pb.TopicSubscription{
PubsubName: storeName,
Topic: topicName,
Topic: "hello",
Metadata: nil,
}
result.Subscriptions = append(result.Subscriptions, ts)
result.Subscriptions = append(result.Subscriptions, &runtimev1pb.TopicSubscription{
PubsubName: storeName,
Topic: "topic1",
Metadata: nil,
})
return result, nil
}

func (a *AppCallbackServerImpl) OnTopicEvent(ctx context.Context, request *runtimev1pb.TopicEventRequest) (*runtimev1pb.TopicEventResponse, error) {
fmt.Printf("Received a new event.Topic: %s , Data:%s \n", request.Topic, request.Data)
fmt.Printf("Received a new event.Topic: %s , Data: %s \n", request.Topic, request.Data)
return &runtimev1pb.TopicEventResponse{}, nil
}
3 changes: 2 additions & 1 deletion docker/layotto-redis/config_redis.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
},
"app": {
"app_id": "app1",
"grpc_callback_port": 9999
"grpc_callback_port": 9999,
"grpc_callback_host": "host.docker.internal"
}
}
}
Expand Down
92 changes: 65 additions & 27 deletions docs/en/start/pubsub/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ The architecture of this example is shown in the figure below. The running proce
![img_1.png](../../../img/mq/start/img_1.png)

### Step 1. Start the Subscriber
Build:
<!-- tabs:start -->
#### **Go**
Build the golang subscriber

```shell
cd ${project_path}/demo/pubsub/server/
cd demo/pubsub/server/
go build -o subscriber
```

Expand All @@ -27,6 +29,29 @@ Start subscriber:
./subscriber -s pub_subs_demo
```

#### **Java**

Download the java sdk and examples:

```bash
git clone https://github.com/layotto/java-sdk
```

```bash
cd java-sdk
```

Build and run it:

```bash
# build example jar
mvn -f examples-pubsub-subscriber/pom.xml clean package
# run the example
java -jar examples-pubsub-subscriber/target/examples-pubsub-subscriber-1.1.0-jar-with-dependencies.jar
```

<!-- tabs:end -->

If the following information is printed out, it means the startup is successful:

```bash
Expand All @@ -38,7 +63,7 @@ Start listening on port 9999 ......
>
> - ListTopicSubscriptions
>
> Calling this API will return the topics subscribed by the application. This program will return "topic1"
> Calling this API will return the topics subscribed by the application. This program will return "topic1" and "hello"
>
> - OnTopicEvent
>
Expand All @@ -65,27 +90,7 @@ You can run Redis with Docker, then compile and run Layotto locally.

#### step 2.1. Run Redis with Docker

1. Get the latest version of Redis image.

Here we pull the latest version of the official image:

```shell
docker pull redis:latest
```

2. Check local mirror

Use the following command to check whether Redis is installed:

```shell
docker images
```

![img.png](../../../img/mq/start/img.png)

3. Run the container

After the installation is complete, we can use the following command to run the Redis container:
We can use the following command to run the Redis container:

```shell
docker run -itd --name redis-test -p 6380:6379 redis
Expand Down Expand Up @@ -116,26 +121,59 @@ After completion, the layotto file will be generated in the directory, run it:
<!-- tabs:end -->

### Step 3. Run the Publisher program and call Layotto to publish events
<!-- tabs:start -->
#### **Go**
Build the golang publisher:

```shell
cd ${project_path}/demo/pubsub/client/
go build -o publisher
./publisher -s pub_subs_demo
```

#### **Java**

Download the java sdk and examples:

```shell @if.not.exist java-sdk
git clone https://github.com/layotto/java-sdk
```

```shell
cd java-sdk
```

Build:

```shell @if.not.exist examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
# build example jar
mvn -f examples-pubsub-publisher/pom.xml clean package
```

Run it:

```shell
# run the example
java -jar examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
```

<!-- tabs:end -->

If the following information is printed, the call is successful:

```bash
Published a new event.Topic: topic1 ,Data: value1
Published a new event.Topic: hello ,Data: world
Published a new event.Topic: topic1 ,Data: value1
```

### Step 4. Check the event message received by the subscriber

Go back to the subscriber's command line and you will see that a new message has been received:
```bash
Start listening on port 9999 ......
Received a new event.Topic: topic1 , Data:value1
Start listening on port 9999 ......
Received a new event.Topic: topic1 , Data: value1
Received a new event.Topic: hello , Data: world
```
### step 5. Stop containers and release resources
Expand Down
100 changes: 74 additions & 26 deletions docs/zh/start/pubsub/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,57 @@ Layotto Pub/Sub API的设计目标是定义一套统一的消息发布/订阅API
![img_1.png](../../../img/mq/start/img_1.png)

### step 1. 启动 Subscriber 程序,订阅事件
<!-- tabs:start -->
#### **Go**
编译 golang 写的 subscriber:

```shell
cd ${project_path}/demo/pubsub/server/
cd demo/pubsub/server/
go build -o subscriber
```

运行:

```shell @background
./subscriber -s pub_subs_demo
```

打印出如下信息则代表启动成功:
#### **Java**

下载 java sdk 和 examples:

```bash
git clone https://github.com/layotto/java-sdk
```

切换目录:

```bash
cd java-sdk
```

构建、运行:

```bash
# build example jar
mvn -f examples-pubsub-subscriber/pom.xml clean package
# run the example
java -jar examples-pubsub-subscriber/target/examples-pubsub-subscriber-1.1.0-jar-with-dependencies.jar
```

<!-- tabs:end -->

打印出以下信息说明运行成功:

```bash
Start listening on port 9999 ......
Start listening on port 9999 ......
```

> [!TIP|label: Subscriber 程序做了什么?]
> 该程序会启动一个gRPC服务器,开放两个接口:
> - ListTopicSubscriptions
>
> 调用该接口会返回应用订阅的Topic。本程序会返回"topic1"
> 调用该接口会返回应用订阅的Topic。本程序会返回"topic1"和 "hello"
>
> - OnTopicEvent
>
Expand All @@ -61,25 +91,7 @@ docker-compose up -d
> Layotto 在 Windows 下会编译失败。建议 Windows 用户使用 docker-compose 部署
#### step 2.1. 用 Docker 运行 Redis
1. 取最新版的 Redis 镜像。
这里我们拉取官方的最新版本的镜像:

```shell
docker pull redis:latest
```

2. 查看本地镜像
使用以下命令来查看是否已安装了 redis:

```shell
docker images
```

![img.png](../../../img/mq/start/img.png)

3. 运行容器

安装完成后,我们可以使用以下命令来运行 redis 容器:
我们可以使用以下命令来运行 Redis 容器:

```shell
docker run -itd --name redis-test -p 6380:6379 redis
Expand Down Expand Up @@ -112,26 +124,62 @@ go build -o layotto
<!-- tabs:end -->

### step 3. 运行Publisher程序,调用Layotto发布事件
<!-- tabs:start -->
#### **Go**
编译 golang 写的 publisher:

```shell
cd ${project_path}/demo/pubsub/client/
go build -o publisher
./publisher -s pub_subs_demo
```

#### **Java**

下载 java sdk 和 examples:

```shell @if.not.exist java-sdk
git clone https://github.com/layotto/java-sdk
```

切换目录:

```shell
cd java-sdk
```

构建:

```shell @if.not.exist examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
# build example jar
mvn -f examples-pubsub-publisher/pom.xml clean package
```

运行:

```shell
# run the example
java -jar examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
```


<!-- tabs:end -->

打印出如下信息则代表调用成功:

```bash
Published a new event.Topic: topic1 ,Data: value1
Published a new event.Topic: hello ,Data: world
Published a new event.Topic: topic1 ,Data: value1
```

### step 4. 检查Subscriber收到的事件消息

回到subscriber的命令行,会看到接收到了新消息:

```bash
Start listening on port 9999 ......
Received a new event.Topic: topic1 , Data:value1
Start listening on port 9999 ......
Received a new event.Topic: topic1 , Data: value1
Received a new event.Topic: hello , Data: world
```

### step 5. 销毁容器,释放资源
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
type AppConfig struct {
AppId string `json:"app_id"`
GrpcCallbackPort int `json:"grpc_callback_port"`
GrpcCallbackHost string `json:"grpc_callback_host"`
}

type MosnRuntimeConfig struct {
Expand Down
Loading

0 comments on commit 98c60b2

Please sign in to comment.