构建 Zookeeper + Dubbo + Spring Boot 的分布式调用项目(二)

本案例代码下载,在我github上面:https://github.com/hemin1003/dubbo-spring-study

大致上实现代码一样,但有小些不同


要注意一个地方是:

1. 两个项目都要集成spring-boot-starter-web依赖,不然启动不了

2. 暴露的接口服务package路径都要一致,不然会失败


以下为转载内容:

一、使用 spring Initializr 构建 Dubbo 服务消费者 dubbo-consumer 项目

1. 登录 http://start.spring.io/ 填写如下信息后点击 “Generate Project” 按钮,得到 dubbo-consumer 项目骨架


初始 dubbo-provider 项目结构如下:



2. 为 dubbo-provider 项目添加依赖:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 格式化对象,方便输出日志 -->  
  2. <dependency>  
  3.     <groupId>com.alibaba</groupId>  
  4.     <artifactId>fastjson</artifactId>  
  5.     <version>1.1.41</version>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>com.alibaba</groupId>  
  9.     <artifactId>dubbo</artifactId>  
  10.     <version>2.4.10</version>  
  11.     <exclusions>  
  12.         <exclusion>  
  13.             <artifactId>spring</artifactId>  
  14.             <groupId>org.springframework</groupId>  
  15.         </exclusion>  
  16.     </exclusions>  
  17. </dependency>  
  18. <dependency>  
  19.     <groupId>org.apache.zookeeper</groupId>  
  20.     <artifactId>zookeeper</artifactId>  
  21.     <version>3.4.6</version>  
  22.     <exclusions>  
  23.         <exclusion>  
  24.             <artifactId>slf4j-log4j12</artifactId>  
  25.             <groupId>org.slf4j</groupId>  
  26.         </exclusion>  
  27.     </exclusions>  
  28. </dependency>  
  29. <dependency>  
  30.     <groupId>com.github.sgroschupf</groupId>  
  31.     <artifactId>zkclient</artifactId>  
  32.     <version>0.1</version>  
  33. </dependency>  


3. 引入接口(此处偷懒了,没有将接口打成 jar 包后导入,而是直接把接口文件添加到项目下,强烈不建议此种做法)

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.shawearn.dubbo.remote;  
  2. /** 
  3.  * 测试远程调用的接口; 
  4.  * <p/> 
  5.  * Created by Shawearn on 2017/2/14. 
  6.  */  
  7. public interface TestService {  
  8.     String sayHello(String name);  
  9. }  


4. 定义测试用的 Controller 类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.shawearn.dubbo.consumer.controller;  
  2. import com.alibaba.fastjson.JSONObject;  
  3. import com.shawearn.dubbo.remote.TestService;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.ResponseBody;  
  9. /** 
  10.  * 测试用的 Controller 类; 
  11.  * <p/> 
  12.  * Created by Shawearn on 2017/2/14. 
  13.  */  
  14. @Controller  
  15. public class TestController {  
  16.     @Autowired  
  17.     TestService testService;  
  18.     /** 
  19.      * 测试 JSON 接口; 
  20.      * 
  21.      * @param name 名字; 
  22.      * @return 
  23.      */  
  24.     @ResponseBody  
  25.     @RequestMapping("/test/{name}")  
  26.     public JSONObject testJson(@PathVariable("name") String name) {  
  27.         JSONObject jsonObject = new JSONObject();  
  28.         String testStr = testService.sayHello(name);  
  29.         jsonObject.put("str", testStr);  
  30.         return jsonObject;  
  31.     }  
  32. }  


5. 在 resource/ 下添加 consumers .xml 配置文件,用于向 zookeeper 注册中心注册服务

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.        http://code.alibabatech.com/schema/dubbo  
  8.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  9.     <!-- 配置可参考 http://dubbo.io/User+Guide-zh.htm -->  
  10.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  11.     <dubbo:application  name="dubbo-consumer" owner="dubbo-consumer"/>  
  12.     <!-- 定义 zookeeper 注册中心地址及协议 -->  
  13.     <dubbo:registry protocol="zookeeper" address="192.168.10.41:4183" client="zkclient" />  
  14.     <!-- 生成暴露的远程服务代理,可以和本地 bean 一样使用 testService -->  
  15.     <dubbo:reference id="testService" interface="com.shawearn.dubbo.remote.TestService"/>  
  16. </beans>  


6. DubboConsumerApplication 中使用 consumers.xml 配置文件;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.shawearn.dubbo.consumer;  
  2. import org.springframework.boot.SpringApplication;  
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  4. import org.springframework.context.annotation.ImportResource;  
  5. @SpringBootApplication  
  6. @ImportResource(value = {"classpath:consumers.xml"}) // 使用 consumers.xml 配置;  
  7. public class DubboConsumerApplication {  
  8.     public static void main(String[] args) {  
  9.         SpringApplication.run(DubboConsumerApplication.class, args);  
  10.     }  
  11. }  


7. application.properties 中指定项目启动时占用的端口号:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. server.port=8012  


8. 此时 dubbo-consumer 项目结构如下:



9. 启动 dubbo-consumer 项目,可以通过 Dubbo 服务控制台看到当前项目已经在消费 Dubbo 服务:



10. 通过浏览器访问 http://192.168.10.41:8012/test/shawearn (此路径为 dubbo-consumer 项目的 WEB 访问路径),可以看到如下页面,证明 dubbo-consumer 已经成功远程调用了 dubbo-provider 项目提供的 Dubbo 服务;



本文示例项目代码可从此地址下载:下载地址


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贺佬湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值