Dubbo入门实战

Dubbo是什么?

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

其核心部分包含:
  • 远程通讯
  • 集群容错
  • 自动发现:

Spring集成

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

新建一个DemoService.java类,提供者和消费者都要有这个Service,

public interface HelloService {

	String getName();
}

新建一个服务提供方实现接口,对服务消费方隐藏实现

public class HelloServiceImpl implements HelloService {

   public String getName() {
      return "rhwayfun";
   }

}
用Spring配置声明暴露服务:
新建一个dubbo-provider.xml文件对dubbo进行配置
<?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://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 ">   
        
    <!-- 具体的实现bean -->  
    <bean id="helloService" class="com.rhwayfun.service.impl.HelloServiceImpl" />  
    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="provider-dubbo"  />    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />     
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="29015" />  
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.rhwayfun.service.HelloService" ref="helloService" />  
</beans> 

消费者部分

另外新建一个工程:consumer-demo
新建一个dubbo-consumer.xml,对dubbo进行配置:

<?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://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:application name="consumer-dubbo" />
   
   <!-- 使用zookeeper注册中心订阅服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />   
    
    <!-- 生成远程服务代理,可以和本地bean一样使用HelloService -->
    <dubbo:reference id="helloService" interface="com.rhwayfun.service.HelloService" />
</beans>

新建一个测试类,当然具体用的时候会写一个专门的类使用IOC注入:

public class DubboConsumerDemo {

   public static void main(String[] args) {
      ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:application.xml"});
      ctx.start();
      
      // 获取远程服务代理
      HelloService helloservice = (HelloService)ctx.getBean("helloService");
      System.out.println(helloservice.getName());
   }
}

运行结果:

rhwayfun

demo地址:https://github.com/GHBghb/DubboDemo


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值