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

来源:互联网 发布:六十知天命 编辑:程序博客网 时间:2024/06/06 13:45

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

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

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


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



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

<!-- 格式化对象,方便输出日志 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.41</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.4.10</version><exclusions><exclusion><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.6</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency>


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

package com.shawearn.dubbo.remote;/** * 测试远程调用的接口; * <p/> * Created by Shawearn on 2017/2/14. */public interface TestService {    String sayHello(String name);}


4. 定义测试用的 Controller 类

package com.shawearn.dubbo.consumer.controller;import com.alibaba.fastjson.JSONObject;import com.shawearn.dubbo.remote.TestService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * 测试用的 Controller 类; * <p/> * Created by Shawearn on 2017/2/14. */@Controllerpublic class TestController {    @Autowired    TestService testService;    /**     * 测试 JSON 接口;     *     * @param name 名字;     * @return     */    @ResponseBody    @RequestMapping("/test/{name}")    public JSONObject testJson(@PathVariable("name") String name) {        JSONObject jsonObject = new JSONObject();        String testStr = testService.sayHello(name);        jsonObject.put("str", testStr);        return jsonObject;    }}


5. 在 resource/ 下添加 consumers.xml 配置文件,用于向 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: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://dubbo.io/User+Guide-zh.htm -->    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->    <dubbo:application  name="dubbo-consumer" owner="dubbo-consumer"/>    <!-- 定义 zookeeper 注册中心地址及协议 -->    <dubbo:registry protocol="zookeeper" address="192.168.10.41:4183" client="zkclient" />    <!-- 生成远程服务代理,可以和本地 bean 一样使用 testService -->    <dubbo:reference id="testService" interface="com.shawearn.dubbo.remote.TestService"/></beans>


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

package com.shawearn.dubbo.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportResource;@SpringBootApplication@ImportResource(value = {"classpath:consumers.xml"}) // 使用 consumers.xml 配置;public class DubboConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(DubboConsumerApplication.class, args);    }}


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

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 0
原创粉丝点击