Docker Desktop部署微软微服务Dapr(Redis+Zipkin+Placement)

Docker Desktop部署微软微服务Dapr(Redis+Zipkin+Placement)

说明

  • 系统:Windows 11 专业版 23H2
  • Docker:Docker Desktop v4.29.0+
  • 本文为开发环境学习和测试使用

安装Dapr CLI

使用 MSI 安装程序安装

每个 Dapr CLI 的发布版本还包括一个适用于 Windows 的安装程序。 您可以手动下载MSI:

  1. 从最新的Dapr发布页面下载MSI安装包dapr.msi​。
  2. 导航到下载的MSI文件,双击文件运行它。
  3. 按照安装提示接受许可证和安装目录。 所选文件夹已添加到用户的PATH环境变量中。 默认值设置为$Env:SystemDrive\dapr​。
  4. 点击 Install​ 开始安装。 安装完成后,您将看到一条最终消息。

安装目录“D:\dapr\”,将安装目录添加到用户的PATH环境变量中,PowerShell验证

dapr -h
PS C:\Windows\system32> dapr -h

          __
     ____/ /___ _____  _____
    / __  / __ '/ __ \/ ___/
   / /_/ / /_/ / /_/ / /
   \__,_/\__,_/ .___/_/
             /_/

===============================
Distributed Application Runtime

Usage:
  dapr [flags]
  dapr [command]

Available Commands:
  annotate       Add dapr annotations to a Kubernetes configuration. Supported platforms: Kubernetes
  build-info     Print build info of Dapr CLI and runtime
  completion     Generates shell completion scripts
  components     List all Dapr components. Supported platforms: Kubernetes
  configurations List all Dapr configurations. Supported platforms: Kubernetes
  dashboard      Start Dapr dashboard. Supported platforms: Kubernetes and self-hosted
  help           Help about any command
  init           Install Dapr on supported hosting platforms. Supported platforms: Kubernetes and self-hosted
  invoke         Invoke a method on a given Dapr application. Supported platforms: Self-hosted
  list           List all Dapr instances. Supported platforms: Kubernetes and self-hosted
  logs           Get Dapr sidecar logs for an application. Supported platforms: Kubernetes
  mtls           Check if mTLS is enabled. Supported platforms: Kubernetes
  publish        Publish a pub-sub event. Supported platforms: Self-hosted
  run            Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted
  status         Show the health status of Dapr services. Supported platforms: Kubernetes
  stop           Stop Dapr instances and their associated apps. Supported platforms: Self-hosted
  uninstall      Uninstall Dapr runtime. Supported platforms: Kubernetes and self-hosted
  upgrade        Upgrades or downgrades a Dapr control plane installation in a cluster. Supported platforms: Kubernetes
  version        Print the Dapr runtime and CLI version

Flags:
  -h, --help                  help for dapr
      --log-as-json           Log output in JSON format
      --runtime-path string   The path to the dapr runtime installation directory
  -v, --version               version for dapr

Use "dapr [command] --help" for more information about a command.

init命令会用到特殊网络,如果没有,可以参考安装及初始化Dapr方式(国内)

dapr init
PS C:\Windows\system32> dapr init
Making the jump to hyperspace...
Container images will be pulled from Docker Hub
Installing runtime version 1.13.2
Downloading binaries and setting up components...
Downloaded binaries and completed components set up.
daprd binary has been installed to C:\Users\sywq6\.dapr\bin.
dapr_placement container is running.
dapr_redis container is running.
dapr_zipkin container is running.
Use `docker ps` to check running containers.
Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started

init完会生成“.dapr”文件夹,存在的文件如下

在这里插入图片描述

