Skip to content

Commit d37ebce

Browse files
authored
Update & Translate Overview Docs (#3040)
1 parent 1ef7e90 commit d37ebce

File tree

1,550 files changed

+69793
-58399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,550 files changed

+69793
-58399
lines changed

content/en/blog/ web/_index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: "Web"
3+
linkTitle: "Web"
4+
weight: 32
5+
description: "Developing Dubbo Web applications running in the browser using Javascript"
6+
---
7+
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
---
2+
title: "Can web browser pages access Dubbo and gRPC microservices? Dubbo-js alpha version officially released"
3+
linkTitle: "Can web browser pages access Dubbo and gRPC microservices? Dubbo-js alpha version officially released"
4+
tags: ["web", "dubbo-js", "browser"]
5+
authors: ["Cai Jianyi"]
6+
date: 2023-10-07
7+
description: "Dubbo-js has officially released its first alpha version supporting the Dubbo3 protocol in September, which has the potential to fundamentally change the architecture and communication patterns between microservices' front-end and back-end, allowing you to access back-end Dubbo RPC services directly from browser pages or web servers."
8+
---
9+
10+
Based on the Triple protocol defined by Dubbo3, you can easily write browser and gRPC-compatible RPC services, allowing these services to run simultaneously on HTTP/1 and HTTP/2. The [Dubbo TypeScript SDK](https://github.com/apache/dubbo-js/) supports defining services using IDL or language-specific approaches and provides a lightweight API to publish or invoke these services.
11+
12+
Dubbo-js has officially released its first alpha version supporting the Dubbo3 protocol in September, which promises to transform the architecture and communication models between front-end and back-end of microservices, enabling direct access to back-end Dubbo RPC services from browser pages or web servers. The project is rapidly evolving, and developers interested in participating in the apache/dubbo-js project are welcome to search for the DingTalk group: **29775027779** to join the developer group.
13+
14+
![Can web browser pages access Dubbo and gRPC microservices](/imgs/blog/2023/9/web/img.png)
15+
# Browser Web Application Example
16+
This example demonstrates how to develop a web application running in the browser using dubbo-js, where the web page calls back-end services developed in dubbo node.js to generate page content. This example showcases both IDL and non-IDL coding modes.
17+
18+
![Can web browser pages access Dubbo and gRPC microservices](/imgs/blog/2023/9/web/img_1.png)
19+
## IDL Mode
20+
### Prerequisites
21+
First, we will use Vite to generate our front-end project template, which includes all the necessary feature support we'll need later.
22+
23+
```shell
24+
npm create vite@latest -- dubbo-web-example --template react-ts
25+
cd dubbo-web-example
26+
npm install
27+
```
28+
29+
Due to using Protocol Buffer, we first need to install the relevant code generation tools, including `@bufbuild/protoc-gen-es`, `@bufbuild/protobuf`, `@apachedubbo/protoc-gen-apache-dubbo-es`, and `@apachedubbo/dubbo`.
30+
31+
```shell
32+
npm install @bufbuild/protoc-gen-es @bufbuild/protobuf @apachedubbo/protoc-gen-apache-dubbo-es @apachedubbo/dubbo
33+
```
34+
35+
### Define Services Using Proto
36+
37+
Now, define a Dubbo service using Protocol Buffer (IDL).
38+
39+
Create the util/proto directory under src and generate the file
40+
41+
```shell
42+
mkdir -p src/util/proto && touch src/util/proto/example.proto
43+
```
44+
45+
Write the content
46+
47+
```protobuf
48+
syntax = "proto3";
49+
50+
package apache.dubbo.demo.example.v1;
51+
52+
message SayRequest {
53+
string sentence = 1;
54+
}
55+
56+
message SayResponse {
57+
string sentence = 1;
58+
}
59+
60+
service ExampleService {
61+
rpc Say(SayRequest) returns (SayResponse) {}
62+
}
63+
```
64+
65+
This file declares a service called `ExampleService`, defining the `Say` method along with its request parameter `SayRequest` and return value `SayResponse`.
66+
67+
### Generate Code
68+
69+
Create a gen directory as the target directory for generated files
70+
71+
```shell
72+
mkdir -p src/util/gen
73+
```
74+
75+
Run the following command to generate the code files in the gen directory using plugins like `protoc-gen-es`, `protoc-gen-apache-dubbo-es`
76+
77+
```shell
78+
PATH=$PATH:$(pwd)/node_modules/.bin \
79+
protoc -I src/util/proto \
80+
--es_out src/util/gen \
81+
--es_opt target=ts \
82+
--apache-dubbo-es_out src/util/gen \
83+
--apache-dubbo-es_opt target=ts \
84+
example.proto
85+
```
86+
87+
After running the command, you should see the following generated files in the target directory:
88+
89+
```
90+
├── src
91+
│ ├── util
92+
│ │ ├── gen
93+
│ │ │ ├── example_dubbo.ts
94+
│ │ │ └── example_pb.ts
95+
│ │ └── proto
96+
│ │ └── example.proto
97+
```
98+
99+
### Create App
100+
101+
First, install `@apachedubbo/dubbo-web`
102+
103+
```shell
104+
npm install @apachedubbo/dubbo-web
105+
```
106+
107+
Now we can import the service from the package and set up a client. Add the following content in App.tsx:
108+
109+
```typescript
110+
import { useState } from "react";
111+
import "./App.css";
112+
113+
import { createPromiseClient } from "@apachedubbo/dubbo";
114+
import { createDubboTransport } from "@apachedubbo/dubbo-web";
115+
116+
// Import service definition that you want to connect to.
117+
import { ExampleService } from "./util/gen/example_dubbo";
118+
119+
// The transport defines what type of endpoint we're hitting.
120+
// In our example we'll be communicating with a Dubbo endpoint.
121+
const transport = createDubboTransport({
122+
baseUrl: "http://localhost:8080",
123+
});
124+
125+
// Here we make the client itself, combining the service
126+
// definition with the transport.
127+
const client = createPromiseClient(ExampleService, transport, { serviceGroup: 'dubbo', serviceVersion: '1.0.0' });
128+
129+
function App() {
130+
const [inputValue, setInputValue] = useState("");
131+
const [messages, setMessages] = useState<
132+
{
133+
fromMe: boolean;
134+
message: string;
135+
}[]
136+
>([]);
137+
return (
138+
<>
139+
<ol>
140+
{messages.map((msg, index) => (
141+
<li key={index}>{`${msg.fromMe ? "ME:" : "Dubbo Server:"} ${msg.message}`}</li>
142+
))}
143+
</ol>
144+
<form
145+
onSubmit={async (e) => {
146+
e.preventDefault();
147+
// Clear inputValue since the user has submitted.
148+
setInputValue("");
149+
// Store the inputValue in the chain of messages and
150+
// mark this message as coming from "me"
151+
setMessages((prev) => [
152+
...prev,
153+
{
154+
fromMe: true,
155+
message: inputValue,
156+
},
157+
]);
158+
const response = await client.say({
159+
sentence: inputValue,
160+
});
161+
setMessages((prev) => [
162+
...prev,
163+
{
164+
fromMe: false,
165+
message: response.sentence,
166+
},
167+
]);
168+
}}
169+
>
170+
<input value={inputValue} onChange={(e) => setInputValue(e.target.value)} />
171+
<button type="submit">Send</button>
172+
</form>
173+
</>
174+
);
175+
}
176+
177+
export default App;
178+
```
179+
180+
Run the following command to obtain the sample page
181+
182+
```shell
183+
npm run dev
184+
```
185+
186+
### Start Server
187+
188+
Next, we need to start the server. You can develop the server in any language supported by Dubbo, such as Java, Go, or Node.js. Here we will use a Node.js server embedded with Dubbo services, detailed in the [Node.js Development of Dubbo Backend Services](https://github.com/apache/dubbo-js/tree/dubbo3/example/dubbo-node-example) guide.
189+
190+
However, we need to make an additional modification to the Node.js example: import @fastify/cors to solve the CORS issue for front-end requests.
191+
192+
```shell
193+
npm install @fastify/cors
194+
```
195+
196+
The modification in the server.ts file should include
197+
198+
```typescript
199+
...
200+
import cors from "@fastify/cors";
201+
202+
...
203+
async function main() {
204+
const server = fastify();
205+
...
206+
await server.register(cors, {
207+
origin: true,
208+
});
209+
...
210+
await server.listen({ host: "localhost", port: 8080 });
211+
...
212+
}
213+
214+
void main();
215+
```
216+
217+
Finally, run the code to start the service
218+
219+
```shell
220+
npx tsx server.ts
221+
```
222+
223+
## Non-IDL Mode
224+
In upcoming versions, we will continue to provide non-IDL mode communication support for easier access to non-IDL back-end services. Here, we will quickly look at the use of non-IDL mode.
225+
226+
Again, first install `@apachedubbo/dubbo`, `@apachedubbo/dubbo-web`
227