Skip to content

Commit f3396ab

Browse files
authored
docs: update kitex generic and add kitex proxy docs (#1426)
1 parent b4c925d commit f3396ab

6 files changed

Lines changed: 1375 additions & 1206 deletions

File tree

content/en/docs/kitex/Tutorials/advanced-feature/generic-call/basic_usage.md

Lines changed: 606 additions & 607 deletions
Large diffs are not rendered by default.

content/en/docs/kitex/Tutorials/advanced-feature/grpcproxy.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ keywords: ["Kitex", "gRPC", "Proxy"]
66
description: Kitex supports custom Proxy routing for unregistered gRPC method calls.
77
---
88

9+
> **⚠️ Deprecated**
10+
>
11+
> This document has been deprecated. Please refer to the new comprehensive [Proxy Application Development Guide](./proxy_application_development/) for more up-to-date and complete proxy development documentation.
12+
>
13+
> The new guide covers all proxy scenarios including gRPC, TTHeader, and other protocols with better examples and best practices.
14+
15+
---
16+
917
Kitex provides the `WithGRPCUnknownServiceHandler` function when transport is using gRPC. When the server receives a request from an unknown gRPC method, it will execute the unknown service handler:
1018

1119
```go
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: "Proxy Application Development"
3+
date: 2025-09-28
4+
weight: 15
5+
keywords: ["Proxy", "Generic Call", "Advanced Feature"]
6+
description: "A comprehensive guide for developing proxy applications using Kitex generic calls, including server initialization, client setup, and traffic forwarding."
7+
---
8+
9+
## Prerequisites
10+
11+
Ensure you are using **cloudwego/kitex >= v0.15.1**.
12+
13+
For protocol consistency, if your business scenarios require proxying different streaming protocols simultaneously (such as gRPC and ttstream), it is recommended to divide business operations by cluster for different protocols. For example, ttstream clusters should only handle ttstream protocol requests, while gRPC clusters should only handle gRPC protocol requests. Ping-pong methods like ttheader/framed are recommended to go through the ttstream cluster.
14+
15+
## Initialize Server
16+
17+
Kitex proxy receives upstream traffic through a kitex server, which is identical to the binary generic server used in the [Kitex Generic Call Guide](../generic-call/).
18+
19+
```go
20+
import (
21+
"github.com/cloudwego/kitex/server"
22+
)
23+
24+
opts = append(opts, server.WithListener(ln),
25+
server.WithMetaHandler(transmeta.ServerTTHeaderHandler),
26+
server.WithMetaHandler(transmeta.ServerHTTP2Handler))
27+
28+
svr := server.NewServer(opts...)
29+
err := genericserver.RegisterUnknownServiceOrMethodHandler(svr, &genericserver.UnknownServiceOrMethodHandler{
30+
DefaultHandler: defaultUnknownHandler,
31+
StreamingHandler: streamingUnknownHandler,
32+
})
33+
```
34+
35+
If the server only receives ttheader/framed/buffered traffic, you only need to pass in the DefaultHandler. Otherwise, for traffic involving grpc/ttstream protocols, you must also specify the StreamingHandler.
36+
37+
**Note**: For non-streaming methods defined in IDL, if the upstream access is through gRPC protocol, the traffic will be routed to the StreamingHandler. This is because gRPC can only obtain the actual defined streaming mode through IDL. In unknown service scenarios, it cannot obtain IDL Info, so all traffic is treated as bidirectional streaming by default. Therefore, the method signature of StreamingHandler provides generic.BidiStreamingServer type input parameter.
38+
39+
## Initialize Client
40+
41+
After determining the protocol, when receiving RPC requests, you can forward them to specific kitex generic call clients. Kitex client and IDL Service have a one-to-one correspondence, so you can usually dynamically create clients to implement traffic forwarding.
42+
43+
```go
44+
import (
45+
"github.com/cloudwego/kitex/client/genericclient"
46+
)
47+
48+
options := []client.Option{
49+
client.WithHostPorts(addr.String()),
50+
client.WithTransportProtocol(transport.TTHeader | transport.TTHeaderStreaming),
51+
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
52+
client.WithMetaHandler(transmeta.ClientHTTP2Handler),
53+
}
54+
55+
// thrift binary
56+
cli, err := genericclient.NewClient(downstreamService, generic.BinaryThriftGenericV2(serviceName), options...)
57+
// protobuf binary
58+
cli, err := genericclient.NewClient(downstreamService, generic.BinaryPbGeneric(serviceName, packageName), options...)
59+
```
60+
61+
If the client needs to support streaming generic calls, you need to confirm the streaming call protocol. By default, the generic client generated through the above method uses TTHeaderStreaming for streaming protocols, while non-streaming messages use Framed or TTHeaderFramed. If you need to configure streaming methods to use GRPC protocol without changing the protocol of non-streaming methods, add the following client options:
62+
63+
```go
64+
client.WithTransportProtocol(transport.GRPCStreaming)
65+
```
66+
67+
## Traffic Forwarding
68+
69+
### Ping-pong Traffic Forwarding
70+
71+
**Recommended Best Practice**: [Proxy Implementation Example](https://github.com/cloudwego/kitex-tests/blob/main/generic/proxy/proxy.go#L136)
72+
73+
### Streaming Traffic Forwarding
74+
75+
**Recommended Best Practice**: [Streaming Proxy Implementation](https://github.com/cloudwego/kitex-tests/blob/main/generic/proxy/proxy.go#L150)

0 commit comments

Comments
 (0)