本文背景簡述:
最近在學習公司的一套RPC框架,初步接觸的時候感受挺複雜的。可是知道其原理確定是和dubbo很類似的,畢竟都是RPC框架嘛,只是各自使用的範圍和使
用者羣體有些不一樣罷了。因而就去dubbo官網熟悉了其相關文檔和案例教程,而後結合網上大佬的一些資料,本身搭了個簡單的分佈式遠程調用案例。這裏
強調一個東西--記住官方文檔第一!!!下面分享一下個人案例代碼吧,也但願本案例能對在讀的讀者啓動一點幫助......html
先來一張簡單的架構圖吧,這個圖是本身使用processOn畫的(這裏推薦一波processOn線上做圖神器):
java
這個項目中只定義公共接口和實體,以供後面的provider工程項目和consumer工程項目使用(以下圖所示):
web
TeamService中的代碼也很簡單,以下:spring
package com.chandler.dubbo.service; import com.chandler.dubbo.entity.Team; public interface TeamService { Team getTeamInfo(String city); }
下載地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ ,至於怎麼配置和使用,能夠本身移駕度娘,上面不少,其實也比較簡單。數據庫
首先從下圖所示這裏建立一個springboot項目:
apache
建立後的工程大概以下圖所示,這個provider項目很簡單,就是一個實現了dubbo-common項目中的那個接口的service方法。
json
項目建立完了後咱們首先要把dubbo-common中的那個劃紅線的jar包手動添加到lib庫中,以備後面使用(正常來講咱們確定是將相似於dubbo-common這樣一個公共工程上傳到Maven公共鏡像倉庫或者是公司內部的是有個Maven鏡像倉庫中去,以備提服務的項目和消費服務的項目經過pom文件直接添加依賴就可使用。這裏咱們爲了簡單演示就直接手動添加算了)。手動添加步驟以下:
File-->Project Structure-->Modules-->Dependencies-->右側+號找到那個jar包-->點擊底部apply-->點擊ok
api
pom文件內容:瀏覽器
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.chandler.dubbo</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--boot依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入dubbo的依賴 --> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 引入zookeeper的依賴 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- slf4j依賴 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml中的內容:springboot
server.port=8081 spring.application.name=dubbo-provider #本地zookeeper註冊中心地址 spring.dubbo.registry=zookeeper://localhost:2181
MyServiceImpl代碼:(因爲這裏只是爲了簡單演示,因此就沒有使用數據庫什麼的了,直接在static代碼塊中使用集合存儲幾個對象以備後面查詢)
package com.chandler.dubbo.dubboprovider.service; import com.alibaba.dubbo.config.annotation.Service; import com.chandler.dubbo.entity.Team; import com.chandler.dubbo.service.TeamService; import org.springframework.stereotype.Component; import java.util.HashMap; @Component //這個service註解是dubbo中的,目的就是將這個服務暴露出去。 //裏面能夠指定代理版本,代理名稱還有不少的其餘可選配置項,dubbo官網的文檔中都有說明 @Service(version = "1.0.0",proxy = "TeamServer") public class MyserviceImpl implements TeamService { private static HashMap<String,Team> teamHashMap = new HashMap<>(16); static { Team team1 = new Team(); team1.setCity("chicago"); team1.setName("bulls"); team1.setTopPlayer("jordan&rose"); Team team2 = new Team(); team2.setTopPlayer("james&kobe"); team2.setName("lakers"); team2.setCity("loss angeles"); teamHashMap.put("chicago",team1); teamHashMap.put("lal",team2); } @Override public Team getTeamInfo(String s) { return teamHashMap.get(s); } }
最後是DubboProviderApplication啓動類:
package com.chandler.dubbo.dubboprovider; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); } }
到這一步就能夠啓動項目了,啓動項目後可使用dubbo的管理控制檯查看和管理服務,具體怎麼弄能夠參考官網文檔:
http://dubbo.apache.org/zh-cn/docs/admin/introduction.html,咱們接着在建立消費者項目。
同建立dubbo-provider同樣的步驟建立consumer項目,項目結構以下圖:
而後呢,也同dubbo-provider項目中同樣手動添加dubbo-common項目的jar包到lib庫中。
接下來就是pom文件內容,和provider項目中的pom文件幾乎同樣:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.chandler.dubbo</groupId> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入dubbo的依賴 --> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 引入zookeeper的依賴 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- slf4j依賴 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
TeamService類代碼:
package com.chandler.dubbo.dubboconsumer.service; import com.alibaba.dubbo.config.annotation.Reference; import com.chandler.dubbo.entity.Team; import org.springframework.stereotype.Service; @Service public class TeamService { @Reference(proxy = "TeamServer",version = "1.0.0") com.chandler.dubbo.service.TeamService teamService; public Team getTeam(String cityName){ return teamService.getTeamInfo(cityName); } }
TeamController類代碼:
package com.chandler.dubbo.dubboconsumer.controller; import com.chandler.dubbo.dubboconsumer.service.TeamService; import com.chandler.dubbo.entity.Team; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/api") public class TeamController { @Resource private TeamService teamService; @GetMapping("/getTeam") public Team getTeam(@RequestParam("cityName") String cityName){ return teamService.getTeam(cityName); } }
DubboConsumerApplication啓動類代碼:
package com.chandler.dubbo.dubboconsumer; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); } }
最後就能夠依次啓動zookeeper、dubbo-provider和dubbo-consumer項目了,而後可使用瀏覽器或者postman測試dubbo-consumer項目中的controller方法了。
我是一頭實習中的小菜雞,第一次寫博客,有不少不到位的地方,還請各位大佬直接評論!不管是讚揚仍是噴,本人都是能夠接受的。。。