为了模仿服务间的相互调用,定义了2个service,Service02调用Service01的API。每个运行的服务都有一个 Dapr 运行时进程(Daprd)以Sidecar模式运行。Sidecar 编程模型鼓励每个应用程序与自己的 Dapr 实例对话,Dapr 实例会相互发现并进行通信。在k8s集群中国,Dapr 使用 CoreDNS 协议提供负载均衡,将Service02的请求分布到两个Service01的实例上。 1.2、架构优势
关注点分离
Service02只需要知道Service01的app-id即可Service01不需要知道SideCar的存在Service01
不同实例使用相同app-id不同实例使用不同端口Service02
不需要知道Service01的实例数量不需要知道Service01的端口号SideCar
Service02不需要知道SideCar的端口号Service01的实例可以随意增加和减少SideCar通过心跳发现或剔除Service01的实例2、新建一个ASP.NET Core Web API项目 Service01
新建HelloController定义一个API: api/hello每个Service01实例分配不同的InstanceIdAPI被访问时,返回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
var builder = WebApplication.CreateBuilder(args);builder.Services.AddControllers() .AddDapr(); // 注入Dapr servicevar 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
Dockerfile文件及内容会自动生成,无需更改 4.2、给Service02 添加Dockerfile
Dockerfile文件及内容会自动生成,无需更改 4.2、登录docker hub
https://hub.docker.com/
docker login
4.3、给service01和service02打镜像docker build -t netcorecore/service01:v1 -f ./Service01/Dockerfile .
docker build -t netcorecore/service02:v1 -f ./Service02/Dockerfile .
4.4、上传Service01和service02的镜像docker push netcorecore/service01:v1
docker push netcorecore/service02:v1
4.4、查看上传结果 5、创建Service01和Service02的K8S部署文件 5.1、创建Service01的部署文件apiVersion: v1kind: Namespacemetadata: name: dapr-demo # 命名空间---apiVersion: apps/v1kind: Deploymentmetadata: name: service01 namespace: dapr-demo labels: app: service01spec: replicas: 2 # 2个副本 selector: matchLabels: app: service01 template: metadata: labels: app: service01 annotations: dapr.io/enabled: "true" # 开启Dapr边车 dapr.io/app-id: "service01" dapr.io/app-port: "80" spec: containers: - name: service01 image: docker.io/netcorecore/service01:v1 # 上面定义的docker image
5.2、创建Service02的部署文件apiVersion: v1kind: Namespacemetadata: name: dapr-demo # 命名空间---apiVersion: apps/v1kind: Deploymentmetadata: name: service02 namespace: dapr-demo labels: app: service02spec: replicas: 1 selector: matchLabels: app: service02 template: metadata: labels: app: service02 annotations: dapr.io/enabled: "true" # 开启Dapr边车 dapr.io/app-id: "service02" dapr.io/app-port: "80" spec: containers: - name: service02 image: docker.io/netcorecore/service02:v1 # 上面定义的docker image ---apiVersion: v1kind: Servicemetadata: name: service02 namespace: dapr-demospec: type: NodePort selector: app: service02 ports: - port: 80 targetPort: 80 nodePort: 30006 # 对外开放可访问的端口
6、部署创建Service01和Service02到k8s集群 6.1、部署Service01kubectl apply -f service01.yaml
6.2、部署Service02kubectl apply -f service02.yaml
6.3、验证pod
kubectl get pods -n dapr-demo
service
kubectl get svc -n dapr-demo
7、公网访问我们的微服务 7.1 反向代理vi /usr/local/nginx/conf/nginx.conf
server { listen 80; server_name www.corecore.cn; location / { proxy_pass http://localhost:30006; }} server { listen 80; server_name cicd.corecore.cn; location / { proxy_pass http://localhost:8080; }} server { listen 80; server_name monitor.corecore.cn; location / { proxy_pass http://localhost:8081; }}
7.2、重启nginx/usr/local/nginx/sbin/nginx -s reload
7.3、公网访问我们的微服务http://www.corecore.cn/api/hello