Skip to content

Commit 98c60b2

Browse files
authored
feat: specify callback ip in config.json (#685)
* callback Signed-off-by: seeflood <[email protected]> * improve quickstart Signed-off-by: seeflood <[email protected]>
1 parent ce71ce2 commit 98c60b2

File tree

7 files changed

+163
-67
lines changed

7 files changed

+163
-67
lines changed

demo/pubsub/client/publish_client.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
client "mosn.io/layotto/sdk/go-sdk/client"
2525
)
2626

27-
const topicName = "topic1"
28-
2927
var storeName string
3028

3129
func init() {
@@ -43,16 +41,17 @@ func main() {
4341
panic(err)
4442
}
4543
// 2. publish a new event
46-
testPublish(cli)
44+
testPublish(cli, "hello", "world")
45+
testPublish(cli, "topic1", "value1")
4746
cli.Close()
4847
}
4948

50-
func testPublish(cli client.Client) error {
51-
data := []byte("value1")
52-
err := cli.PublishEvent(context.Background(), storeName, topicName, data)
49+
func testPublish(cli client.Client, topic string, value string) error {
50+
data := []byte(value)
51+
err := cli.PublishEvent(context.Background(), storeName, topic, data)
5352
if err != nil {
5453
panic(err)
5554
}
56-
fmt.Printf("Published a new event.Topic: %s ,Data: %s \n", topicName, data)
55+
fmt.Printf("Published a new event.Topic: %s ,Data: %s \n", topic, data)
5756
return err
5857
}

demo/pubsub/server/subscribe_server.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929
runtimev1pb "mosn.io/layotto/spec/proto/runtime/v1"
3030
)
3131

32-
const topicName = "topic1"
33-
3432
var storeName string
3533

3634
func init() {
@@ -68,14 +66,19 @@ func (a *AppCallbackServerImpl) ListTopicSubscriptions(ctx context.Context, empt
6866
result := &runtimev1pb.ListTopicSubscriptionsResponse{}
6967
ts := &runtimev1pb.TopicSubscription{
7068
PubsubName: storeName,
71-
Topic: topicName,
69+
Topic: "hello",
7270
Metadata: nil,
7371
}
7472
result.Subscriptions = append(result.Subscriptions, ts)
73+
result.Subscriptions = append(result.Subscriptions, &runtimev1pb.TopicSubscription{
74+
PubsubName: storeName,
75+
Topic: "topic1",
76+
Metadata: nil,
77+
})
7578
return result, nil
7679
}
7780

7881
func (a *AppCallbackServerImpl) OnTopicEvent(ctx context.Context, request *runtimev1pb.TopicEventRequest) (*runtimev1pb.TopicEventResponse, error) {
79-
fmt.Printf("Received a new event.Topic: %s , Data:%s \n", request.Topic, request.Data)
82+
fmt.Printf("Received a new event.Topic: %s , Data: %s \n", request.Topic, request.Data)
8083
return &runtimev1pb.TopicEventResponse{}, nil
8184
}

docker/layotto-redis/config_redis.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
},
6666
"app": {
6767
"app_id": "app1",
68-
"grpc_callback_port": 9999
68+
"grpc_callback_port": 9999,
69+
"grpc_callback_host": "host.docker.internal"
6970
}
7071
}
7172
}

docs/en/start/pubsub/start.md

+65-27
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ The architecture of this example is shown in the figure below. The running proce
1414
![img_1.png](../../../img/mq/start/img_1.png)
1515

1616
### Step 1. Start the Subscriber
17-
Build:
17+
<!-- tabs:start -->
18+
#### **Go**
19+
Build the golang subscriber
1820

1921
```shell
20-
cd ${project_path}/demo/pubsub/server/
22+
cd demo/pubsub/server/
2123
go build -o subscriber
2224
```
2325

