微服務實踐k8s&dapr開發部署實驗(2)狀態管理

shiningrise發表於2024-05-26

新建webapi專案

image

  • 建專案時取消https支援,勾選docker支援,
    image
  • 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支援
    image
  • 修改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

驗證是否成功

image
我是nat到虛擬機器
測試介面,如果都能正常設定狀態,獲取狀態,刪除狀態就OK了

遇到問題

我上面部署成功的是在vmare虛擬機器k8s叢集上測試成功了,但我在win11本地測試時死也成功不了,搞了好幾天都搞不定
pod執行不了,知道的大佬指點一下啊。
《Blazor+Dapr+K8s微服務之狀態管理》 本地執行能成功。
image

相關檔案下載

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

相關文章

  • Blazor+Dapr+K8s微服務之狀態管理

相關文章