dapr安装
Install the Dapr CLI
实践
计算器calculator微服务部署之dapr,对标 sidecar构架之istio-k8s部署

后端服务
- Addition: Go mux application (加法)
镜像:ghcr.io/dapr/samples/distributed-calculator-go:latest
容器内端口:6000
- Multiplication: Python flask application (乘法)
镜像:ghcr.io/dapr/samples/distributed-calculator-slow-python:latest
容器内端口:5001
- Division: Node Express application (除法)
镜像:ghcr.io/dapr/samples/distributed-calculator-node:latest
容器内端口:4000
- Subtraction: .NET Core application (减法)
镜像:ghcr.io/dapr/samples/distributed-calculator-csharp:latest
容器内端口:80
前端服务
- React
镜像:ghcr.io/dapr/samples/distributed-calculator-react-calculator:latest
容器内端口:8080

上图为该示例应用各个组件的组成和服务架构。
部署
我们可以随便查看一个微服务的部署清单,位于 deploy/ 目录下面,比如 go-adder.yaml:
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
| # go-adder.yaml apiVersion: apps/v1 kind: Deployment metadata: name: addapp labels: app: add spec: replicas: 1 selector: matchLabels: app: add template: metadata: labels: app: add annotations: dapr.io/enabled: "true" dapr.io/app-id: "addapp" dapr.io/app-port: "6000" dapr.io/config: "appconfig" spec: containers: - name: add image: ghcr.io/dapr/samples/distributed-calculator-go:latest env: - name: APP_PORT value: "6000" ports: - containerPort: 6000 imagePullPolicy: Always
|
1
| kubectl apply -f deploy/
|
部署完成后我们可以通过 dapr configurations 命令查看当前集群中的所有配置信息:
1 2 3
| ➜ dapr configurations -k -A NAMESPACE NAME TRACING-ENABLED METRICS-ENABLED AGE CREATED default appconfig true true 1m 2022-09-20 17:01.21
|
应用部署完成后查看 Pod 的状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| ➜ kubectl get pods NAME READY STATUS RESTARTS AGE addapp-84c9764fdb-72mxf 2/2 Running 0 74m calculator-front-end-59cbb6658c-rbctf 2/2 Running 0 74m divideapp-8476b7fbb6-kr8dr 2/2 Running 0 74m multiplyapp-7c45fbbf99-hrmff 2/2 Running 0 74m subtractapp-58645db87-25tg9 2/2 Running 0 62m ➜ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE addapp-dapr ClusterIP None <none> 80/TCP,50001/TCP,50002/TCP,9090/TCP 8m29s calculator-front-end LoadBalancer 10.110.177.32 <pending> 80:31701/TCP 8m29s calculator-front-end-dapr ClusterIP None <none> 80/TCP,50001/TCP,50002/TCP,9090/TCP 8m29s divideapp-dapr ClusterIP None <none> 80/TCP,50001/TCP,50002/TCP,9090/TCP 8m29s multiplyapp-dapr ClusterIP None <none> 80/TCP,50001/TCP,50002/TCP,9090/TCP 8m29s subtractapp-dapr ClusterIP None <none> 80/TCP,50001/TCP,50002/TCP,9090/TCP 8m29s zipkin NodePort 10.108.46.223 <none> 9411:32411/TCP 16m
|
部署完成后我们可以通过 calculator-front-end
这个 LoadBalancer 类型的 Service 去访问计算器的前端应用,我们这里分配的 EXTERNAL-IP 地址为 。因为本机没有接LB,,可以改成nodeport,或者直接port-forward一下
1 2 3 4
| kubectl port-forward service/calculator-front-end 8000:80
Forwarding from 127.0.0.1:8000 -> 8080 Forwarding from [::1]:8000 -> 8080
|
Curl 测试
operands.json
1
| {"operandOne":"52","operandTwo":"34"}
|
persist.json
1
| [{"key":"calculatorState","value":{"total":"54","next":null,"operation":null}}]
|
1 2 3 4 5 6
| curl -s http://localhost:8080/calculate/add -H Content-Type:application/json --data @operands.json curl -s http://localhost:8080/calculate/subtract -H Content-Type:application/json --data @operands.json curl -s http://localhost:8080/calculate/divide -H Content-Type:application/json --data @operands.json curl -s http://localhost:8080/calculate/multiply -H Content-Type:application/json --data @operands.json curl -s http://localhost:8080/persist -H Content-Type:application/json --data @persist.json curl -s http://localhost:8080/state
|
结果
1 2 3 4 5 6
| 86 18 1.5294117647058822 1768
{"operation":null,"total":"54","next":null}
|
前端服务调用后端服务
当前端服务器调用各自的操作服务时(见下面的server.js代码),它不需要知道它们所在的IP地址或它们是如何构建的。相反,它通过名称调用其本地 dapr side-car,它知道如何调用服务上的方法,利用平台的服务发现机制,在本例中为 Kubernetes DNS 解析。
${daprUrl}/${微服务名称}/method/${服务接口路由}
1
| 如:${daprUrl}/addapp/method/add
|
- addapp 我们的加法微服务
- add 我们的接口路由
1 2 3 4 5 6 7 8 9 10 11 12
| const daprUrl = `http://localhost:${daprPort}/v1.0/invoke`;
app.post('/calculate/add', async (req, res) => { const appResponse = await axios.post(`${daprUrl}/addapp/method/add`, req.body); return res.send(`${appResponse.data}`); });
app.post('/calculate/subtract', async (req, res) => { const appResponse = await axios.post(`${daprUrl}/subtractapp/method/subtract`, req.body); return res.send(`${appResponse.data}`); });
|
相关
Dapr 可观测性之分布式追踪
distributed-calculator