Apache Dubbo is a high-performance, lightweight, open source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.
Note, Apache Dubbo, not Alibaba Dubbo. Simply put, Alibaba handed Dubbo over to the Apache open source community for maintenance. See dubbo – spring – the boot – project
See the Spring Boot series for the older version of Dubbo: Integrating Alibaba Dubbo
I. Illustration of examples in this paper
1.1 Framework Version
Dubbo
version
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.5</version>
</dependency>
Copy the code
Spring Boot
version
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1. RELEASE</version>
<relativePath/>
</parent>
Copy the code
1.2 Module Relationship
- The root of engineering
order
: Management of engineering information; - The son of engineering
order-api
Definition:RPC
Service interfaces, parameters, and result sets for response results; - The son of engineering
order-provider
:RPC
Service provider; - The son of engineering
order-consumer
:RPC
On the consumer side of the service, what actually happens during development is that other services call this orderRPC
service
Second, root project
2.1 Creating a Projectorder
In order to distinguish it from the old version of Alibaba’s Dubbo project, THE file name is apache-Dubbo-demo and the name of Maven project is Order.
The main purpose of this project is to define the project information, manage the dependency versions of the entire project, etc., so the SRC directory is not needed.
2.2 pom.xml
and
are used to manage dependencies in the root project.
<dependencyManagement>
Declare global dependencies that inherit only when a subproject specifies a reference.<dependencies>
: Declares global dependencies that subprojects automatically inherit directly.
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<! -- Parent reference -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1. RELEASE</version>
<relativePath/>
</parent>
<! -- Basic information -->
<groupId>cn.van.order</groupId>
<artifactId>order</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>Apache Dubbo root project</description>
<! - configuration - >
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.5</dubbo.version>
<zookeeper.version>3.4.14</zookeeper.version>
</properties>
<! -- Subproject -->
<modules>
<module>order-api</module>
<module>order-provider</module>
<module>order-consumer</module>
</modules>
<! Declare global dependencies (subprojects need to display references to inherit dependencies) -->
<dependencyManagement>
<dependencies>
<! -- dubo-start dependency -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<! -- ZooKeeper registry client is a curator client.
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<! Declare global dependencies (subitems do not need to display references, automatically inherit dependencies) -->
<dependencies>
<! -- Spring Boot dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<! -- Package plugin -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
Three,order-api
3.1 Project Dependency
No more dependencies, so it’s easy.
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.van.order</groupId>
<artifactId>order</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</parent>
<groupId>cn.van.order</groupId>
<artifactId>order-api</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>Dubbo Public Project</description>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
3.2 packagingRPC
The result set
- Start by encapsulating a return code enumeration class
ResultCodeEnum.java
public enum ResultCodeEnum {
/*** generic part 100-599 ***/
// Successful request
SUCCESS(200."successful"),
/*** The error code can be separated by different modules, for example: ***/
// The range from 1000 to 1999 indicates an error in the user module
// The range from 2000 to 2999 indicates an order module error
// The range from 3000 to 3999 indicates that the product module is faulty
/ /...
ORDER_NOT_FOUND(2000."order not found"),;/** * Response status code */
private Integer code;
/** * Response message */
private String message;
ResultCodeEnum(Integer code, String msg) {
this.code = code;
this.message = msg;
}
public Integer getCode(a) {
return code;
}
public String getMessage(a) {
return message;
}
public void setMessage(String message) {
this.message = message; }}Copy the code
- Encapsulate an RPC response result set
RpcResult.java
public class RpcResult <T> implements Serializable {
/** * Whether the response is successful */
private Boolean success;
/** * Response status code */
private Integer code;
/** * Response data */
private T data;
/** * error message */
private String message;
// The constructor starts
/** * No parameter constructor (the constructor is private and cannot be created directly externally) */
private RpcResult(a) {
this.code = 200;
this.success = true;
}
/** * parameter constructor *@param obj
*/
private RpcResult(T obj) {
this.code = 200;
this.data = obj;
this.success = true;
}
/** * parameter constructor *@param resultCode
*/
private RpcResult(ResultCodeEnum resultCode) {
this.success = false;
this.code = resultCode.getCode();
this.message = resultCode.getMessage();
}
// The constructor ends
/** * General return success (no result returned) *@param <T>
* @return* /
public static<T> RpcResult<T> success(a){
return new RpcResult();
}
/** * returns success *@param data
* @param <T>
* @return* /
public static<T> RpcResult<T> success(T data){
return new RpcResult<T>(data);
}
/** * General return failed *@param resultCode
* @param <T>
* @return* /
public static<T> RpcResult<T> failure(ResultCodeEnum resultCode){
return new RpcResult<T>(resultCode);
}
public Boolean getSuccess(a) {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public Integer getCode(a) {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public T getData(a) {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMessage(a) {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString(a) {
return "RpcResult{" +
"success=" + success +
", code=" + code +
", data=" + data +
", message='" + message + '\' ' +
'} '; }}Copy the code
3.3 Writing aRPC
interface
public interface OrderDubboService {
RpcResult<OrderDomain> getOrder(a);
}
Copy the code
The entity orderDomain.java is quite simple, see the Github repository for details.
Four,order-provider
This subproject is a service-class project that registers the interface service to the ZooKeeper registry for the consumer to retrieve.
4.1 Project Dependency
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.van.order</groupId>
<artifactId>order</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</parent>
<groupId>cn.van.order</groupId>
<artifactId>order-provider</artifactId>
<version>1.0 the SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>Dubbo service provider</description>
<dependencies>
<dependency>
<groupId>cn.van.order</groupId>
<artifactId>order-api</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<! -- ZooKeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
4.2 Service Implementation Interface
@Service
public class OrderDubboServiceImpl implements OrderDubboService {
@Override
public RpcResult<OrderDomain> getOrder(a) {
return RpcResult.success(new OrderDomain(1.10086, LocalDateTime.now())); }}Copy the code
Note: @service is a dubbo package annotation, not a Spring annotation.
4.3 Project Configuration
dubbo
The configuration is directly useddubbo
, no longer toSpring
At the beginning.base-packages
: Specifies the path where the interface implementation resides.
Port: 7777 Spring: application: name: order-provider # dubbo Order-provider scan: order-provider scan: order-provider scan: order-provider Cn. Van. Order. Service. Impl # registry registry information: address: zookeeper: / / 127.0.0.1:2181 protocol: # agreement name name: Dubbo # Protocol port: 20880Copy the code
Five,order-consumer
This sub-project is a consumption project, such as goods module, finance module, and so on.
5.1 Project Dependency
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.van.order</groupId>
<artifactId>order</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</parent>
<groupId>cn.van.order</groupId>
<artifactId>order-consumer</artifactId>
<version>1.0 the SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>Dubbo consumers</description>
<dependencies>
<dependency>
<groupId>cn.van.order</groupId>
<artifactId>order-api</artifactId>
<version>1.0.0 - the SNAPSHOT</version>
</dependency>
<! -- Web project dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Dubbo dependency -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<! Dubbo's ZooKeeper dependency -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
5.2 Test Interfaces
Simulate an interface to get order details.
@RestController
@RequestMapping("/order")
public class OrderConsumerController {
@Reference
OrderDubboService orderDubboService;
@GetMapping("getOrder")
public RpcResult getOrder(a) {
returnorderDubboService.getOrder(); }}Copy the code
Note: @Reference introduces the Dubbo interface, so it is a Dubbo annotation.
5.3 Configuration File
Server: port: 7000 Spring: Application: name: order-consumer # dubbo The order - consumer registry: address: zookeeper: / / 127.0.0.1:2181Copy the code
Six, test,
The Dubbo service is set up if the order-consumer test interface successfully requests data.
6.1 startzookeeper
We chose ZooKeeper as the registry, so we need to start it before starting the project.
6.2 dubbo-admin
Dubbo-admin You can observe whether the order-provider successfully registers the interface. For details about how to install the order-provider, see Apache /dubbo-admin
Default port: 8080.
6.3 startdubbo-provider
Dubo-admin: the interface OrderService has been successfully registered with ZooKeeper as follows:
If the excuse is successfully registered in the registry, the Dubbo-Provider is successfully registered.
6.4 startorder-cosumer
Start project, consumers in the browser request consumer interface: http://localhost:7000/order/getOrder, successful return data is as follows:
{
"success":true."code":200."data": {"id":1."orderNum":10086."gmtCreate":"The 2020-05-06 T11:59:45. 535"
},
"message":null
}
Copy the code
If the data provided by the order-provider is successfully requested, Dubbo is successfully set up.
Seven,
The complete code above has been uploaded to Github. If you need it, you can test it yourself. Welcome star!
7.1 Ancestral Secrets
Welcome to come here to make fun of issues and tell bloggers what to update next. Van firmly believes: 1 + 1 > 2!
7.2 Cultural Exchange
- Over the blog
- Dust blog – Gold digging
- Travelogue Blog – Blog Garden
- The Dust Blog -CSDN
- Github
The latest article, welcome to pay attention to: public number – dust blog; Exchange views, welcome to add: personal wechat