新建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,如下图
dapr init
dapr run --app-id front --app-port 5002 dotnet run
访问http://localhost:5002/swagger/index.html
操作各个方法,都符合预期就对了
部署到k8s
docker build -t daprfrontend:v1 -f backend/Dockerfile .
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"
spec:
containers:
- name: daprfrontend
image: daprfrontend:v1
imagePullPolicy: Never
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了
相关文件下载
常用命令
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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。