新建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; }
}
}
# dapr自托管部署
修改launchSettings.json文件,端口改成5002,如下图
![](https://wxy-blog.oss-cn-hangzhou.aliyuncs.com/wxy-blog/2024/202405290847807.png)
```c#
dapr init
dapr run --app-id front --app-port 5002 dotnet run
访问http://localhost:5002/swagger/index.html
操作各个方法,都符合预期就对了
部署到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:
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微服务之状态管理》 本地运行能成功。
相关文件下载
待上传
常用命令
docker build -t registry.cn-hangzhou.aliyuncs.com/shiningrise/statestore:v1 -f backend/Dockerfile .
docker build -t daprfrontend:v1 -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
相关文章
作者
吴晓阳,微信号:shiningrise