一、註冊中心 Zookeeper
1.1 docker-compose 僞集羣部署
複製自 DockerHub,docker-compose-zookeeper.yaml
:
version: '3.1'
services:
zoo1:
image: zookeeper
restart: always
hostname: zoo1
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
zoo2:
image: zookeeper
restart: always
hostname: zoo2
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181
zoo3:
image: zookeeper
restart: always
hostname: zoo3
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
運行:
docker-compose -f docker-compose-zookeeper.yaml up -d
二、管理控制檯 Dubbo Admin
2.1 docker-compose 部署
僞集羣部署 zookeeper 時會創建一個網絡。教主的docker-compose-zookeeper.yaml
文件放在 /d/docker/zookeeper/
下,所以創建了一個名爲 zookeeper_default
的網絡。現在 dubbo-admin 需要訪問 zookeeper 僞集羣的容器的網絡,需要將 dubbo-admin 加入至該網絡。
docker network ls
version: '3'
services:
admin:
image: apache/dubbo-admin
ports:
- 8080:8080
environment:
- admin.registry.address=zookeeper://zoo1:2181
- admin.config-center=zookeeper://zoo1:2181
- admin.metadata-report.address=zookeeper://zoo1:2181
networks:
- zookeeper_default
networks:
zookeeper_default:
external: true
給容器傳遞環境變量時,以 docker-compose 的『服務名』代替 IP 地址:
運行:
docker-compose -f docker-compose-dubbo-admin.yaml up -d
訪問:
2.2 版本相關
DockerHub 裏的鏡像還是很久很久之前更新的,最新版本爲 0.1.0。但是 https://github.com/apache/dubbo-admin 目錄中卻又有 0.2.0 版本的 Dockerfile,然而經過自己構建測試(時間:2020-05-27)似乎並沒有什麼不同(界面稍微變好看了一點點)。
2.3 jar 包部署和前後端分離部署相關
根據官網描述:
QUOTE http://dubbo.apache.org/zh-cn/docs/admin/introduction.html
然而,如果直接進行 mvn clean package
會報一堆錯誤,解決方案爲跳過測試:
mvn clean package -Dmaven.test.skip=true
另外,可能還會因爲網絡原因,node 未下載完整可能也會報一堆錯誤,解決方案爲刪除下載不完整的包:
2.4 其它相關
dubbo-amdin 的 docker 鏡像默認沒有登錄密碼,而 GitHub 的源碼包是配置有登錄密碼的。右圖爲 Dockerfile 構建的源碼包中的配置文件:
三、Dubbo 簡單的 RPC 調用
3.1 Maven 創建接口模塊 hello-dubbo-demo-api
3.1.1 GAV 座標
<groupId>xyz.icefery.dubbodemo</groupId>
<artifactId>hello-dubbo-demo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
3.1.2 接口
package xyz.icefery.dubbodemo.hello.dubbo.demo.api;
public interface DemoService {
String sayHello();
}
3.1.3 安裝到本地倉庫
mvn clean install
3.2 SpringBoot 創建提供者模塊 hello-dubbo-demo-provider
3.2.1 GAV 座標
<groupId>xyz.icefery.dubbodemo</groupId>
<artifactId>hello-dubbo-demo-provider</artifactId>
<version>1.0.0-SNAPSHOT</version>
3.2.2 引入 dubbo 相關依賴
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3.2.3 引入接口依賴
<dependency>
<groupId>xyz.icefery.dubbodemo</groupId>
<artifactId>hello-dubbo-demo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
3.2.4 實現接口
使用 dubbo 的 @Service
註解:
package xyz.icefery.dubbodemo.hello.dubbo.demo.provider.api.impl;
import com.alibaba.dubbo.config.annotation.Service;
import xyz.icefery.dubbodemo.hello.dubbo.demo.api.DemoService;
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello() {
return "Hello Dubbo";
}
}
3.2.5 main 函數啓動 provider 容器
package xyz.icefery.dubbodemo.hello.dubbo.demo.provider;
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloDubboDemoProviderApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboDemoProviderApplication.class, args);
Main.main(args);
}
}
3.2.6 配置文件
application.yaml
:
spring:
application:
name: hello-dubbo-demo-provider
dubbo:
scan:
basePackages: xyz.icefery.dubbodemo.hello.dubbo.demo.provider.api
application:
id: hello-dubbo-demo-provider
name: hello-dubbo-demo-provider
qos-port: 22222
qos-enable: true
protocol:
id: dubbo
name: dubbo
port: 20880
status: server
registry:
id: zookeeper
address: zookeeper://127.0.0.1:2181
management:
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
health:
dubbo:
status:
defaults: memory
extras: load,threadpool
3.3 SpringBoot 創建消費者模塊 hello-dubbo-demo-consumer
3.3.1 GAV 座標
<groupId>xyz.icefery.dubbodemo</groupId>
<artifactId>hello-dubbo-demo-consumer</artifactId>
<version>1.0.0-SNAPSHOT</version>
3.3.2 dubbo 相關依賴
dubbo 相關依賴和提供者模塊的 dubbo 相關依賴相同。
3.3.3 web 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.3.4 RPC 調用
使用 dubbo 的 @Reference
註解:
package xyz.icefery.dubbodemo.hello.dubbo.demo.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.icefery.dubbodemo.hello.dubbo.demo.api.DemoService;
@RestController
public class DemoController {
@Reference(version = "1.0.0")
private DemoService demoService;
@RequestMapping(value = "/hello")
public String sayHi() {
return demoService.sayHello();
}
}
3.3.5 配置文件
application.yaml
:
spring:
application:
name: hello-dubbo-demo-consumer
server:
port: 9090
dubbo:
scan:
base-packages: xyz.icefery.dubbodemo.hello.dubbo.demo.consumer.controller
application:
id: hello-dubbo-demo-consumer
name: hello-dubbo-demo-consumer
registry:
id: zookeeper
address: zookeeper://127.0.0.1:2181
endpoints:
dubbo:
enabled: true
management:
server:
port: 9091
health:
dubbo:
status:
defaults: memory
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
endpoints:
web:
exposure:
include: "*"
3.4 啓動
- 確認註冊中心 zookeeper 正常運行
- 啓動服務提供者和服務消費者
- 訪問http://127.0.0.1:9090/hello
- 查看管理控制檯