dubbo 初体验

112 阅读1分钟

dubbo 初体验

dubbo 是阿里开源的一个 RPC 框架,之后捐献给 Apache 开源基金会,并且成为 apache 基金会的顶级项目(dubbo apache 官网),如今社区依旧十分的活跃,得益于他的高性能的传输效率,优秀的设计,以及业内人士的维护如今在各大公司被使用(阿里,jd,饿了吗)

初始化项目

引入如 xml 配置

<dependencies>
    <!-- dubbo -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>3.2.14</version>
        <exclusions>
            <exclusion>
                <groupId>io.netty</groupId>
                <artifactId>netty-common</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-common</artifactId>
        <version>4.1.115.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.1.14</version>
    </dependency>
</dependencies>

配置文件

这里我们使用 spring 的 xml 配置文件

服务端配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-first-application"/>

    <!-- 网络通讯 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- registry to spring -->
    <bean name="helloService" class="com.rpc.dubbo.provider.service.HelloServiceImpl"/>

    <!-- server publish -->
    <dubbo:service interface="com.rpc.dubbo.service.HelloService" ref="helloService" />
</beans>
客户端配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-consumer-first-application">
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>

    <dubbo:protocol name="dubbo" port="-1" />

    <dubbo:reference id="helloService" interface="com.rpc.dubbo.service.HelloService" url="192.168.0.104:20880/com.rpc.dubbo.service.HelloService" />
</beans>

基础代码编写

服务端
package com.rpc.dubbo.provider;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.CountDownLatch;

/**
 * @author xl-9527
 * @since 2024/12/24
 **/
@Slf4j
public class ProviderStart {

    public void start() throws InterruptedException {
        final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
        ctx.start();
        new CountDownLatch(1).await();
    }
}
客户端
package com.rpc.dubbo.consumer;

import com.rpc.dubbo.entity.Hello;
import com.rpc.dubbo.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author xl-9527
 * @since 2024/12/25
 **/
@Slf4j
public class ConsumerStart {

    public void start() {
        final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
        Boolean hello = ctx.getBean("helloService", HelloService.class).hello(new Hello("你好", "dubbo-consumer"));
        log.info("调用结果 -> {}", hello);
    }
}

调用