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

6 篇文章 0 订阅
3 篇文章 0 订阅

构建 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.
 */
@Controller
public 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 服务;



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


  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值