(五)MMA(OpenTelemetry/Rabbit MQ/)


文章目录

  • 项目地址
  • 一、OpenTelemetry
    • [1.1 配置OpenTelemetry](#1.1 配置OpenTelemetry)
      • [1. 服务添加](#1. 服务添加)
      • [2. 添加服务标识](#2. 添加服务标识)
      • [3. 添加请求的标识](#3. 添加请求的标识)
      • [4. 添加中间价](#4. 添加中间价)
  • [二、Rabbit MQ](#二、Rabbit MQ)
    • [2.1 配置Rabbit MQ](#2.1 配置Rabbit MQ)
      • [1. docker-compose](#1. docker-compose)
      • [2. 添加Rabbit MQ的Connect String](#2. 添加Rabbit MQ的Connect String)
    • [2.2 替换成Rabbit MQ](#2.2 替换成Rabbit MQ)
      • [1. 安装所需要的包](#1. 安装所需要的包)
      • [2. 使用](#2. 使用)
  • [三、API Gateways](#三、API Gateways)
    • [3.1 创建Gateway](#3.1 创建Gateway)
      • [1. 配置docker-compose](#1. 配置docker-compose)
      • [2. 添加各种服务](#2. 添加各种服务)
      • [3. 添加jwt配置](#3. 添加jwt配置)
      • [4. 添加日志追踪](#4. 添加日志追踪)
      • [5. 配置appsettings](#5. 配置appsettings)
      • [6. Yarp反向代理设置](#6. Yarp反向代理设置)

项目地址

  • 教程作者:
  • 教程地址:
复制代码
  • 代码仓库地址:
复制代码
  • 所用到的框架和插件:

    dbt
    airflow

一、OpenTelemetry

1.1 配置OpenTelemetry

1. 服务添加

  1. namespace Evently.Common.Infrastructure; 配置
cs 复制代码
        services
            .AddOpenTelemetry()
            .ConfigureResource(resource => resource.AddService(serviceName))
            .WithTracing(tracing =>
            {
                tracing
                    .AddAspNetCoreInstrumentation()
                    .AddHttpClientInstrumentation()
                    .AddEntityFrameworkCoreInstrumentation()
                    .AddRedisInstrumentation()
                    .AddNpgsql()
                    .AddSource(MassTransit.Logging.DiagnosticHeaders.DefaultListenerName);

                tracing.AddOtlpExporter();
            });

2. 添加服务标识

  1. 创建服务标识
cs 复制代码
namespace Evently.Api.OpenTelemetry;

public static class DiagnosticsConfig
{
    public const string ServiceName = "Evently.Api";
}
  1. program里注册

3. 添加请求的标识

  • 在RequestLoggingPipelineBehavior添加的请求和服务的标识

4. 添加中间价

  • LogContextTraceLoggingMiddleware
cs 复制代码
namespace Evently.Api.Middleware;
internal sealed class LogContextTraceLoggingMiddleware(RequestDelegate next)
{
    public Task Invoke(HttpContext context)
    {
        string traceId = Activity.Current?.TraceId.ToString();
        using (LogContext.PushProperty("TraceId", traceId))
        {
            return next.Invoke(context);
        }
    }
}
  • MiddlewareExtensions 用于将自定义日志追踪中间件 LogContextTraceLoggingMiddleware 添加到 ASP.NET Core 的中间件管道中。
cs 复制代码
namespace Evently.Api.Middleware;
internal static class MiddlewareExtensions
{
    internal static IApplicationBuilder UseLogContextTraceLogging(this IApplicationBuilder app)
    {
        app.UseMiddleware<LogContextTraceLoggingMiddleware>();
        return app;
    }
}
  • 中间件添加

二、Rabbit MQ

2.1 配置Rabbit MQ

1. docker-compose

  • docker-compose.yml

    复制代码
    evently.queue:
      image: rabbitmq:management-alpine
      container_name: Evently.Queue
      hostname: evently-queue
      volumes:
          - ./.containers/queue/data/:/var/lib/rabbitmq
          - ./.containers/queue/log/:/var/log/rabbitmq
      environment:
          RABBITMQ_DEFAULT_USER: guest
          RABBITMQ_DEFAULT_PASS: guest
      ports:
        - 5672:5672
        - 15672:15672

2. 添加Rabbit MQ的Connect String

cs 复制代码
  "ConnectionStrings": {
    "Database": "Host=evently.database;Port=5432;Database=evently;Username=postgres;Password=postgres;Include Error Detail=true",
    "Cache": "evently.redis:6379",
    "Queue": "amqp://evently-queue:5672"
  },

2.2 替换成Rabbit MQ

1. 安装所需要的包

  • 替换之前内存为Rabbit MQ
  • 安装所需要的包
cs 复制代码
    <PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="8.0.1" />
    <PackageReference Include="MassTransit.RabbitMQ" Version="8.2.1" />
  1. 创建MQ配置文件
cs 复制代码
namespace Evently.Common.Infrastructure.EventBus;
public sealed record RabbitMqSettings(string Host, string Username = "guest", string Password = "guest");

2. 使用

  1. 修改MassTransit,将内存改为MQ
  2. 注册Ticketing的消费者
  3. 注册Event的消费者
  4. Program里注册

三、API Gateways

3.1 创建Gateway

1. 配置docker-compose

2. 添加各种服务

  • 在Gateway的program.cs里添加服务
cs 复制代码
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, loggerConfig) => loggerConfig.ReadFrom.Configuration(context.Configuration));
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
    
builder.Services
    .AddOpenTelemetry()
    .ConfigureResource(resource => resource.AddService(DiagnosticsConfig.ServiceName))
    .WithTracing(tracing =>
    {
        tracing
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddSource("Yarp.ReverseProxy");

        tracing.AddOtlpExporter();
    });

builder.Services.AddAuthorization();
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.ConfigureOptions<JwtBearerConfigureOptions>();
WebApplication app = builder.Build();
app.UseLogContextTraceLogging();
app.UseSerilogRequestLogging();
app.UseAuthentication();
app.UseAuthorization();
app.MapReverseProxy();
app.Run();

3. 添加jwt配置

cs 复制代码
namespace Evently.Gateway.Authentication;
internal sealed class JwtBearerConfigureOptions(IConfiguration configuration)
    : IConfigureNamedOptions<JwtBearerOptions>
{
    private const string ConfigurationSectionName = "Authentication";

    public void Configure(JwtBearerOptions options)
    {
        configuration.GetSection(ConfigurationSectionName).Bind(options);
    }
    public void Configure(string? name, JwtBearerOptions options)
    {
        Configure(options);
    }
}

4. 添加日志追踪

  1. 添加日志追踪
cs 复制代码
using System.Diagnostics;
using Serilog.Context;
namespace Evently.Gateway.Middleware;
internal sealed class LogContextTraceLoggingMiddleware(RequestDelegate next)
{
    public Task Invoke(HttpContext context)
    {
        string traceId = Activity.Current?.TraceId.ToString();

        using (LogContext.PushProperty("TraceId", traceId))
        {
            return next.Invoke(context);
        }
    }
}
  1. 注册
cs 复制代码
namespace Evently.Gateway.Middleware;
internal static class MiddlewareExtensions
{
    internal static IApplicationBuilder UseLogContextTraceLogging(this IApplicationBuilder app)
    {
        app.UseMiddleware<LogContextTraceLoggingMiddleware>();
        return app;
    }
}

5. 配置appsettings

  • 基础设置
cs 复制代码
{
  "Authentication": {
    "Audience": "account",
    "TokenValidationParameters": {
      "ValidIssuers": [ "http://evently.identity:8080/realms/evently", "http://localhost:18080/realms/evently" ]
    },
    "MetadataAddress": "http://evently.identity:8080/realms/evently/.well-known/openid-configuration",
    "RequireHttpsMetadata": false
  },
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.Seq"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "Seq",
        "Args": { "serverUrl": "http://evently.seq:5341" }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Properties": {
      "Application": "Evently.Gateway"
    }
  },
  "OTEL_EXPORTER_OTLP_ENDPOINT": "http://evently.jaeger:4317",
}

6. Yarp反向代理设置

相关推荐
Miqiuha8 小时前
生成唯一id
分布式
遇见火星11 小时前
Redis主从复制深度解析:数据高可用与负载均衡的核心方案
数据库·redis·缓存·负载均衡
左灯右行的爱情14 小时前
Kafka专辑- 整体架构
分布式·架构·kafka
山上春16 小时前
Odoo 分布式单体与微服务模式深度对比研究报告
分布式·微服务·架构
左灯右行的爱情16 小时前
Kafka专辑 : 生产者写入路径
分布式·kafka·linq
java1234_小锋17 小时前
Zookeeper集群数据是如何同步的?
分布式·zookeeper·云原生
Ahtacca17 小时前
Redis 五大常用数据类型详解及 Java 客户端(RedisTemplate)操作实战
java·数据库·redis·学习·缓存
左灯右行的爱情18 小时前
Kafka专辑: 日志存储模型
分布式·kafka·linq
LB211218 小时前
Kafka笔记
分布式·kafka·linq
C_心欲无痕20 小时前
nodejs - pnpm解决幽灵依赖
前端·缓存·npm·node.js