dubbo基本使用

使用Dubbo进行远程调用实现服务交互,它支持多种协议,如HessianHTTPRMIMemcachedRedis等等。由于Dubbo将这些协议的实现进行了封装了,无论是服务端(开发服务)还是客户端(调用服务),都不需要关心协议的细节,只需要在配置中指定使用的协议即可,从而保证了服务提供方与服务消费方之间的透明。

 

Dubbo的客户端和服务端有三种连接方式,分别是:广播,直连和使用zookeeper注册中心


服务端配置

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.taotao.manager.service.impl"/>

    <!-- 配置dubbo -->
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="taotao-manager-service"/>

    <!-- 使用multicast广播注册中心暴露服务地址 -->
   <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    <!--改用直连方式-->
    <!--<dubbo:registry address="N/A"/>-->
    <dubbo:registry protocol="zookeeper" address="192.168.37.161:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 声明需要暴露的服务接口 -->
    <!-- <dubbo:service interface="com.taotao.manager.service.ItemCatService" -->
    <!-- ref="itemCatServiceImpl" /> -->
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.taotao.manager.service.TestService" ref="testServiceImpl"/>
    <dubbo:service interface="com.taotao.manager.service.ItemCatService" ref="itemCatServiceImpl"/>
    <dubbo:service interface="com.taotao.manager.service.ItemDescService" ref="itemDescServiceImpl"/>
    <dubbo:service interface="com.taotao.manager.service.ItemService" ref="itemServiceImpl"/>
    <dubbo:service interface="com.taotao.manager.service.ContentService" ref="contentServiceImpl"/>
    <dubbo:service interface="com.taotao.manager.service.ContentCategoryService" ref="contentCategoryServiceImpl"/>
</beans>

从配置文件可以看出,首先第一步

 <!-- 提供方应用信息,用于计算依赖关系 -->

<dubbo:application name="taotao-manager-service"/>

第二步声明使用的通讯方式,和端口

<dubbo:protocol name="dubbo" port="20880"/>

通讯方式有三种 

广播

<dubbo:registry address="multicast://224.5.6.7:1234"/>

直连

<dubbo:registry address="N/A"/>

注册中心。

<dubbo:registry protocol="zookeeper" address="192.168.37.161:2181"/>

第三步声明提供的服务接口

<dubbo:service interface ="接口" ref="接口实现类">

三步服务端完成。

接着接口调用方

    <!-- 配置dubbo服务 -->
    <dubbo:application name="taotao-manager-web" />

    <!-- 使用广播 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<!--使用zookeeper-->
    <dubbo:registry protocol="zookeeper" address="192.168.37.161:2181"/>
    <!-- 声明要调用的服务,timeout是设置连接超时最长时间,如果不设置,超时时间默认是3秒 -->
    <dubbo:reference interface="com.taotao.manager.service.TestService" id="testService" timeout="1000000" />
    <dubbo:reference interface="com.taotao.manager.service.ItemCatService" id="itemCatService" timeout="1000000" />
    <dubbo:reference interface="com.taotao.manager.service.ItemService" id="itemService" timeout="1000000"/>
    <dubbo:reference interface="com.taotao.manager.service.ContentCategoryService" id="contentCategoryService"  timeout="1000000"/>
    <dubbo:reference interface="com.taotao.manager.service.ContentService" id="contentService"  timeout="1000000"/>

<dubbo:reference interface="com.taotao.manager.service.TestService" id="testService" timeout="1000000" />

表示根据interface属性获取到的服务程序实例对象放入spring容器,其中id做唯一标识作用。在java代码中需要注入这个服务实例的时候根据这个id注入。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用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支持多种注册中心和协议,可以根据实际需求进行配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值