│  config.yaml
│  
├─bin
│  │  daprd.exe
│  │  dashboard.exe // dapr看板程序
│  │  
│  └─web
│      └─dist
│          │  favicon.ico
│          │  index.html
│          │  main.js
│          │  main.js.map
│          │  polyfills-es5.js
│          │  polyfills-es5.js.map
│          │  polyfills.js
│          │  polyfills.js.map
│          │  runtime.js
│          │  runtime.js.map
│          │  src_app_pages_pages_module_ts.js
│          │  src_app_pages_pages_module_ts.js.map
│          │  styles.css
│          │  styles.css.map
│          │  vendor.js
│          │  vendor.js.map
│          │  
│          ├─assets
│          │  └─images
│          │          dapr.png
│          │          logo-white.svg
│          │          logo.svg
│          │  
│          └─lib
│              └─vs
│                  │  loader.js
│                  │  
│                  ├─base
│                  │  ├─browser
│                  │  │  └─ui
│                  │  │      └─codicons
│                  │  │          └─codicon
│                  │  │                  codicon.ttf
│                  │  │          
│                  │  └─worker
│                  │          workerMain.js
│                  │  
│                  ├─basic-languages
│                  │  ├─abap
│                  │  │      abap.js
│                  │  │  
│                  │  ├─apex
│                  │  │      apex.js
│                  │  │  
│                  │  ├─azcli
│                  │  │      azcli.js
│                  │  │  
│                  │  ├─bat
│                  │  │      bat.js
│                  │  │  
│                  │  ├─bicep
│                  │  │      bicep.js
│                  │  │  
│                  │  ├─cameligo
│                  │  │      cameligo.js
│                  │  │  
│                  │  ├─clojure
│                  │  │      clojure.js
│                  │  │  
│                  │  ├─coffee
│                  │  │      coffee.js
│                  │  │  
│                  │  ├─cpp
│                  │  │      cpp.js
│                  │  │  
│                  │  ├─csharp
│                  │  │      csharp.js
│                  │  │  
│                  │  ├─csp
│                  │  │      csp.js
│                  │  │  
│                  │  ├─css
│                  │  │      css.js
│                  │  │  
│                  │  ├─dart
│                  │  │      dart.js
│                  │  │  
│                  │  ├─dockerfile
│                  │  │      dockerfile.js
│                  │  │  
│                  │  ├─ecl
│                  │  │      ecl.js
│                  │  │  
│                  │  ├─elixir
│                  │  │      elixir.js
│                  │  │  
│                  │  ├─fsharp
│                  │  │      fsharp.js
│                  │  │  
│                  │  ├─go
│                  │  │      go.js
│                  │  │  
│                  │  ├─graphql
│                  │  │      graphql.js
│                  │  │  
│                  │  ├─handlebars
│                  │  │      handlebars.js
│                  │  │  
│                  │  ├─hcl
│                  │  │      hcl.js
│                  │  │  
│                  │  ├─html
│                  │  │      html.js
│                  │  │  
│                  │  ├─ini
│                  │  │      ini.js
│                  │  │  
│                  │  ├─java
│                  │  │      java.js
│                  │  │  
│                  │  ├─javascript
│                  │  │      javascript.js
│                  │  │  
│                  │  ├─julia
│                  │  │      julia.js
│                  │  │  
│                  │  ├─kotlin
│                  │  │      kotlin.js
│                  │  │  
│                  │  ├─less
│                  │  │      less.js
│                  │  │  
│                  │  ├─lexon
│                  │  │      lexon.js
│                  │  │  
│                  │  ├─liquid
│                  │  │      liquid.js
│                  │  │  
│                  │  ├─lua
│                  │  │      lua.js
│                  │  │  
│                  │  ├─m3
│                  │  │      m3.js
│                  │  │  
│                  │  ├─markdown
│                  │  │      markdown.js
│                  │  │  
│                  │  ├─mips
│                  │  │      mips.js
│                  │  │  
│                  │  ├─msdax
│                  │  │      msdax.js
│                  │  │  
│                  │  ├─mysql
│                  │  │      mysql.js
│                  │  │  
│                  │  ├─objective-c
│                  │  │      objective-c.js
│                  │  │  
│                  │  ├─pascal
│                  │  │      pascal.js
│                  │  │  
│                  │  ├─pascaligo
│                  │  │      pascaligo.js
│                  │  │  
│                  │  ├─perl
│                  │  │      perl.js
│                  │  │  
│                  │  ├─pgsql
│                  │  │      pgsql.js
│                  │  │  
│                  │  ├─php
│                  │  │      php.js
│                  │  │  
│                  │  ├─postiats
│                  │  │      postiats.js
│                  │  │  
│                  │  ├─powerquery
│                  │  │      powerquery.js
│                  │  │  
│                  │  ├─powershell
│                  │  │      powershell.js
│                  │  │  
│                  │  ├─pug
│                  │  │      pug.js
│                  │  │  
│                  │  ├─python
│                  │  │      python.js
│                  │  │  
│                  │  ├─qsharp
│                  │  │      qsharp.js
│                  │  │  
│                  │  ├─r
│                  │  │      r.js
│                  │  │  
│                  │  ├─razor
│                  │  │      razor.js
│                  │  │  
│                  │  ├─redis
│                  │  │      redis.js
│                  │  │  
│                  │  ├─redshift
│                  │  │      redshift.js
│                  │  │  
│                  │  ├─restructuredtext
│                  │  │      restructuredtext.js
│                  │  │  
│                  │  ├─ruby
│                  │  │      ruby.js
│                  │  │  
│                  │  ├─rust
│                  │  │      rust.js
│                  │  │  
│                  │  ├─sb
│                  │  │      sb.js
│                  │  │  
│                  │  ├─scala
│                  │  │      scala.js
│                  │  │  
│                  │  ├─scheme
│                  │  │      scheme.js
│                  │  │  
│                  │  ├─scss
│                  │  │      scss.js
│                  │  │  
│                  │  ├─shell
│                  │  │      shell.js
│                  │  │  
│                  │  ├─solidity
│                  │  │      solidity.js
│                  │  │  
│                  │  ├─sophia
│                  │  │      sophia.js
│                  │  │  
│                  │  ├─sparql
│                  │  │      sparql.js
│                  │  │  
│                  │  ├─sql
│                  │  │      sql.js
│                  │  │  
│                  │  ├─st
│                  │  │      st.js
│                  │  │  
│                  │  ├─swift
│                  │  │      swift.js
│                  │  │  
│                  │  ├─systemverilog
│                  │  │      systemverilog.js
│                  │  │  
│                  │  ├─tcl
│                  │  │      tcl.js
│                  │  │  
│                  │  ├─twig
│                  │  │      twig.js
│                  │  │  
│                  │  ├─typescript
│                  │  │      typescript.js
│                  │  │  
│                  │  ├─vb
│                  │  │      vb.js
│                  │  │  
│                  │  ├─xml
│                  │  │      xml.js
│                  │  │  
│                  │  └─yaml
│                  │          yaml.js
│                  │  
│                  ├─editor
│                  │      editor.main.css
│                  │      editor.main.js
│                  │      editor.main.nls.de.js
│                  │      editor.main.nls.es.js
│                  │      editor.main.nls.fr.js
│                  │      editor.main.nls.it.js
│                  │      editor.main.nls.ja.js
│                  │      editor.main.nls.js
│                  │      editor.main.nls.ko.js
│                  │      editor.main.nls.ru.js
│                  │      editor.main.nls.zh-cn.js
│                  │      editor.main.nls.zh-tw.js
│                  │  
│                  └─language
│                      ├─css
│                      │      cssMode.js
│                      │      cssWorker.js
│                      │  
│                      ├─html
│                      │      htmlMode.js
│                      │      htmlWorker.js
│                      │  
│                      ├─json
│                      │      jsonMode.js
│                      │      jsonWorker.js
│                      │  
│                      └─typescript
│                              tsMode.js
│                              tsWorker.js
│                      
└─components
        pubsub.yaml
        statestore.yaml

