Dubbo入门实例

1.   概述

 

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案

 

主要核心部件

 

Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.

RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能

Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。

 

 

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

 

 

2.   简单实例

demo文件下载:http://download.csdn.net/detail/ruishenh/7164585

首先maven项目增加dubbo的jar依赖,因为要用到zookeeper注册中心,也要依赖但是要去掉自带的log4j不然会默认的版本依赖jms-1.1.jar jmxtools-1.2.1.jar jmxri-1.2.1.jar等3个包,下载挺麻烦,当然如果个人已经在自己的仓库中有了就无所谓了。

 

<!-- dubbo -->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>dubbo</artifactId>
         <version>2.0.13</version>
      </dependency>
      <dependency>
         <groupId>org.apache.zookeeper</groupId>
         <artifactId>zookeeper</artifactId>
         <version>3.3.6</version>
         <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.16</version>
      </dependency>
 
 


 

Spring的依赖自己添加就好了

 

 

因为要增加zookeeper的注册管理,所以如果有可用的zookeeper就用可用的zookeeper,没有可以按照如下的安装去本地安装一个。

 http://blog.csdn.net/ruishenh/article/details/23180355

 

 

 

 

 

项目结构图



 

/gomeTest/src/main/resources/spring/dubbo-provider.xml

 

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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
       ">
         <dubbo:applicationname="hello-world-app" />
        
         <!--zookeeper注册中心 -->
         <dubbo:registryprotocol="zookeeper" address="10.57.41.19:2181" />
         <!--使用multicast广播注册中心暴露服务地址 -->
         <!--<dubbo:registry address="multicast://10.57.41.19:1234" />-->
         <dubbo:protocolname="dubbo" port="20880" />
         <dubbo:serviceinterface="com.ruishenh.dubbo.example.DemoService"
                   ref="demoService"/>       <!-- 和本地bean一样实现服务 -->
         <beanid="demoService"class="com.ruishenh.dubbo.example.DemoServiceImpl" />
</beans>

 

/gomeTest/src/main/resources/spring/dubbo-consumer.xml

 

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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
       ">    
         <!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
         <dubbo:applicationname="consumer-of-helloworld-app" />
         <!--zookeeper注册中心 -->
         <dubbo:registry  protocol="zookeeper"address="10.57.41.19:2181" /> 
         <!--使用multicast广播注册中心暴露的服务地址 -->
         <!--<dubbo:registryaddress="multicast://10.57.41.19:1234" /> -->
          <!-- 生成远程服务代理,可以和本地bean一样使用demoService-->
         <dubbo:referenceid="demoService"interface="com.ruishenh.dubbo.example.DemoService" />
</beans>


 

 

/gomeTest/src/main/java/com/ruishenh/dubbo/example/DemoService.java

 

package com.ruishenh.dubbo.example;
public interface DemoService {
  
   public voidsayHello();
   
   public String returnHello();
   
   public MsgInfo returnMsgInfo(MsgInfo info);
   
}


 

/gomeTest/src/main/java/com/ruishenh/dubbo/example/DemoServiceImpl.java

 

package com.ruishenh.dubbo.example;
 
public class DemoServiceImpl implements DemoService{
  
   public void sayHello() {
      System.out.println("hello world!");
   }
 
   public String returnHello() {
      return "hello world!";
   }
 
   public MsgInforeturnMsgInfo(MsgInfo info) {
      info.getMsgs().add("处理完毕");
      return info;
   }
}
 


 

/gomeTest/src/main/java/com/ruishenh/dubbo/example/LuncherProvider.java

 

package com.ruishenh.dubbo.example;
 
importorg.springframework.context.ApplicationContext;
importorg.springframewor
  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
基于dubbo的代码实例可以通过以下步骤来实现[^2]: 1. 首先,确保你已经安装了Zookeeper,并启动了Zookeeper服务。 2. 创建一个Maven项目,并在pom.xml文件中添加Dubbo和Zookeeper的依赖。 3. 创建一个接口,定义需要暴露的服务方法。 ```java public interface HelloService { String sayHello(String name); } ``` 4. 创建一个实现类,实现接口中定义的服务方法。 ```java public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 5. 在resources目录下创建一个dubbo配置文件dubbo-provider.xml,配置Dubbo的服务提供者。 ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="dubbo-demo-provider" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.example.HelloService" ref="helloService" /> <bean id="helloService" class="com.example.HelloServiceImpl" /> ``` 6. 创建一个启动类,用于启动Dubbo服务提供者。 ```java public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml"); context.start(); System.in.read(); } } ``` 7. 创建一个消费者项目,重复步骤2和3,创建一个接口和实现类。 8. 在resources目录下创建一个dubbo配置文件dubbo-consumer.xml,配置Dubbo的服务消费者。 ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="dubbo-demo-consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:reference id="helloService" interface="com.example.HelloService" /> ``` 9. 创建一个启动类,用于启动Dubbo服务消费者。 ```java public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.sayHello("World"); System.out.println(result); } } ``` 以上是一个基于Dubbo的简单代码实例,通过配置Dubbo的服务提供者和消费者,可以实现分布式的服务调用。你可以根据自己的需求进行扩展和定制。如果你想了解更多关于Dubbo的使用和原理,可以参考Dubbo的官方文档和源码[^1]。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值