Dubbo简单入门实例

Dubbo简单入门实例——Dubbo

首先启动zookeeper注册中心,bin目录下双击运行zkServer.cmd , 末尾显示binding to port 0.0.0.0/0.0.0.0:2181表示zookeeper注册中心服务启动成功!

提供者端:

接口方法:TestDubboService( 路径:com.xxx.demo.service.TestDubboService)

  public interface TestDubboService {
      public String sayHello(String name);
  }

实现类:TestDubboServiceImpl(路径:com.xxx.demo.service.impl.TestDubboServiceImpl)

 public class TestDubboServiceImpl implements TestDubboService{
       public String sayHello(String name){
            return "Hello" + name;
       }
  }

提供者Dubbo配置文件的配置:spring-provider.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://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="dubbo-provider"/>
     <!-- 使用multicast广播注册中心暴露发现服务地址 -->
     <dubbo:registry address="zookeeper://192.168.1.108:2181" timeout="50000"/>
     <!-- 用dubbo协议在20880端口暴露服务 -->
     <dubbo:protocol name="dubbo" port="20880"/>
     <dubbo:service interface="com.xxx.demo.service.TestDubboService" group="tqs"             ref="testdubbo" />
     <bean id="testdubbo" class="com.xxx.demo.service.impl.TestDubboServiceImpl" />
  </beans>

消费者端:

接口方法:TestDubboService( 路径:com.xxx.demo.service.TestDubboService,接口方法名和路径 必须和提供者保持一致)

  public interface TestDubboService {
     public String sayHello(String name);
  }

消费者调用提供者提供的服务:StudyController.java

public class StudyController{

        @Resource
        private TestDubboService testDubboService;

        @RequestMapping("studyController.do")
        public String execute(){
            //dubbo消费者测试
            System.out.println("调用服务开始......");
            String hello = testDubboService.sayHello("tom");
            System.out.println(hello);
            System.out.println("调用服务结束......");
            return "study/day1";
        }

        @RequestMapping("studyController!queryForList.do")
        public String queryForList(){
        
        }
    }

消费者Dubbo配置文件:spring-comsumer.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://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="dubbo-consumer"/>
        <dubbo:consumer timeout="30000" retries="0"/>
        <dubbo:protocol name="dubbo" port="20880"/>
        <!--使用zookeeper广播注册中心暴露服务地址-->
        <dubbo:registry address="zookeeper://192.168.1.108:2181" check="false"/>
        <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
        <dubbo:reference id="testdubbo" interface="com.xxx.demo.service.TestDubboService" group="tqs" />
     </beans>

1.启动提供者项目
2.启动消费者项目
3.调用dubbo消费者测试方法,消费者控制台显示如下:

在这里插入图片描述
Dubbo服务调用成功!

本人第一次写博客,格式和内容质量请多多包涵,欢迎讨论指正~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值