@@ -27,6 +29,29 @@ Start subscriber:
2729
./subscriber -s pub_subs_demo
2830
```
2931

32+
#### **Java**
33+
34+
Download the java sdk and examples:
35+
36+
```bash
37+
git clone https://github.com/layotto/java-sdk
38+
```
39+
40+
```bash
41+
cd java-sdk
42+
```
43+
44+
Build and run it:
45+
46+
```bash
47+
# build example jar
48+
mvn -f examples-pubsub-subscriber/pom.xml clean package
49+
# run the example
50+
java -jar examples-pubsub-subscriber/target/examples-pubsub-subscriber-1.1.0-jar-with-dependencies.jar
51+
```
52+
53+
<!-- tabs:end -->
54+
3055
If the following information is printed out, it means the startup is successful:
3156

3257
```bash
@@ -38,7 +63,7 @@ Start listening on port 9999 ......
3863
>
3964
> - ListTopicSubscriptions
4065
>
41-
> Calling this API will return the topics subscribed by the application. This program will return "topic1"
66+
> Calling this API will return the topics subscribed by the application. This program will return "topic1" and "hello"
4267
>
4368
> - OnTopicEvent
4469
>
@@ -65,27 +90,7 @@ You can run Redis with Docker, then compile and run Layotto locally.
6590
6691
#### step 2.1. Run Redis with Docker
6792

68-
1. Get the latest version of Redis image.
69-
70-
Here we pull the latest version of the official image:
71-
72-
```shell
73-
docker pull redis:latest
74-
```
75-
76-
2. Check local mirror
77-
78-
Use the following command to check whether Redis is installed:
79-
80-
```shell
81-
docker images
82-
```
83-
84-
![img.png](../../../img/mq/start/img.png)
85-
86-
3. Run the container
87-
88-
After the installation is complete, we can use the following command to run the Redis container:
93+
We can use the following command to run the Redis container:
8994

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

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

120128
```shell
121129
cd ${project_path}/demo/pubsub/client/
122130
go build -o publisher
123131
./publisher -s pub_subs_demo
124132
```
125133

134+
#### **Java**
135+
136+
Download the java sdk and examples:
137+
138+
```shell @if.not.exist java-sdk
139+
git clone https://github.com/layotto/java-sdk
140+
```
141+
142+
```shell
143+
cd java-sdk
144+
```
145+
146+
Build:
147+
148+
```shell @if.not.exist examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
149+
# build example jar
150+
mvn -f examples-pubsub-publisher/pom.xml clean package
151+
```
152+
153+
Run it:
154+
155+
```shell
156+
# run the example
157+
java -jar examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
158+
```
159+
160+
<!-- tabs:end -->
161+
126162
If the following information is printed, the call is successful:
127163

128164
```bash
129-
Published a new event.Topic: topic1 ,Data: value1
165+
Published a new event.Topic: hello ,Data: world
166+
Published a new event.Topic: topic1 ,Data: value1
130167
```
131168

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

134171
Go back to the subscriber's command line and you will see that a new message has been received:
135172

136173
```bash
137-
Start listening on port 9999 ......
138-
Received a new event.Topic: topic1 , Data:value1
174+
Start listening on port 9999 ......
175+
Received a new event.Topic: topic1 , Data: value1
176+
Received a new event.Topic: hello , Data: world
139177
```
140178

141179
### step 5. Stop containers and release resources

docs/zh/start/pubsub/start.md

+74-26
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,57 @@ Layotto Pub/Sub API的设计目标是定义一套统一的消息发布/订阅API
1515
![img_1.png](../../../img/mq/start/img_1.png)
1616

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

1922
```shell
20-
cd ${project_path}/demo/pubsub/server/
23+
cd demo/pubsub/server/
2124
go build -o subscriber
2225
```
2326

27+
运行:
28+
2429
```shell @background
2530
./subscriber -s pub_subs_demo
2631
```
2732