正常Init完,会看到启动如下三个服务

​​在这里插入图片描述

安装出错(能正常安装可忽略本节)

如果没有使用特殊网络,大概率会因为网络原因,导致安装失败

  • Docker中关闭三个服务(Redis+Zipkin+Placement)
  • 在PowerShell中卸载Dapr。使用不带“–all”参数的uninstall命令(否则会把“.dapr”文件夹内的所有文件清理),会把bin文件夹删除
dapr uninstall

离线下载文件部分可以参考这篇文章:安装及初始化Dapr方式(国内)

验证安装

PowderShell执行

docker ps --format "{{.Image}} - {{.Ports}} - {{.Names}}"
PS C:\Windows\system32> docker ps --format "{{.Image}} - {{.Ports}} - {{.Names}}"
daprio/dapr:1.13.2 - 0.0.0.0:58080->8080/tcp, 0.0.0.0:59090->9090/tcp, 0.0.0.0:6050->50005/tcp - dapr_placement
openzipkin/zipkin - 9410/tcp, 0.0.0.0:9411->9411/tcp - dapr_zipkin
redis:6 - 0.0.0.0:6379->6379/tcp - dapr_redis

启动dapr Dashboard

dapr dashboard Dapr Dashboard running on http://localhost:8080

浏览器打开“http://localhost:8080/”,打开dapr Dashboard

