Dubbo Getting Started Series: Quickly Deploy a Microservice Application

This article will demonstrate how to quickly build and deploy a microservice application based on Dubbo Samples.

background

arch-service-discovery

As a microservice framework, the most important thing about Dubbo is to provide users with cross-process RPC remote calling capabilities. As shown in the figure above, Dubbo's service consumer (Consumer) sends the request to the service provider (Provider) through a series of work.

In order to achieve such a goal, Dubbo introduces the registration center (Registry) component. Through the registration center, service consumers can perceive the connection mode of the service provider and thus send the request to the correct service provider.

Target

Understand how microservices are called and the capabilities of Dubbo

difficulty

Low

Environmental requirements

  • System: Windows, Linux, MacOS

  • JDK 8 and above (JDK17 is recommended)

  • Git

  • Docker (optional)

Hands

This chapter will teach you step by step how to deploy and run the simplest Dubbo use case through a few simple commands.

1. Get the test project

Before starting the entire tutorial, we need to get the code for the test project. All Dubbo test case codes are stored in the apache/dubbo-samples warehouse. The following command can help you get all the codes in the Samples warehouse.

git clone --depth=1 --branch master [email protected]:apache/dubbo-samples.git

2. Understand the Dubbo Samples project structure

After cloning the apache/dubbo-samples warehouse locally, this section will explain the specific organization of the warehouse.

.
├── codestyle        // 开发使用的 style 配置文件

├── 1-basic          // 基础的入门用例
├── 2-advanced       // 高级用法
├── 3-extensions     // 扩展使用示例
├── 4-governance     // 服务治理用例
├── 10-task          // Dubbo 学习系列示例

├── 99-integration   // 集成测试使用
├── test             // 集成测试使用
└── tools            // 三方组件快速启动工具

As shown in the table above, apache/dubbo-samples mainly consists of three parts: code style files, test code, and integration tests.

  1. Code style files can be used when developing Dubbo code, including IntelliJ IDEA configuration files.

  2. Test code is the core content required by this textbook. It currently includes 5 parts: basic introductory use cases for beginners, advanced usage for developers, extensions Dubbo peripheral extension usage examples for middleware maintainers, governance service management use cases for production, and Dubbo learning series. This article will explain based on the simplest way to use Dubbo API in basic entry use cases.

  3. Integration testing is an important part of Dubbo's quality assurance system. Each version of Dubbo will perform regression verification on all samples to ensure that all changes to Dubbo will not affect the use of samples.

3. Start a simple registration center

Starting from this section, a microservice application will be formally deployed through three commands.

As can be seen from the background section, a major prerequisite for running a Dubbo application is to deploy a registration center. In order to make this tutorial easier to get started, we provide a simple launcher based on the Apache Zookeeper registration center. If you need to deploy it in a production environment Registration center, please refer to the production environment initialization article to deploy a highly available registration center.

Windows:
./mvnw.cmd clean compile exec:java -pl tools/embedded-zookeeper

Linux / MacOS:
./mvnw clean compile exec:java -pl tools/embedded-zookeeper

注:需要开一个独立的 terminal 运行,命令将会保持一直执行的状态。

Docker:
docker run --name some-zookeeper --restart always -d zookeeper

After executing the above command, wait for a while and the log as shown in the figure below will appear, which means that the registration center has been started and you can continue to perform subsequent tasks.

registry

4. Start the service provider

After starting the registration center, the next step is to start a service provider that provides services to the outside world. Corresponding examples are also provided in dubbo-samples, which can be quickly pulled up with the following command.

Windows:
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.provider.Application"

Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.provider.Application"

注:需要开一个独立的 terminal 运行,命令将会保持一直执行的状态。

After executing the above command, wait for a while and the log as shown in the figure below will appear ( DubboBootstrap awaiting), which means that the service provider has been started, indicating that the service provider can provide services to the outside world.

provider

[19/01/23 03:55:49:049 CST] org.apache.dubbo.samples.provider.Application.main()  INFO bootstrap.DubboBootstrap:  [DUBBO] DubboBootstrap awaiting ..., dubbo version: 3.2.0-beta.3, current host: 169.254.44.42

5. Start the service consumer

The last step is to start a service consumer to call the service provider, which is the core of the RPC call and provides a bridge for the service consumer to call the service provider.

