Dapr+Net6+K8S最小原型项目架构和实现-11-使用AspNetCore6.0编写Dapr项目
Posted 敦格
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dapr+Net6+K8S最小原型项目架构和实现-11-使用AspNetCore6.0编写Dapr项目相关的知识,希望对你有一定的参考价值。
1. 回顾
上一章,我们动手在K8S集群中安装了Dapr:
这一章,K8S集群和Dapr运行时都准备好了 ,我们开始使用AspNetCore6.0编写Dapr项目。
2. 新建一个ASP.NET Core Web API项目 Service01
- 新建HelloController
- 定义一个API: api/hello
- 每个Service01实例分配不同的InstanceId
- API被访问时,返回InstanceId,来验证负载均衡效果
using Microsoft.AspNetCore.Mvc;
namespace Service01.Controllers
[ApiController]
public class HelloController : ControllerBase
// 每个Service01实例分配不同的InstanceId
private static readonly string InstanceId = Guid.NewGuid().ToString();
[HttpGet("api/Hello")]
public async Task<IActionResult> Hello()
await Task.CompletedTask;
// API被访问时,返回InstanceId,来验证负载均衡效果
return Ok(new Service = "Service01", InstanceId );
3. 新建一个ASP.NET Core Web API项目 Service02
3.1. 引用Dapr.AspNetCore包
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<!--引用Dapr.AspNetCore包-->
<PackageReference Include="Dapr.AspNetCore" Version="1.6.0" />
</ItemGroup>
</Project>
3.2. 注入Dapr service
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers()
.AddDapr(); // 注入Dapr service
var app = builder.Build();
app.UseAuthorization();
app.MapControllers();
app.Run();
3.3. 新建HelloController
- 通过dapr访问Service01的API
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
namespace Service02.Controllers
[ApiController]
public class HelloController : ControllerBase
private readonly DaprClient _daprClient;
public HelloController(DaprClient daprClient)
_daprClient = daprClient;
[HttpGet("api/Hello")]
public async Task<IActionResult> Hello()
// 通过dapr访问Service01的API
var result = await _daprClient.InvokeMethodAsync<object>(HttpMethod.Get, "service01", "api/hello");
return Ok(result);
4. 给Service01和Service02准备Dockerfile
4.1. 给Service01 添加Dockerfile
- Dockerfile文件及内容会自动生成,无需更改
4.2. 给Service02 添加Dockerfile
- Dockerfile文件及内容会自动生成,无需更改
5. 上传Service01和Service02的Docker镜像
5.1. 登录 docker hub
https://hub.docker.com/
docker login
5.2. 给service01和service02打镜像
docker build -t k8schina/service01:v1 -f ./Service01/Dockerfile .
docker build -t k8schina/service02:v1 -f ./Service02/Dockerfile .
5.3. 上传Service01和service02的镜像
docker push k8schina/service01:v1
docker push k8schina/service02:v1
5.4. 查看上传结果
6. 下一章
Dapr+Net6+K8S最小原型项目架构和实现-12-将Dapr项目部署到K8S集群
1. 回顾
上一章,我们使用AspNetCore6.0编写了Dapr项目:
这一章,我们将Dapr项目部署到K8S集群。
2. 编写Dapr项目yaml部署文件
apiVersion: v1
kind: Namespace
metadata:
name: dapr-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service01
namespace: dapr-demo
labels:
app: service01
spec:
replicas: 2
selector:
matchLabels:
app: service01
template:
metadata:
labels:
app: service01
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "service01"
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: service01
image: docker.io/k8schina/service01:v1
ports:
- containerPort: 80
imagePullPolicy: Always
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service02
namespace: dapr-demo
labels:
app: service02
spec:
replicas: 1
selector:
matchLabels:
app: service02
template:
metadata:
labels:
app: service02
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "service02"
dapr.io/app-port: "80"
dapr.io/sidecar-liveness-probe-delay-seconds: "10"
dapr.io/sidecar-readiness-probe-delay-seconds: "10"
spec:
containers:
- name: service02
image: docker.io/k8schina/service02:v1
ports:
- containerPort: 80
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: service02
namespace: dapr-demo
spec:
type: NodePort
selector:
app: service02
ports:
- port: 80
targetPort: 80
nodePort: 30006
3. 将DarpDemo项目部署到K8S集群
3.1. 部署
kubectl apply -f http://k8schina.corecore.cn/DaprDemo.yaml
3.2. 验证docker镜像
docker images | grep service
- 可能被调度到slave节点下载
3.3. 验证pod
kubectl get pods -n dapr-demo
- 每个pod有两个container,一个是service,一个是dapr sidecar
3.4. 验证 service
kubectl get svc -n dapr-demo
4. 配置反向代理
- 反向代理到DaprDemo的K8S服务
vi /usr/local/nginx/conf/nginx.conf
server
listen 80;
server_name app.corecore.cn;
location /
proxy_pass http://localhost:30006;
server
listen 80;
server_name www.corecore.cn;
location /
root html;
index index.html index.htm;
-
重启nginx
/usr/local/nginx/sbin/nginx -s reload
5. 公网访问我们的微服务
- 可以看到负载均衡效果,两次访问,被路由到了不同的pod
6. 下一章
以上是关于Dapr+Net6+K8S最小原型项目架构和实现-11-使用AspNetCore6.0编写Dapr项目的主要内容,如果未能解决你的问题,请参考以下文章