構建 Zookeeper + Dubbo + Spring Boot 的分散式呼叫專案(二)
阿新 • • 發佈:2019-02-07
一、使用 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 類
5. 在 resource/ 下新增 consumers.xml 配置檔案,用於向 zookeeper 註冊中心註冊服務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 服務;
本文示例專案程式碼可從此地址下載:下載地址