Windows:
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.client.Application"

Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.client.Application"

After executing the above command, wait for a while and the log as shown below will appear ( hi, dubbo). The printed data is returned by the service provider after processing, indicating the success of a service call.

consumer

Receive result ======> hi, dubbo

Further reading

1. How does the consumer find the server?

Step 3 in this use case starts a Zookeeper registration center, and the service provider writes its own address to the registration center for service consumers to obtain.

Dubbo will write the connection information of the service provider under Zookeeper /dubbo/interfaceNameand ./services/appName

The following is an example of data on Zookeeper:

[zk: localhost:2181(CONNECTED) 5] ls /dubbo/org.apache.dubbo.samples.api.GreetingsService/providers
[dubbo%3A%2F%2F30.221.146.35%3A20880%2Forg.apache.dubbo.samples.api.GreetingsService%3Fanyhost%3Dtrue%26application%3Dfirst-dubbo-provider%26background%3Dfalse%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26environment%3Dproduct%26generic%3Dfalse%26interface%3Dorg.apache.dubbo.samples.api.GreetingsService%26ipv6%3Dfd00%3A1%3A5%3A5200%3A3218%3A774a%3A4f67%3A2341%26methods%3DsayHi%26pid%3D85639%26release%3D3.1.4%26service-name-mapping%3Dtrue%26side%3Dprovider%26timestamp%3D1674960780647]

[zk: localhost:2181(CONNECTED) 2] ls /services/first-dubbo-provider
[30.221.146.35:20880]
[zk: localhost:2181(CONNECTED) 3] get /services/first-dubbo-provider/30.221.146.35:20880
{"name":"first-dubbo-provider","id":"30.221.146.35:20880","address":"30.221.146.35","port":20880,"sslPort":null,"payload":{"@class":"org.apache.dubbo.registry.zookeeper.ZookeeperInstance","id":"30.221.146.35:20880","name":"first-dubbo-provider","metadata":{"dubbo.endpoints":"[{\"port\":20880,\"protocol\":\"dubbo\"}]","dubbo.metadata-service.url-params":"{\"connections\":\"1\",\"version\":\"1.0.0\",\"dubbo\":\"2.0.2\",\"release\":\"3.1.4\",\"side\":\"provider\",\"ipv6\":\"fd00:1:5:5200:3218:774a:4f67:2341\",\"port\":\"20880\",\"protocol\":\"dubbo\"}","dubbo.metadata.revision":"871fbc9cb2730caea9b0d858852d5ede","dubbo.metadata.storage-type":"local","ipv6":"fd00:1:5:5200:3218:774a:4f67:2341","timestamp":"1674960780647"}},"registrationTimeUTC":1674960781893,"serviceType":"DYNAMIC","uriSpec":null}

For more details about Dubbo service discovery model, please refer to the service discovery article.

2. How does the consumer initiate a request?

In Dubbo's calling model, the interface serves as a bridge connecting service consumers and service providers.

The service provider implements the specified interface, and the service consumer subscribes to this interface through Dubbo. When the service consumer calls the interface, Dubbo will encapsulate the request into a network request, and then send it to the service provider for the actual call.

In this use case, an GreetingsServiceinterface is defined, and this interface has a sayHimethod named.

// 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java

package org.apache.dubbo.samples.api;

public interface GreetingsService {

    String sayHi(String name);

}

Service consumers can obtain GreetingsServicethe proxy of this interface through Dubbo's API, and then call it according to the ordinary interface calling method. Thanks to Dubbo's dynamic proxy mechanism, this all feels like local calls.

// 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/client/Application.java

// 获取订阅到的 Stub
GreetingsService service = reference.get();
// 像普通的 java 接口一样调用
String message = service.sayHi("dubbo");

3. Can multiple servers be deployed?

Yes, this section will demonstrate how to start a server cluster .

1) To start a registration center, you can refer to the tutorial in Section 3 of the hands-on practice

2) Modify the data returned by the service provider and let the first started service provider returnhi, dubbo. I am provider 1.

Modify 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.javaline 25 of the file to look like this.

// 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java

package org.apache.dubbo.samples.provider;

import org.apache.dubbo.samples.api.GreetingsService;

