Dubbo的基本使用

1、Dubbo概述

Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开
源Java RPC框架,可以和Spring框架无缝集成。

官网提供了用户文档与开发指南,基本对所有功能有大概的描述与使用方式
官方网址:https://dubbo.apache.org/zh/

2、Dubbo处理流程

以下处理流程图与节点说明均来自官方

在这里插入图片描述

节点说明

节点角色名称
Provider暴露服务的服务提供方
Consumer调用远程服务的服务消费方
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心
Container服务运行容器,负责启动、加载、运行服务提供者

3、服务注册中心Zookeeper

通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官方推荐使用Zookeeper作为服务注册中心。Zookeeper 是 Apache Hadoop 的子项目,作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。
Zookeeper安装及使用请参考:Zookeeper

4、Dubbo基本使用

4.1、基于注解模式

目录结构如下
在这里插入图片描述

  1. 接口协定
package com.ljfngu.service;

public interface HelloService {
    String sayHello(String name);
}
  1. 创建服务提供者
    引入Maven依赖(依赖版本在父工程中定义了,所以此处不再重新定义)
		<dependency>
            <groupId>com.ljfngu</groupId>
            <artifactId>service-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- Dubbo相关依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-rpc-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-remoting-netty4</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-hessian2</artifactId>
        </dependency>

编写服务实现类:

package com.ljfngu.service.impl;

import com.ljfngu.service.HelloService;
import org.apache.dubbo.config.annotation.Service;

// @Service为dubbo提供的Service注解,非Spring提供的
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "hello, " + name;
    }
}

编写dubbo-provider.properties配置文件

dubbo.application.name=service-provider-anno
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

编写服务提供者启动类:

package com.ljfngu;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;

public class DubboPureMain {
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
        // 让程序等待,输入任何字符程序结束
        System.in.read();
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "com.ljfngu.service.impl")
    @PropertySource("classpath:/dubbo-provider.properties")
    static class ProviderConfiguration{}
}

  1. 创建服务消费者
    首先引入与服务提供者相同依赖
    编写Component调用服务
package com.ljfngu.component;

import com.ljfngu.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

@Component
public class ConsumerComponent {
    @Reference
    private HelloService helloService;

    public String sayHello(String name){
        return helloService.sayHello(name);
    }
}

编写dubbo-consumer.properties配置文件

dubbo.application.name=service-consumer-anno
dubbo.registry.address=zookeeper://localhost:2181

编写消费者启动类

package com.ljfngu;

import com.ljfngu.component.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;

public class ConsumerPureMain {
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        ConsumerComponent consumerComponent = context.getBean(ConsumerComponent.class);
        while (true) {
            System.in.read();
            try {
                System.out.println(consumerComponent.sayHello("dubbo"));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "com.ljfngu.service")
    @ComponentScan(value = {"com.ljfngu.component"} )
    @PropertySource("classpath:/dubbo-consumer.properties")
    static class ConsumerConfiguration {}
}
  1. 测试
    先启动服务提供者,然后启动服务消费者,测试结果如下图:
    在这里插入图片描述

4.2、基于XML模式

  1. 引用上述api模块
  2. 创建服务提供者
    依赖与基于注解的服务提供者一致,新增如下依赖:
....
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-config-spring</artifactId>
</dependency>

编写与基于注解的服务实现类一致代码,去掉@Service注解即可

package com.ljfngu.service.impl;

import com.ljfngu.service.HelloService;

public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "hello, " + name;
    }
}

编写dubbo-provider.xml配置类

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

    <dubbo:application name="service-provider-xml"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <dubbo:protocol name="dubbo"/>

    <bean id="helloService" class="com.ljfngu.service.impl.HelloServiceImpl"/>

    <dubbo:service interface="com.ljfngu.service.HelloService" ref="helloService"/>

</beans>

编写服务提供者启动类

package com.ljfngu;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class DubboPureMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
        // 让程序等待,输入任何字符程序结束
        System.in.read();
    }

}
  1. 创建服务消费者
    依赖与服务创建者一致
    编写dubbo-consumer.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="service-consumer-xml"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <dubbo:reference interface="com.ljfngu.service.HelloService" id="helloService"/>

</beans>

编写服务消费者

package com.ljfngu;

import com.ljfngu.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
        HelloService helloService = context.getBean(HelloService.class);
        System.out.println(helloService.sayHello("dubbo"));
    }
}

  1. 测试
    先后启动服务提供者和服务消费者
    在这里插入图片描述
  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Dubbo进行Java RPC调用的步骤如下: 1. 首先需要在pom.xml文件中添加dubbo相关的依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.7.7</version> </dependency> ``` 2. 编写dubbo的配置文件dubbo.properties或dubbo.xml,配置dubbo的服务注册中心地址、协议等信息。 3. 编写服务提供者的代码,并在启动时将服务注册到注册中心上。 ```java @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello " + name; } } public class Provider { public static void main(String[] args) throws Exception { // 服务实现 HelloService helloService = new HelloServiceImpl(); // 配置服务提供者 ServiceConfig<HelloService> service = new ServiceConfig<>(); service.setInterface(HelloService.class); service.setRef(helloService); service.setVersion("1.0.0"); // 配置注册中心 RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127.0.0.1:2181"); // 启动服务 service.export(); } } ``` 4. 编写服务消费者的代码,在服务消费时通过dubbo的引用注解@Reference来获取服务。 ```java public class Consumer { public static void main(String[] args) { // 配置服务消费者 ReferenceConfig<HelloService> reference = new ReferenceConfig<>(); reference.setInterface(HelloService.class); reference.setVersion("1.0.0"); // 配置注册中心 RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127.0.0.1:2181"); reference.setRegistry(registryConfig); // 获取服务 HelloService helloService = reference.get(); // 调用服务 String result = helloService.sayHello("world"); System.out.println(result); } } ``` 以上就是使用Dubbo进行Java RPC调用的基本步骤,需要注意的是,Dubbo支持多种注册中心和协议,可以根据实际需求进行配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值