因爲dubbo在2018年2月15日成爲 Apache 基金會孵化項目,因此再也不是阿里維護的一個項目了。而後彷佛就出現了斷層,網絡上大部分的文章都是對於阿里階段的dubbo開發的教程,致使尋找了好久才能找到apache的dubbo開發教程,因此想在這裏整理一下最新的dubbo與springboot的簡單整合。
首先咱們須要下載運行zookeeper:
http://mirror.bit.edu.cn/apac...
下載最新的版本,這裏須要知道的是項目中的zookeeper版本號最好和下載的版本一致。
咱們只須要下載解壓以後將conf裏面的zoo_sample.cfg複製重命名爲zoo.cfg後運行bin下面的zkServer.cmd便可。
若是咱們是在linux下面運行的話:linux
啓動ZK服務: sh zkServer.sh start 查看ZK服務狀態: sh zkServer.sh status 中止ZK服務: sh zkServer.sh stop 重啓ZK服務: sh zkServer.sh restart
新版本dubbo admin的搭建按照官方的說明很簡單就能夠搭建完成,具體步驟看官方文檔便可:
https://github.com/apache/dub...git
這裏我用的是IDEA進行開發構建的項目。github
填上項目名稱,而後點擊完成便可。web
咱們這裏用的是springboot模塊,先新建一個生產者模塊:spring
按照上述的順序再新建一個消費者模塊。數據庫
三個模塊建完以後:apache
咱們首先進行公共模塊的開發,也就是common模塊的開發。
新建一個實體類和一個service接口:springboot
public class User implements Serializable { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
public interface GetUserService { List<User> getUserList(String name); }
由於但願將common模塊做爲模板,生產者和消費者模塊都要繼承它,因此在它的pom文件裏面整合全部須要的包便可:網絡
<dependencies> <!-- apache的springboot dubbo依賴 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>io.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <!-- Zookeeper客戶端 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> </dependencies>
common模塊開發結束了。app
如今開發生產者模塊,只須要繼承common模塊,而後實現service接口便可。
這裏的Service是apache.dubbo的包,須要注意。
@Service public class GetUserServiceImpl implements GetUserService { @Override public List<User> getUserList(String name) { //此處沒有鏈接數據庫,因此就先生成幾條數據返回便可。 System.out.println(name); List<User> list = new ArrayList<>(); list.add(new User("小明",20)); list.add(new User("小強",21)); list.add(new User("小紅",22)); return list; } }
將application.properties更名爲application.yml,而後進行配置
server: port: 8082 #端口號 dubbo: application: name: provider #當前服務/應用的名字 # scan: # base-packages: com.zhouxiaoxi.provider.service.impl #開啓包掃描,可替代 @EnableDubbo 註解 monitor: protocol: registry #鏈接監控中心 protocol: name: dubbo #通訊協議 port: 20880 #接口 registry: address: 127.0.0.1:2181 #註冊中心的地址 protocol: zookeeper #註冊中心的協議
在這裏咱們仍是在啓動類上添加 @EnableDubbo 註解,由於比較簡便:
@EnableDubbo @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
而後咱們須要在pom文件裏面繼承common的模塊:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- common模塊 --> <dependency> <groupId>com.zhouxiaoxi</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
如今能夠啓動生產者,而後看dubbo admin界面就會發現一個提供者:
接下來咱們繼續開發消費者:
這裏咱們只是須要寫一個controller來調用一下這個接口就能夠了:
@RestController public class GetUserController { @Reference GetUserService getUserService; @RequestMapping("getUserList") public List<User> getUserList(@RequestParam("name") String name){ return getUserService.getUserList(name); } }
將application.properties更名爲application.yml,而後進行配置
server: port: 8081 #端口號 dubbo: application: name: consumer #當前服務/應用的名字 # scan: # base-packages: com.zhouxiaoxi.provider.service.impl #開啓包掃描,可替代 @EnableDubbo 註解 monitor: protocol: registry #鏈接監控中心 protocol: name: dubbo #通訊協議 port: 20880 #接口 registry: address: 127.0.0.1:2181 #註冊中心的地址 protocol: zookeeper #註冊中心的協議
在這裏咱們仍是在啓動類上添加 @EnableDubbo 註解,由於比較簡便:
@EnableDubbo @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
而後咱們須要在pom文件裏面繼承common的模塊而且添加web依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- web依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.9.RELEASE</version> </dependency> <!-- common模塊 --> <dependency> <groupId>com.zhouxiaoxi</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
如今能夠啓動消費者,而後看dubbo admin界面就會發現一個消費者:
訪問http://localhost:8081/getUserList?name=zhouxiaoxi便可看到相應的返回值。
至此咱們的springboot與apache dubbo的整合就結束了。
該項目已經上傳到github上面,你們能夠下載運行一下:
https://github.com/zhouzhaodo...