- A+
前面我们说了在单机模式下和K8S集群下的Dapr实战,这次我们来看看如何在不使用K8S的情况下,在一个传统的虚拟机集群里来部署Dapr。
1.环境准备
我们准备两台centos7虚拟机
Dapr1:192.168.43.131
Dapr2:192.168.43.132
2.Dapr CLI安装
分别在Dapr1和Dapr2两台虚机上安装Dapr CLI
方式1 wget命令安装
wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
方式2 二进制方式手动安装
从https://github.com/dapr/cli/releases下载Dapr脚手架,并解压后放入/usr/local/bin目录
tar -xzvf dapr_linux_amd64.tar.gz mv dapr /usr/local/bin
3.Dapr初始化
在Dapr1和Dapr2两台虚机上初始化,不依赖Docker的情况下课通过指定--slim选项来启动
dapr init --slim
初始化好之后查看~/.dapr文件夹下的文件夹与文件,其中bin文件夹存放着daprd,dashboard,placement三个可执行文件
[root@localhost ~]# ll ~/.dapr 总用量 4 drwxrwxrwx 3 root root 64 10月 15 10:37 bin drwxrwxrwx 2 root root 29 10月 15 15:33 components -rw-r--r-- 1 root root 224 10月 15 18:34 config.yaml
[root@localhost ~]# ll ~/.dapr/bin/ 总用量 135632 -rwxrwxrwx 1 root root 94117888 10月 15 11:08 daprd -rwxrwxrwx 1 root root 30007296 10月 15 11:08 dashboard -rwxrwxrwx 1 root root 14761984 10月 15 11:07 placement drwxr-xr-x 3 root root 18 10月 15 11:08 web
4.部署项目
我们仍然采用之前的FrontEnd和BackEnd项目,在Dapr1与Dapr2两台机器上分别部署BackEnd,使其实现负载
发布FrontEnd和BackEnd,选择完全发布模式,这样我们不需要安装dotnet core运行时即可直接运行FrontEnd与BackeEnd,并选择linux-x64目标运行时
发布完成后,将BackEnd上传到Dapr1和Dapr2两台机器动;将FrontEnd上传到Dapr2
5.运行BackEnd和FrontEnd
在Dapr1上和Dapr2上启动BackEnd
chmod 777 /usr/local/src/backend/BackEnd dapr run --dapr-http-port 3511 --app-port 5000 --app-id backend /usr/local/src/backend/BackEnd
在Dapr2上启动FrontEnd
chmod 777 /usr/local/src/frontend/FrontEnd dapr run --dapr-http-port 3501 --app-port 5001 --app-id frontend /usr/local/src/frontend/FrontEnd
6.调用FrontEnd接口Dapr/ip验证
发现每次调用获取的ip不一致,已经通过mDNS实现负载
7.自定义组件
我们在Dapr1和Dapr2两台机器上的~/.dapr/components文件夹中分别新建文件statestore.yaml
apiVersion: dapr.io/v1alpha1 kind: Component metadata: name: statestore spec: type: state.redis version: v1 metadata: - name: redisHost value: 192.168.43.102:6379 - name: redisPassword value: "123456" - name: actorStateStore value: "true"
重新启动FrontEnd,调用api/State接口,发现组件设置成功
8.Actors使用
因为我们初始化时指定了--slim选项,这种模式下placement需要我们手动启动,我们启动FrontEnd中的~/.dapr/bin/placement
[root@localhost ~]# ~/.dapr/bin/placement INFO[0000] starting Dapr Placement Service -- version 1.4.3 -- commit a8ee30180e1183e2a2e4d00c283448af6d73d0d0 instance=localhost.localdomain scope=dapr.placement type=log ver=unknown INFO[0000] log level set to: info instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3 INFO[0000] metrics server started on :9090/ instance=localhost.localdomain scope=dapr.metrics type=log ver=1.4.3 INFO[0000] Raft server is starting on 127.0.0.1:8201... instance=localhost.localdomain scope=dapr.placement.raft type=log ver=1.4.3 INFO[0000] placement service started on port 50005 instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3 INFO[0000] Healthz server is listening on :8080 instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3 INFO[0001] cluster leadership acquired instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3 INFO[0001] leader is established. instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3 INFO[0004] Start disseminating tables. memberUpdateCount: 1, streams: 1, targets: 1, table generation: 1 instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3 INFO[0004] Completed dissemination. memberUpdateCount: 1, streams: 1, targets: 1, table generation: 1 instance=localhost.localdomain scope=dapr.placement type=log ver=1.4.3
重新启动FrontEnd与BackEnd,并调用api/ActorsClient/123,Actors环境启动成功
9.Consul名字解析组件
前面的负载我们使用了mDNS,官方还为我们提供了Consul名字解析组件,我们新建一个虚拟机Consul(192.168.43.133)来安装Consul
1.下载 wget https://releases.hashicorp.com/consul/1.3.0/consul_1.3.0_linux_amd64.zip 2.解压 unzip consul_1.3.0_linux_amd64.zip 3.拷贝到/usr/local/bin mv consul /usr/local/bin 4.验证 consul 5.启动 consul agent -dev -ui -node=consul-dev -client=192.168.43.133 6.访问192.168.43.133:8500
下面修改Dapr1和Dapr2机器上的~/.dapr/config.yaml配置,让其使用Consul来解析名字
apiVersion: dapr.io/v1alpha1 kind: Configuration metadata: name: daprConfig spec: nameResolution: component: "consul" configuration: client: address: "192.168.43.133:8500" selfRegister: true
重启FrontEnd与BackEnd并调用api/Dapr/ip接口,发现只能调用到后启动的BackEnd,无法实现负载
针对这个问题我像dapr官方提了issue,静等回复
https://github.com/dapr/components-contrib/issues/1199