新建webapi项目
-
建项目时取消https支持,勾选docker支持,
-
Program.cs中注释下面语句,这样部署后才能访问Swagger
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
} -
添加Dapr.Client与Dapr.AspNetCore两个nuget包
-
修改Program.cs文件,增加dapr sdk支持
-
修改WeatherForecastController.cs文件
using Dapr;
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;namespace backend.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly DaprClient _daprClient;
public WeatherForecastController(DaprClient daprClient)
{
_daprClient = daprClient;
}[HttpGet(Name = "GetWeatherForecast")] public object Get() { return new { message = "Hello Dapr!" }; } [HttpPost(nameof(SaveStateValue))] public async Task SaveStateValue(StateModel stateModel) { await _daprClient.SaveStateAsync("statestore", stateModel.Key, stateModel.Value);//statestore } [HttpDelete(nameof(DeleteStateValue) + "/{stateKey}")] public async Task DeleteStateValue(string stateKey) { await _daprClient.DeleteStateAsync("statestore", stateKey); } [HttpGet("GetStateValue/{stateKey}")] public async Task<string> GetStateValue(string stateKey) { return await _daprClient.GetStateAsync<string>("statestore", stateKey); } [HttpGet(nameof(GetStateValueFromState) + "/{stateKey}")] public async Task<string> GetStateValueFromState([FromState("statestore", "stateKey")] StateEntry<string> stateEntry) { return await Task.FromResult(stateEntry.Value); } }
}
增加文件StateModel.cs
namespace backend.Controllers
{
public class StateModel
{
public string Key { get; set; }
public string Value { get; set; }
}
}
部署到k8s
dapr init -k
kubectl apply -f redis.yaml
kubectl apply -f statestore.yaml
kubectl apply -f dapr-front.yaml
#redis.yaml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: redis
version: v1
name: redis
#namespace: dapr-test1
spec:
replicas: 1
selector:
matchLabels:
app: redis
strategy:
type: Recreate
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:6-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis
name: redis
#namespace: dapr-test1
spec:
type: NodePort
ports:
- name: "data"
port: 6379
targetPort: 6379
selector:
app: redis
# statestore.yaml 文件
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
#namespace: default #dapr-test1
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
# dapr-front.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: dapr-deploy-front
labels:
service: front
spec:
replicas: 1
selector:
matchLabels:
service: front
template:
metadata:
labels:
service: front
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "front"
dapr.io/app-port: "8080"
#dapr.io/config: "dapr-config"
spec:
containers:
- name: daprfrontend
image: registry.cn-hangzhou.aliyuncs.com/shiningrise/statestore:v2
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: daprfrontend
labels:
service: front
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
nodePort: 30002
protocol: TCP
name: http
- port: 50001
targetPort: 50001
nodePort: 30041
protocol: TCP
name: dapr-grpc
selector:
service: front
验证是否成功
我是nat到虚拟机
测试接口,如果都能正常设置状态,获取状态,删除状态就OK了
遇到问题
我上面部署成功的是在vmare虚拟机k8s集群上测试成功了,但我在win11本地测试时死也成功不了,搞了好几天都搞不定
pod运行不了,知道的大佬指点一下啊。
《Blazor+Dapr+K8s微服务之状态管理》 本地运行能成功。
相关文件下载
https://files.cnblogs.com/files/shiningrise/statestore.zip?t=1716723380&download=true
常用命令
docker build -t registry.cn-hangzhou.aliyuncs.com/shiningrise/statestore:v1 -f backend/Dockerfile .
docker build -t daprfrontend -f backend/Dockerfile .
::kubectl delete all --all
::dapr uninstall --all
::dapr init -k
kubectl apply -f namespace.yaml
kubectl apply -f dapr-config.yaml
kubectl apply -f zipkin.yaml
kubectl apply -f redis.yaml
kubectl apply -f statestore.yaml
::kubectl apply -f dapr-statestore-test.yaml
:: kubectl delete -f dapr-front.yaml
kubectl apply -f dapr-front.yaml
pause