public class GreetingsServiceImpl implements GreetingsService {
    @Override
    public String sayHi(String name) {
        return "hi, " + name + ". I am provider 1.";
    }
}

3) To start the first service provider, you can refer to the tutorial in Section 4 of the hands-on practice

4) Modify the data returned by the service provider and let the second started service provider returnhi, dubbo. I am provider 2.

Modify 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.javaline 25 of the file to look like this.

// 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java

package org.apache.dubbo.samples.provider;

import org.apache.dubbo.samples.api.GreetingsService;

public class GreetingsServiceImpl implements GreetingsService {
    @Override
    public String sayHi(String name) {
        return "hi, " + name + ". I am provider 2.";
    }
}

4) To start the second service provider, you can refer to the tutorial in Section 4 of the hands-on practice

5) To start the service consumer, you can refer to the tutorial in Section 5 of the hands-on practice . If you start the consumer multiple times, you can see that the returned results are different.

Dubbo-samples also provides a consumer application that will initiate calls periodically org.apache.dubbo.samples.client.AlwaysApplication, which can be started with the following command.

Windows:
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.client.AlwaysApplication"

Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.client.AlwaysApplication"

After startup, you can see logs similar to the following. The consumer will randomly call different service providers, and the returned results are also the results of the remote service provider.

Sun Jan 29 11:23:37 CST 2023 Receive result ======> hi, dubbo. I am provider 1.
Sun Jan 29 11:23:38 CST 2023 Receive result ======> hi, dubbo. I am provider 2.
Sun Jan 29 11:23:39 CST 2023 Receive result ======> hi, dubbo. I am provider 2.
Sun Jan 29 11:23:40 CST 2023 Receive result ======> hi, dubbo. I am provider 1.
Sun Jan 29 11:23:41 CST 2023 Receive result ======> hi, dubbo. I am provider 1.

4. Is this use case complex?

No, Dubbo only requires simple configuration to achieve stable and efficient remote calls.

The following is a simple example of a service provider that can be started by defining several configurations.

// 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/Application.java

// 定义所有的服务
ServiceConfig<GreetingsService> service = new ServiceConfig<>();
service.setInterface(GreetingsService.class);
service.setRef(new GreetingsServiceImpl());

// 启动 Dubbo
DubboBootstrap.getInstance()
        .application("first-dubbo-provider")
        .registry(new RegistryConfig(ZOOKEEPER_ADDRESS))
        .protocol(new ProtocolConfig("dubbo", -1))
        .service(service)
        .start();

The following is a simple example of a service consumer. By defining several configurations, the corresponding proxy object can be obtained after startup. After that, the user does not need to be aware of the complex implementation behind this object. Everything only needs to be the same as local calls .

// 1-basic/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/client/Application.java

// 定义所有的订阅
ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();
reference.setInterface(GreetingsService.class);

// 启动 Dubbo
DubboBootstrap.getInstance()
        .application("first-dubbo-consumer")
        .registry(new RegistryConfig(ZOOKEEPER_ADDRESS))
        .reference(reference)
        .start();

// 获取订阅到的 Stub
GreetingsService service = reference.get();
// 像普通的 java 接口一样调用
String message = service.sayHi("dubbo");

More

This use case introduces the basic process of an RPC remote call and simulates a microservice deployment architecture by starting three nodes: registration center, service provider, and service consumer.

In the next tutorial, we will explain the configurations of service providers and service consumers, and tell you how to build a microservice application from scratch.

Feel free to star Dubbo at https://github.com/apache/dubbo .

Microsoft launches new "Windows App" .NET 8 officially GA, the latest LTS version Xiaomi officially announced that Xiaomi Vela is fully open source, and the underlying kernel is NuttX Alibaba Cloud 11.12 The cause of the failure is exposed: Access Key Service (Access Key) exception Vite 5 officially released GitHub report : TypeScript replaces Java and becomes the third most popular language Offering a reward of hundreds of thousands of dollars to rewrite Prettier in Rust Asking the open source author "Is the project still alive?" Very rude and disrespectful Bytedance: Using AI to automatically tune Linux kernel parameter operators Magic operation: disconnect the network in the background, deactivate the broadband account, and force the user to change the optical modem
{{o.name}}
{{m.name}}

Ich denke du magst

Origin my.oschina.net/u/6214966/blog/7020319
Empfohlen
Rangfolge