From e9840f3c92f0bbb4ce6ab44c4a8ef5dc7a76470d Mon Sep 17 00:00:00 2001 From: seeflood Date: Thu, 30 Jun 2022 16:53:12 +0800 Subject: [PATCH 1/2] callback Signed-off-by: seeflood --- demo/pubsub/client/publish_client.go | 13 ++-- demo/pubsub/server/subscribe_server.go | 11 +-- docker/layotto-redis/config_redis.json | 3 +- docs/en/start/pubsub/start.md | 87 ++++++++++++++++-------- docs/zh/start/pubsub/start.md | 94 +++++++++++++++++++------- pkg/runtime/config.go | 1 + pkg/runtime/runtime.go | 10 ++- 7 files changed, 152 insertions(+), 67 deletions(-) diff --git a/demo/pubsub/client/publish_client.go b/demo/pubsub/client/publish_client.go index 983b8e77b1..7cf7ba8866 100644 --- a/demo/pubsub/client/publish_client.go +++ b/demo/pubsub/client/publish_client.go @@ -24,8 +24,6 @@ import ( client "mosn.io/layotto/sdk/go-sdk/client" ) -const topicName = "topic1" - var storeName string func init() { @@ -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 } diff --git a/demo/pubsub/server/subscribe_server.go b/demo/pubsub/server/subscribe_server.go index 1cf0b2d121..c6e654fef1 100644 --- a/demo/pubsub/server/subscribe_server.go +++ b/demo/pubsub/server/subscribe_server.go @@ -29,8 +29,6 @@ import ( runtimev1pb "mosn.io/layotto/spec/proto/runtime/v1" ) -const topicName = "topic1" - var storeName string func init() { @@ -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 } diff --git a/docker/layotto-redis/config_redis.json b/docker/layotto-redis/config_redis.json index 75c7950799..6e82c5a93a 100644 --- a/docker/layotto-redis/config_redis.json +++ b/docker/layotto-redis/config_redis.json @@ -65,7 +65,8 @@ }, "app": { "app_id": "app1", - "grpc_callback_port": 9999 + "grpc_callback_port": 9999, + "grpc_callback_host": "host.docker.internal" } } } diff --git a/docs/en/start/pubsub/start.md b/docs/en/start/pubsub/start.md index c802a88344..87e0e209a7 100644 --- a/docs/en/start/pubsub/start.md +++ b/docs/en/start/pubsub/start.md @@ -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: + +#### **Go** +Build the golang subscriber ```shell - cd ${project_path}/demo/pubsub/server/ + cd demo/pubsub/server/ go build -o subscriber ``` @@ -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 +``` + + + If the following information is printed out, it means the startup is successful: ```bash @@ -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 > @@ -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 @@ -116,6 +121,9 @@ After completion, the layotto file will be generated in the directory, run it: ### Step 3. Run the Publisher program and call Layotto to publish events + +#### **Go** +Build the golang publisher: ```shell cd ${project_path}/demo/pubsub/client/ @@ -123,10 +131,34 @@ After completion, the layotto file will be generated in the directory, run it: ./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 and run it: + +```shell +# build example jar +mvn -f examples-pubsub-publisher/pom.xml clean package +# run the example +java -jar examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar +``` + + + 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 @@ -134,8 +166,9 @@ Published a new event.Topic: topic1 ,Data: value1 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 diff --git a/docs/zh/start/pubsub/start.md b/docs/zh/start/pubsub/start.md index b558936211..b86735883d 100644 --- a/docs/zh/start/pubsub/start.md +++ b/docs/zh/start/pubsub/start.md @@ -15,27 +15,57 @@ Layotto Pub/Sub API的设计目标是定义一套统一的消息发布/订阅API ![img_1.png](../../../img/mq/start/img_1.png) ### step 1. 启动 Subscriber 程序,订阅事件 + +#### **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 -Start listening on port 9999 ...... +# 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 +``` + + + +打印出以下信息说明运行成功: + +```bash +Start listening on port 9999 ...... ``` > [!TIP|label: Subscriber 程序做了什么?] > 该程序会启动一个gRPC服务器,开放两个接口: > - ListTopicSubscriptions > -> 调用该接口会返回应用订阅的Topic。本程序会返回"topic1" +> 调用该接口会返回应用订阅的Topic。本程序会返回"topic1"和 "hello" > > - OnTopicEvent > @@ -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 @@ -112,6 +124,9 @@ go build -o layotto ### step 3. 运行Publisher程序,调用Layotto发布事件 + +#### **Go** +编译 golang 写的 publisher: ```shell cd ${project_path}/demo/pubsub/client/ @@ -119,10 +134,36 @@ go build -o layotto ./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 +# build example jar +mvn -f examples-pubsub-publisher/pom.xml clean package +# run the example +java -jar examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar +``` + + + 打印出如下信息则代表调用成功: ```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收到的事件消息 @@ -130,8 +171,9 @@ Published a new event.Topic: topic1 ,Data: value1 回到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. 销毁容器,释放资源 diff --git a/pkg/runtime/config.go b/pkg/runtime/config.go index 499233a74e..ade6ea7c9f 100644 --- a/pkg/runtime/config.go +++ b/pkg/runtime/config.go @@ -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 { diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index d7bf118d53..82c9580cf3 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -467,16 +467,22 @@ func (m *MosnRuntime) initAppCallbackConnection() error { if m.runtimeConfig == nil || m.runtimeConfig.AppManagement.GrpcCallbackPort == 0 { return nil } + // get callback address + host := m.runtimeConfig.AppManagement.GrpcCallbackHost + if host == "" { + host = "127.0.0.1" + } port := m.runtimeConfig.AppManagement.GrpcCallbackPort + address := fmt.Sprintf("%v:%v", host, port) opts := []rawGRPC.DialOption{ rawGRPC.WithInsecure(), } // dial ctx, cancel := context.WithTimeout(context.Background(), dialTimeout) defer cancel() - conn, err := rawGRPC.DialContext(ctx, fmt.Sprintf("127.0.0.1:%v", port), opts...) + conn, err := rawGRPC.DialContext(ctx, address, opts...) if err != nil { - log.DefaultLogger.Warnf("[runtime]failed to init callback client at port %v : %s", port, err) + log.DefaultLogger.Warnf("[runtime]failed to init callback client to address %v : %s", address, err) return err } m.AppCallbackConn = conn From b1f486056ae20c29e53963b216ceebb3506618a2 Mon Sep 17 00:00:00 2001 From: seeflood Date: Thu, 30 Jun 2022 17:20:29 +0800 Subject: [PATCH 2/2] improve quickstart Signed-off-by: seeflood --- docs/en/start/pubsub/start.md | 9 +++++++-- docs/zh/start/pubsub/start.md | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/en/start/pubsub/start.md b/docs/en/start/pubsub/start.md index 87e0e209a7..7a49f07266 100644 --- a/docs/en/start/pubsub/start.md +++ b/docs/en/start/pubsub/start.md @@ -143,11 +143,16 @@ git clone https://github.com/layotto/java-sdk cd java-sdk ``` -Build and run it: +Build: -```shell +```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 ``` diff --git a/docs/zh/start/pubsub/start.md b/docs/zh/start/pubsub/start.md index b86735883d..ba083ac491 100644 --- a/docs/zh/start/pubsub/start.md +++ b/docs/zh/start/pubsub/start.md @@ -148,15 +148,21 @@ git clone https://github.com/layotto/java-sdk cd java-sdk ``` -构建、运行: +构建: -```shell +```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 ``` + 打印出如下信息则代表调用成功: