CentOS 8 安装 Apache Dubbo 指南
面向 CentOS 8 的 Apache Dubbo 3.2.20 集成指南:dnf 安装 JDK 与 Maven、坐标引入与验证,并以直连模式给出 provider、consumer 最小示例的编译、端口监听与调用回显。
CentOS 8 安装 Apache Dubbo 指南
Apache Dubbo 是高性能 Java RPC 框架,对外以 Java 库(jar)形式发布,没有独立守护进程。因此在服务器上「安装 Dubbo」的本质是:准备 JDK 与构建工具、引入 Maven 依赖、编写最小可运行的服务提供者(provider)与消费者(consumer)并验证调用链打通。本文以 org.apache.dubbo:dubbo:3.2.20 为例,在 CentOS 8 服务器上完成端到端直连(不依赖注册中心)调用实测。
一、前置条件
1.1 环境与账号
- 操作系统:CentOS Linux 8.4(x86_64),dnf 源可用
- 账号权限:
root(示例口令q1w2e3r4) - 网络:可访问 Maven 中央仓库
https://repo1.maven.org - 资源:至少 2GB 可用内存;本文示例 JVM 堆限定为
-Xms256m -Xmx512m - 端口:放行 TCP
20880(Dubbo 协议默认端口)与22222(QoS 运维控制台端口)
1.2 组件版本
- Dubbo:
org.apache.dubbo:dubbo:3.2.20(3.2 线最新版) - 关键传递依赖:
io.netty:netty-all:4.1.113.Final、com.alibaba:hessian-lite:3.2.13、com.alibaba.fastjson2:fastjson2:2.0.52、org.javassist:javassist:3.30.2-GA - 实测平台:CentOS Linux 8.4.2105 + OpenJDK
1.8.0_312+ Maven3.5.4
二、安装 JDK 与 Maven
2.1 使用 dnf 安装
dnf -y install java-1.8.0-openjdk-devel maven iproute
2.2 验证安装
java -version
mvn -version
实测回显:
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_312, vendor: Red Hat, Inc.
三、引入 Dubbo 依赖
3.1 创建最小工程
mkdir -p /root/dubbo-demo/src/main/java/com/example/dubbo
3.2 pom.xml
/root/dubbo-demo/pom.xml 关键内容如下:
<?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>
<groupId>com.example</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dubbo.version>3.2.20</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
</project>
3.3 确认依赖引入
cd /root/dubbo-demo && mvn -B dependency:tree | grep -E "org.apache.dubbo|netty-all|hessian|fastjson|javassist"
实测回显:
[INFO] \- org.apache.dubbo:dubbo:jar:3.2.20:compile
[INFO] +- io.netty:netty-all:jar:4.1.113.Final:compile
[INFO] +- org.javassist:javassist:jar:3.30.2-GA:compile
[INFO] +- com.alibaba:hessian-lite:jar:3.2.13:compile
[INFO] \- com.alibaba.fastjson2:fastjson2:jar:2.0.52:compile
四、编写最小示例(直连模式)
直连模式指 provider 主动暴露服务地址、consumer 直接指定该地址调用,两端均不经过注册中心(使用 RegistryConfig.NO_AVAILABLE,即 N/A),适合安装验证与联调。
4.1 服务接口与实现
src/main/java/com/example/dubbo/GreetingService.java:
package com.example.dubbo;
public interface GreetingService {
String sayHello(String name);
int add(int a, int b);
}
src/main/java/com/example/dubbo/GreetingServiceImpl.java:
package com.example.dubbo;
public class GreetingServiceImpl implements GreetingService {
public String sayHello(String name) {
return "Hello, " + name + " from Dubbo provider!";
}
public int add(int a, int b) {
return a + b;
}
}
4.2 Provider(ServiceConfig 直连暴露)
src/main/java/com/example/dubbo/ProviderApp.java:
package com.example.dubbo;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
public class ProviderApp {
public static void main(String[] args) throws InterruptedException {
ServiceConfig<GreetingService> service = new ServiceConfig<>();
service.setApplication(new ApplicationConfig("dubbo-demo-provider"));
service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
service.setProtocol(new ProtocolConfig("dubbo", 20880));
service.setInterface(GreetingService.class);
service.setRef(new GreetingServiceImpl());
service.export();
System.out.println("[provider] exported urls: " + service.getExportedUrls());
System.out.println("[provider] listening on dubbo://127.0.0.1:20880");
Thread.currentThread().join();
}
}
4.3 Consumer(ReferenceConfig 直连调用)
src/main/java/com/example/dubbo/ConsumerApp.java:
package com.example.dubbo;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
public class ConsumerApp {
public static void main(String[] args) {
ReferenceConfig<GreetingService> reference = new ReferenceConfig<>();
reference.setApplication(new ApplicationConfig("dubbo-demo-consumer"));
reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
reference.setInterface(GreetingService.class);
reference.setUrl("dubbo://127.0.0.1:20880");
GreetingService service = reference.get();
System.out.println("[consumer] sayHello(\"quzuo\") => " + service.sayHello("quzuo"));
System.out.println("[consumer] add(3, 4) => " + service.add(3, 4));
reference.destroy();
}
}
五、编译与运行(真实回显)
5.1 编译
cd /root/dubbo-demo
export MAVEN_OPTS="-Xms256m -Xmx512m"
mvn -B compile
实测回显:
[INFO] BUILD SUCCESS
5.2 启动 Provider
cd /root/dubbo-demo
export MAVEN_OPTS="-Xms256m -Xmx512m"
mvn -B exec:java -Dexec.mainClass=com.example.dubbo.ProviderApp > /tmp/provider.log 2>&1 &
实测日志:
INFO: [DUBBO] Dubbo Application[1.1](dubbo-demo-provider) is ready., dubbo version: 3.2.20, current host: 172.17.0.45
INFO: [DUBBO] Export dubbo service com.example.dubbo.GreetingService to url dubbo://172.17.0.45:20880/com.example.dubbo.GreetingService?anyhost=true&application=dubbo-demo-provider&interface=com.example.dubbo.GreetingService&methods=add,sayHello&release=3.2.20&side=provider ...
[provider] exported urls: [dubbo://172.17.0.45:20880/com.example.dubbo.GreetingService?...]
[provider] listening on dubbo://127.0.0.1:20880
5.3 确认端口监听
ss -lntp | grep -E "20880|22222"
实测回显:
LISTEN 0 4096 *:22222 *:* users:(("java",pid=667,fd=203))
LISTEN 0 4096 *:20880 *:* users:(("java",pid=667,fd=246))
20880 为 Dubbo 协议服务端口,22222 为 QoS 运维控制台端口,二者均由同一个 provider 进程监听。
5.4 Consumer 直连调用
cd /root/dubbo-demo
export MAVEN_OPTS="-Xms256m -Xmx512m"
mvn -B -q exec:java -Dexec.mainClass=com.example.dubbo.ConsumerApp
实测回显:
[consumer] sayHello("quzuo") => Hello, quzuo from Dubbo provider!
[consumer] add(3, 4) => 7
至此,CentOS 8 服务器上「接口编程 + Provider 直连暴露 + Consumer 直连调用」的端到端调用链已打通。
六、卸载
6.1 停止进程并清理工程
pkill -f exec:java
rm -rf /root/dubbo-demo
rm -rf ~/.m2/repository/org/apache/dubbo
6.2 卸载 JDK 与 Maven(可选)
dnf remove -y maven java-1.8.0-openjdk-devel
dnf autoremove -y
卸载后 20880 与 22222 端口随之释放,本机不再驻留任何 Dubbo 相关进程。