在这里插入图片描述

启动Zipkin看板

浏览器打开“http://localhost:9411/”,打开Zipkin看板

在这里插入图片描述

创建.NET项目

打开VS Code,打开终端,新建.NET项目

dotnet new webapi -o dapr.microservice.webapi
cd .\dapr.microservice.webapi\
dotnet add package Dapr.AspNetCore

修改文件Program.cs​的内容

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddDapr();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapSubscribeHandler();
app.Run("http://localhost:5000");

新建文件HelloWorldController.cs

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class HelloController : ControllerBase
{
    private readonly ILogger<HelloController> _logger;
 
    public HelloController(ILogger<HelloController> logger)
    {
        _logger = logger;
    }
 
    [HttpGet()]
    public ActionResult<string> Get()
    {
        Console.WriteLine("Hello, World.");
        return "Hello, World";
    }
}

终端启动项目

dotnet run

使用PowerShell查看结果

PS C:\Users\sywq6> curl http://localhost:5000/hello                                                                   

StatusCode        : 200
StatusDescription : OK
Content           : Hello, World
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Content-Type: text/plain; charset=utf-8
                    Date: Wed, 15 May 2024 05:31:32 GMT
                    Server: Kestrel

                    Hello, World
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Content-Type, text/plain; charset=utf-8], [Date, Wed, 15 May 2024 0
                    5:31:32 GMT], [Server, Kestrel]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 12

使用Dapr启动应用程序​

在这里插入图片描述

dapr run --app-id hello-world --app-port 5000 --dapr-http-port 5010 dotnet run

说明:

  • app-id:用于服务发现的应用程序 Id
  • app-port:应用程序正在侦听的端口
  • dapr-http-port:Dapr 要监听的 HTTP 端口

这时,再次浏览器打开“http://localhost:8080/”,打开dapr Dashboard,会看到app-id为“hello-world”程序

在这里插入图片描述

Zipkin链路追踪

传统微服务中,要实现链路追踪,对代码的侵入强

Dapr 在 Sidecar 中添加了一个 http/grpc中间件。拦截所有应用程序流量,并自动注入关联 ID 以跟踪分布式事务

使用 Zipkin 协议进行分布式跟踪,无需代码检测,使用可配置的跟踪级别自动跟踪所有流量

​​在这里插入图片描述

浏览器打开“http://localhost:9411/”,打开Zipkin管理页面,【RUN QUERY】会看到如下结果

​​在这里插入图片描述

PowerShell执行,及其结果如下

curl http://localhost:5010/v1.0/invoke/hello-world/method/hello
PS C:\Windows\system32> curl http://localhost:5010/v1.0/invoke/hello-world/method/hello                             

StatusCode        : 200
StatusDescription : OK
Content           : Hello, World
RawContent        : HTTP/1.1 200 OK
                    Traceparent: 00-ebd2a75c457c6a6c5fefa8d50be51cfe-ee934c87a7cf2b7b-01
                    Content-Length: 12
                    Content-Type: text/plain; charset=utf-8
                    Date: Wed, 15 May 2024 02:57:08 GMT
                    Server: Kestrel...
Forms             : {}
Headers           : {[Traceparent, 00-ebd2a75c457c6a6c5fefa8d50be51cfe-ee934c87a7cf2b7b-01], [Content-Length, 12], [Con
                    tent-Type, text/plain; charset=utf-8], [Date, Wed, 15 May 2024 02:57:08 GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 12

Zipkin管理页面,能够查询到刚才调用Api的记录

​​在这里插入图片描述
在这里插入图片描述

参考

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值