學習來源
本文參考至
【【演示】SpringBoot+Dubbo+ZooKeeper 快速搭建】 https://www.bilibili.com/video/BV1XK411f7jL/?share_source=copy_web&vd_source=b38b089ffe2c29ef61bebcff0f458ed8
前言
入職了一年來,Dubbo一直是一個我很想學習,但又不知道何從下手的工具,這一年來斷斷續續看了
【黑马程序员Dubbo快速入门,Java分布式框架dubbo教程】 https://www.bilibili.com/video/BV1VE411q7dX/?share_source=copy_web&vd_source=b38b089ffe2c29ef61bebcff0f458ed8
【千锋教育Java微服务架构教程(SpringBoot+Dubbo+Zookeeper)】 https://www.bilibili.com/video/BV18b411v7Xr/?share_source=copy_web&vd_source=b38b089ffe2c29ef61bebcff0f458ed8
之類之類的教學,但這些影片要嘛過時,要嘛前面有一大串複雜繁瑣的配置,我往往在一開始的時候就卡關,最後不了了知,想去看dubbo官方的文件,明明上面寫快速入門,結果文件也是過時,東缺西缺的,根本沒辦法用,後來還遇到ZooKeeper無法安裝在M1的問題,霎那間真的萬念俱灰,直到最近又燃起熱情,決定再來學一次。
開頭還是一樣的卡關,正當我想放棄的時候我點開了【演示】SpringBoot+Dubbo+ZooKeeper 快速搭建,心裡想說死馬當活馬醫,沒想到居然真的成功了!
原始碼
https://github.com/Hoxton019030/Dubbo-Practice
不確定日後會不會有其他的操作,可以checkout到34205e76be46971e92eac34d6bb1a4cbb26bb435在操作會比較保險。
教學
Zookeeper配置
首先安裝zoo-keeper,安裝的方式請參考千迴百轉的ZooKeeper安裝。把zooKeeper啟動起來,會看到這樣子的畫面
接著開啟IDEA,建立SpringBoot專案,由於Dubbo是個分布式的RPC框架,所以我們這邊選擇創建兩個模塊,dubbo-provider dubbo-cousumer來學習,會比較貼近真實的開發情況,創建多模塊要記得將父模塊的src資料夾刪除,否則會有問題,可以參考SideProject開發(2)-台股監控Web,裡面有我血淚的教訓
我們先將相關的Maven依賴引入,由於我比較懶,我把大部分的依賴都寫在父模塊的POM中
Maven配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
| <?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>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-practice</name>
<description>dubbo-practice</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.2.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
兩個子模塊的POM我也提供一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| <?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>com.example</groupId>
<artifactId>dubbo-practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>dubbo-provider</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| <?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>com.example</groupId>
<artifactId>dubbo-practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<description>dubbo-consumer</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<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>
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
|
Properties配置類
1
2
3
4
5
| spring.application.name=dubbo-consumer
server.port=8081
dubbo.application.name=consumer-app
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.port=20881
|
1
2
3
4
5
| spring.application.name=dubbo-provider
server.port=8082
dubbo.application.name=provider-app
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.port=20880
|
程式配置
我們首先先寫dubbo-provider的內容,寫兩個class,一個是interface,一個去實現它,會回傳一個Hello出去
其中@DubboService這個就相當於將這個class的調用註冊到zookeeper中(這功能等等會教)
由於是provider,所以要記得在啟動類上加上
@EnableDubbo
接著來寫dubbo-consumer,我們寫一個controller就好,用來接收helloService回傳的值,並返回出去,這樣就可以了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| package com.example.dubboconsumer.controller;
import com.example.dubboprovider.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class HelloController {
@DubboReference
private HelloService helloService;
@GetMapping("/hello")
public String getHello(){
log.info("呼叫控制器");
return helloService.sayHello();
}
}
|
寫法就像這樣,不是使用@Autowired來註冊bean,而是使用@DubboReferece來調用RPC,接著將兩個模塊啟動後就可以了!非常簡單又輕便的Dubbo就完成了!
使用ZooKeperCli查看註冊的服務
bin/zkCli.cmd的點開
就可以看到目前註冊的服務了