構建 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;
}
}
<?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 服務;
本文示例項目代碼可從此地址下載:下載地址