-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: translate multiple service guide
- Loading branch information
1 parent
281f385
commit bce10de
Showing
2 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
content/zh/docs/kitex/Tutorials/advanced-feature/multi_service.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
title: "单 server 多 service" | ||
date: 2023-11-30 | ||
weight: 10 | ||
keywords: ["Kitex", "多 Service", "单端口多服务", "gRPC"] | ||
description: Kitex 支持在一个 server 上注册多个 service。 | ||
--- | ||
|
||
注:当前这个功能仅支持 **gRPC** 传输协议。 | ||
|
||
## 使用 | ||
|
||
### gRPC service 定义 | ||
|
||
例如,假设您有两个 proto 文件(如下所示),每个包含一个 service。 | ||
通过执行 kitex 命令,可以自动生成对应的代码。 | ||
|
||
```protobuf | ||
// File1:servicea.proto | ||
syntax = "proto3"; | ||
option go_package = "myservice"; | ||
package myservice; | ||
service ServiceA { | ||
rpc EchoA (stream RequestA) returns (stream ReplyA) {} | ||
} | ||
message RequestA { | ||
... | ||
} | ||
message ReplyA { | ||
... | ||
} | ||
// File2:serviceb.proto | ||
syntax = "proto3"; | ||
option go_package = "myservice"; | ||
package myservice; | ||
service ServiceB { | ||
rpc EchoB (stream RequestB) returns (stream ReplyB) {} | ||
} | ||
message RequestB { | ||
... | ||
} | ||
message ReplyB { | ||
... | ||
} | ||
``` | ||
|
||
### 如何在一个 server 上注册多个 service | ||
|
||
在一个 server 上注册多个 service 是一个简单的过程。 | ||
|
||
首先,创建一个 server。然后,通过调用该 server 上的 `RegisterService` 函数,就可以注册 service 了。 | ||
|
||
可以在同一台 server 上提供多个 service,根据需要注册多个 service。 | ||
|
||
```golang | ||
package main | ||
|
||
import ( | ||
"github.com/cloudwego/kitex/pkg/server" | ||
|
||
servicea "your_servicea_kitex_gen_path" | ||
serviceb "your_serviceb_kitex_gen_path" | ||
) | ||
|
||
func main() { | ||
// create a server by calling server.NewServer | ||
svr := server.NewServer(your_server_option) | ||
// register multi-service on a server | ||
err := svr.RegisterService(servicea.NewServiceInfo(), new(ServiceAImpl)) | ||
err := svr.RegisterService(serviceb.NewServiceInfo(), new(ServiceBImpl)) | ||
|
||
err := svr.Run() | ||
|
||
if err != nil { | ||
logs.Error("%s", err.Error()) | ||
} | ||
logs.Stop() | ||
} | ||
``` |