此示例是使用Furion
的.net框架+Dapr来实现的微服务,示例有简单的业务示例。含服务间的调用,订阅/发布。在K8S中实际运行起来,并测试正常。
我的环境是四台服务器
单机部署的docker服务为 172.31.119.226
k8s服务为
名称 | IP |
---|---|
master | 172.31.119.69 |
node1 | 172.31.119.70 |
node2 | 172.31.119.71 |
此教程都是在linux环境下进行的操作,无windows环境教程,可能部分操作需要自己FQ操作
现在安装Dapr Cli
环境
wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
## 运行dapr命令可以看到以下,则代表成功
dapr
如果下载不了,可以自己去GitHub
下载安装包,解压后拷贝到/usr/local/bin
目录即可。
运行dapr init
,等待初始化完成,然后验证输入dapr --version
,一定要CLI和runtime都有版本号才可以。
使用Dapr Cli安装
运行dapr init -k
,等待初始化完成,不过我都没成功过,用helm可以确保成功,且简单。
Helm安装
需要先安装Helm,具体百度,也很简单,就下载安装包,解压,拷贝到目录。mv linux-amd64/helm /usr/local/bin/helm
helm repo add dapr https://daprio.azurecr.io/helm/v1/repo
helm repo update
helm search repo dapr --devel --versions
helm upgrade --install dapr dapr/dapr \
--version=1.2 \ #根据上面查询的实际版本来
--namespace dapr-system \
--create-namespace \
--wait
Dockerfile文件
FROM mcr.microsoft.com/dotnet/aspnet:5.0 as bookservice
WORKDIR /app
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY todo.book.API/ /app
ENTRYPOINT ["dotnet", "todo.book.API.dll"]
FROM mcr.microsoft.com/dotnet/aspnet:5.0 as userservice
WORKDIR /app
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY todo.user.API/ /app
ENTRYPOINT ["dotnet", "todo.user.API.dll"]
打包docker
docker build . -t bookservice:latest --target bookservice --no-cache
docker build -t userservice:latest --target userservice .
dapr run --app-id TodoUserService --app-port 6001 --dapr-http-port 6011 -- dotnet todo.user.API.dll --urls "http://*:6001"
dapr run --app-id TodoBooksService --app-port 6002 --dapr-http-port 6012 -- dotnet todo.book.API.dll --urls "http://*:6002"
分别进入程序编译完成后目录,运行上面语句,等待成功后,在浏览器输入地址http://172.31.119.226:6002/index.html
运行dapr list
查看当前已经运行的dapr appid
也可以在服务器上收到调用接口
dapr invoke --app-id TodoUserService --method /api/users/books --data '{ "name": "string", "type": "string", "url": "string", "socre": 0,"author": "string", "pages": 0, "isbn": "string", "file": "string", "contentValidity": "string", "isFinish": true, "isShow": true, "userId": 0 }'
创建vi Daprdemo.yaml
文件
apiVersion: v1
kind: Namespace
metadata:
name: dapr-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: userservice
namespace: dapr-demo
labels:
app: userservice
spec:
replicas: 1
selector:
matchLabels:
app: userservice
template:
metadata:
labels:
app: userservice
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "todouserservice"
dapr.io/app-port: "80"
dapr.io/sidecar-liveness-probe-delay-seconds: "10"
dapr.io/sidecar-readiness-probe-delay-seconds: "10"
dapr.io/log-as-json: "true"
spec:
containers:
- name: userservice
image: 172.31.119.226:5000/userservice:latest
ports:
- containerPort: 80
imagePullPolicy: Always
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: bookservice
namespace: dapr-demo
labels:
app: bookservice
spec:
replicas: 2
selector:
matchLabels:
app: bookservice
template:
metadata:
labels:
app: bookservice
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "todobooksservice"
dapr.io/app-port: "80"
dapr.io/sidecar-liveness-probe-delay-seconds: "10"
dapr.io/sidecar-readiness-probe-delay-seconds: "10"
spec:
containers:
- name: bookservice
image: 172.31.119.226:5000/bookservice:latest
ports:
- containerPort: 80
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: userservice
namespace: dapr-demo
spec:
type: NodePort
selector:
app: userservice
ports:
- port: 80
targetPort: 80
nodePort: 30006
---
apiVersion: v1
kind: Service
metadata:
name: bookservice
namespace: dapr-demo
spec:
type: NodePort
selector:
app: bookservice
ports:
- port: 80
targetPort: 80
nodePort: 30007
执行脚本 kubectl apply -f Daprdemo.yaml
后完成
然后打开浏览器,输入http://172.31.119.69:30006/index.html
可查看userserivce
服务
查看bookservice
pod的运行日志kubectl logs -f userservice-77f467bb64-gpczv --all-containers=true -n dapr-demo
gitee地址