Skip to content

Commit b68376a

Browse files
committed
docs: update some samples
1 parent d2cd248 commit b68376a

File tree

18 files changed

+165
-121
lines changed

18 files changed

+165
-121
lines changed

README.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,76 +29,91 @@ Visit [the official website](https://dubbo.apache.org/) for more information.
2929
- **Serialization**: Customizable(protobuf, json...)
3030

3131

32-
## Getting started
3332

34-
Before you begin, ensure that you have **`python 3.11+`**. Then, install Dubbo-Python in your project using the following steps:
33+
## Installation
34+
35+
Before you start, make sure you have **`python 3.11+`** installed.
3536

36-
```shell
37-
git clone https://github.com/apache/dubbo-python.git
38-
cd dubbo-python && pip install .
39-
```
37+
1. Install from source
38+
39+
```sh
40+
git clone https://github.com/apache/dubbo-python.git
41+
cd dubbo-python && pip install .
42+
```
43+
44+
45+
## Getting started
4046

41-
Get started with Dubbo-Python in just 5 minutes by following our [Quick Start Guide](https://github.com/apache/dubbo-python/tree/main/samples).
47+
Get up and running with Dubbo-Python in just 5 minutes by following our [Quick Start Guide](https://github.com/apache/dubbo-python/tree/main/samples).
4248

43-
It's as simple as the following code snippet. With just a few lines of code, you can launch a fully functional point-to-point RPC service :
49+
It's as simple as the code snippet below. With just a few lines of code, you can launch a fully functional point-to-point RPC service:
4450

45-
1. Build and start the Server
51+
1. Build and start the server
4652

4753
```python
4854
import dubbo
4955
from dubbo.configs import ServiceConfig
50-
from dubbo.proxy.handlers import RpcServiceHandler, RpcMethodHandler
56+
from dubbo.proxy.handlers import RpcMethodHandler, RpcServiceHandler
5157

5258

53-
def handle_unary(request):
54-
s = request.decode("utf-8")
55-
print(f"Received request: {s}")
56-
return (s + " world").encode("utf-8")
59+
class UnaryServiceServicer:
60+
def say_hello(self, message: bytes) -> bytes:
61+
print(f"Received message from client: {message}")
62+
return b"Hello from server"
5763

5864

59-
if __name__ == "__main__":
65+
def build_service_handler():
6066
# build a method handler
61-
method_handler = RpcMethodHandler.unary(handle_unary)
67+
method_handler = RpcMethodHandler.unary(
68+
method=UnaryServiceServicer().say_hello, method_name="unary"
69+
)
6270
# build a service handler
6371
service_handler = RpcServiceHandler(
6472
service_name="org.apache.dubbo.samples.HelloWorld",
65-
method_handlers={"unary": method_handler},
73+
method_handlers=[method_handler],
6674
)
75+
return service_handler
6776

68-
service_config = ServiceConfig(service_handler)
6977

78+
if __name__ == "__main__":
79+
# build service config
80+
service_handler = build_service_handler()
81+
service_config = ServiceConfig(
82+
service_handler=service_handler, host="127.0.0.1", port=50051
83+
)
7084
# start the server
7185
server = dubbo.Server(service_config).start()
7286

7387
input("Press Enter to stop the server...\n")
7488
```
7589

76-
2. Build and start the Client
90+
1. Build and start the Client
7791

7892
```python
7993
import dubbo
8094
from dubbo.configs import ReferenceConfig
8195

8296

8397
class UnaryServiceStub:
84-
8598
def __init__(self, client: dubbo.Client):
8699
self.unary = client.unary(method_name="unary")
87100

88-
def unary(self, request):
89-
return self.unary(request)
101+
def say_hello(self, message: bytes) -> bytes:
102+
return self.unary(message)
90103

91104

92105
if __name__ == "__main__":
106+
# Create a client
93107
reference_config = ReferenceConfig.from_url(
94108
"tri://127.0.0.1:50051/org.apache.dubbo.samples.HelloWorld"
95109
)
96110
dubbo_client = dubbo.Client(reference_config)
97-
98111
unary_service_stub = UnaryServiceStub(dubbo_client)
99112

100-
result = unary_service_stub.unary("hello".encode("utf-8"))
101-
print(result.decode("utf-8"))
113+
# Call the remote method
114+
result = unary_service_stub.say_hello(b"Hello from client")
115+
print(result)
116+
102117
```
103118

104119

dubbo/protocol/triple/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class TripleHeaderValue(enum.Enum):
118118
TRAILERS = "trailers"
119119
HTTP = "http"
120120
HTTPS = "https"
121-
APPLICATION_GRPC_PROTO = "application/grpc+proto"
121+
APPLICATION_GRPC_PROTO = "application/grpc+data"
122122
APPLICATION_GRPC = "application/grpc"
123123

124124
TEXT_PLAIN_UTF8 = "text/plain; encoding=utf-8"

samples/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# Dubbo-python Samples
22

3-
Before you begin, ensure that you have **`Python 3.11+`**. Then, install Dubbo-Python in your project using the following steps:
4-
5-
```shell
6-
git clone https://github.com/apache/dubbo-python.git
7-
cd dubbo-python && pip install .
8-
```
9-
103
## What It Contains
114

125
1. [**helloworld**](./helloworld): The simplest usage example for quick start.

samples/helloworld/client.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ class UnaryServiceStub:
2121
def __init__(self, client: dubbo.Client):
2222
self.unary = client.unary(method_name="unary")
2323

24-
def unary(self, request):
25-
return self.unary(request)
24+
def say_hello(self, message: bytes) -> bytes:
25+
return self.unary(message)
2626

2727

2828
if __name__ == "__main__":
29+
# Create a client
2930
reference_config = ReferenceConfig.from_url(
3031
"tri://127.0.0.1:50051/org.apache.dubbo.samples.HelloWorld"
3132
)
3233
dubbo_client = dubbo.Client(reference_config)
33-
3434
unary_service_stub = UnaryServiceStub(dubbo_client)
3535

36-
result = unary_service_stub.unary("hello".encode("utf-8"))
37-
print(result.decode("utf-8"))
36+
# Call the remote method
37+
result = unary_service_stub.say_hello(b"Hello from client")
38+
print(result)

samples/helloworld/server.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,31 @@
1818
from dubbo.proxy.handlers import RpcMethodHandler, RpcServiceHandler
1919

2020

21-
def handle_unary(request):
22-
s = request.decode("utf-8")
23-
print(f"Received request: {s}")
24-
return (s + " world").encode("utf-8")
21+
class UnaryServiceServicer:
22+
def say_hello(self, message: bytes) -> bytes:
23+
print(f"Received message from client: {message}")
24+
return b"Hello from server"
2525

2626

27-
if __name__ == "__main__":
27+
def build_service_handler():
2828
# build a method handler
29-
method_handler = RpcMethodHandler.unary(method=handle_unary, method_name="unary")
29+
method_handler = RpcMethodHandler.unary(
30+
method=UnaryServiceServicer().say_hello, method_name="unary"
31+
)
3032
# build a service handler
3133
service_handler = RpcServiceHandler(
3234
service_name="org.apache.dubbo.samples.HelloWorld",
3335
method_handlers=[method_handler],
3436
)
37+
return service_handler
38+
3539

40+
if __name__ == "__main__":
41+
# build service config
42+
service_handler = build_service_handler()
3643
service_config = ServiceConfig(
3744
service_handler=service_handler, host="127.0.0.1", port=50051
3845
)
39-
4046
# start the server
4147
server = dubbo.Server(service_config).start()
4248

samples/registry/zookeeper/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import dubbo
1818
from dubbo.configs import ReferenceConfig, RegistryConfig
19-
from samples.proto import greeter_pb2
19+
from samples.data import greeter_pb2
2020

2121

2222
class GreeterServiceStub:
@@ -36,7 +36,7 @@ def say_hello(self, request):
3636
bootstrap = dubbo.Dubbo(registry_config=registry_config)
3737

3838
reference_config = ReferenceConfig(
39-
protocol="tri", service="org.apache.dubbo.samples.proto.Greeter"
39+
protocol="tri", service="org.apache.dubbo.samples.data.Greeter"
4040
)
4141
dubbo_client = bootstrap.create_client(reference_config)
4242

samples/registry/zookeeper/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import dubbo
1717
from dubbo.configs import RegistryConfig, ServiceConfig
1818
from dubbo.proxy.handlers import RpcMethodHandler, RpcServiceHandler
19-
from samples.proto import greeter_pb2
19+
from samples.data import greeter_pb2
2020

2121

2222
def say_hello(request):
@@ -34,7 +34,7 @@ def say_hello(request):
3434
)
3535
# build a service handler
3636
service_handler = RpcServiceHandler(
37-
service_name="org.apache.dubbo.samples.proto.Greeter",
37+
service_name="org.apache.dubbo.samples.data.Greeter",
3838
method_handlers=[method_handler],
3939
)
4040

0 commit comments

Comments
 (0)