28-
打印出如下信息则代表启动成功:
33+
#### **Java**
34+
35+
下载 java sdk 和 examples:
36+
37+
```bash
38+
git clone https://github.com/layotto/java-sdk
39+
```
40+
41+
切换目录:
42+
43+
```bash
44+
cd java-sdk
45+
```
46+
47+
构建、运行:
48+
49+
```bash
50+
# build example jar
51+
mvn -f examples-pubsub-subscriber/pom.xml clean package
52+
# run the example
53+
java -jar examples-pubsub-subscriber/target/examples-pubsub-subscriber-1.1.0-jar-with-dependencies.jar
54+
```
55+
56+
<!-- tabs:end -->
57+
58+
打印出以下信息说明运行成功:
2959

3060
```bash
31-
Start listening on port 9999 ......
61+
Start listening on port 9999 ......
3262
```
3363

3464
> [!TIP|label: Subscriber 程序做了什么?]
3565
> 该程序会启动一个gRPC服务器,开放两个接口:
3666
> - ListTopicSubscriptions
3767
>
38-
> 调用该接口会返回应用订阅的Topic。本程序会返回"topic1"
68+
> 调用该接口会返回应用订阅的Topic。本程序会返回"topic1"和 "hello"
3969
>
4070
> - OnTopicEvent
4171
>
@@ -61,25 +91,7 @@ docker-compose up -d
6191
> Layotto 在 Windows 下会编译失败。建议 Windows 用户使用 docker-compose 部署
6292
6393
#### step 2.1. 用 Docker 运行 Redis
64-
1. 取最新版的 Redis 镜像。
65-
这里我们拉取官方的最新版本的镜像:
66-
67-
```shell
68-
docker pull redis:latest
69-
```
70-
71-
2. 查看本地镜像
72-
使用以下命令来查看是否已安装了 redis:
73-
74-
```shell
75-
docker images
76-
```
77-
78-
![img.png](../../../img/mq/start/img.png)
79-
80-
3. 运行容器
81-
82-
安装完成后,我们可以使用以下命令来运行 redis 容器:
94+
我们可以使用以下命令来运行 Redis 容器:
8395

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

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

116131
```shell
117132
cd ${project_path}/demo/pubsub/client/
118133
go build -o publisher
119134
./publisher -s pub_subs_demo
120135
```
121136

137+
#### **Java**
138+
139+
下载 java sdk 和 examples:
140+
141+
```shell @if.not.exist java-sdk
142+
git clone https://github.com/layotto/java-sdk
143+
```
144+
145+
切换目录:
146+
147+
```shell
148+
cd java-sdk
149+
```
150+
151+
构建:
152+
153+
```shell @if.not.exist examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
154+
# build example jar
155+
mvn -f examples-pubsub-publisher/pom.xml clean package
156+
```
157+
158+
运行:
159+
160+
```shell
161+
# run the example
162+
java -jar examples-pubsub-publisher/target/examples-pubsub-publisher-1.1.0-jar-with-dependencies.jar
163+
```
164+
165+
166+
<!-- tabs:end -->
167+
122168
打印出如下信息则代表调用成功:
123169

124170
```bash
125-
Published a new event.Topic: topic1 ,Data: value1
171+
Published a new event.Topic: hello ,Data: world
172+
Published a new event.Topic: topic1 ,Data: value1
126173
```
127174

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

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

132179
```bash
133-
Start listening on port 9999 ......
134-
Received a new event.Topic: topic1 , Data:value1
180+
Start listening on port 9999 ......
181+
Received a new event.Topic: topic1 , Data: value1
182+
Received a new event.Topic: hello , Data: world
135183
```
136184

137185
### step 5. 销毁容器,释放资源

pkg/runtime/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
type AppConfig struct {
4040
AppId string `json:"app_id"`
4141
GrpcCallbackPort int `json:"grpc_callback_port"`
42+
GrpcCallbackHost string `json:"grpc_callback_host"`
4243
}
4344

4445
type MosnRuntimeConfig struct {

0 commit comments

Comments
 (0)