当前位置: 首页 > news >正文

(五)MMA(OpenTelemetry/Rabbit MQ/)


文章目录

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


项目地址

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

一、OpenTelemetry

1.1 配置OpenTelemetry

1. 服务添加

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

2. 添加服务标识

在这里插入图片描述

  1. 创建服务标识
namespace Evently.Api.OpenTelemetry;public static class DiagnosticsConfig
{public const string ServiceName = "Evently.Api";
}
  1. program里注册
    在这里插入图片描述

3. 添加请求的标识

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

在这里插入图片描述

4. 添加中间价

在这里插入图片描述

  • LogContextTraceLoggingMiddleware
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 的中间件管道中。
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-alpinecontainer_name: Evently.Queuehostname: evently-queuevolumes:- ./.containers/queue/data/:/var/lib/rabbitmq- ./.containers/queue/log/:/var/log/rabbitmqenvironment:RABBITMQ_DEFAULT_USER: guestRABBITMQ_DEFAULT_PASS: guestports:- 5672:5672- 15672:15672

2. 添加Rabbit MQ的Connect String

  "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
  • 安装所需要的包
    <PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="8.0.1" /><PackageReference Include="MassTransit.RabbitMQ" Version="8.2.1" />
  1. 创建MQ配置文件
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里添加服务
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配置

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. 添加日志追踪
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. 注册
namespace Evently.Gateway.Middleware;
internal static class MiddlewareExtensions
{internal static IApplicationBuilder UseLogContextTraceLogging(this IApplicationBuilder app){app.UseMiddleware<LogContextTraceLoggingMiddleware>();return app;}
}

5. 配置appsettings

  • 基础设置
{"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反向代理设置

在这里插入图片描述

相关文章:

  • Android设置界面层级为最上层实现
  • 零基础远程连接课题组Linux服务器,安装anaconda,配置python环境(换源),在服务器上运行python代码【3/3 适合小白,步骤详细!!!】
  • 深信服防火墙拦截了DELETE、PUT请求,未达到nginx及后端服务
  • 如何将联系人从 Android 传输到 PC(正确步骤)
  • 亚马逊服务器磁盘扩容一般操作
  • R包安装报错解决案例系列|R包使用及ARM架构解决data.table安装错误问题
  • 使用pnpm、vite搭建Phaserjs的开发环境
  • Mico 1.33.1 | 解锁高级版 上千种自定义组件 动态壁纸
  • 评估Facebook的隐私保护:挑战与机遇并存
  • HarmonyOS-ArkUI 窗口层次简介
  • 案例分析|轴承座静力学分析
  • android 输入系统
  • 【R语言编程绘图-折线图】
  • inviteflood:基于 UDP 的 SIP/SDP 洪水攻击工具!全参数详细教程!Kali Linux教程!
  • WPF【09】WPF基础入门 (三层架构与MVC架构)
  • thinkadmin中使用layui日期选择器,数据库存储时间戳
  • 构建高可观测性的云原生应用体系:企业实践指南
  • Java爬虫,获取未来40天预测气象并写入Excel
  • 【HW系列】—目录扫描、口令爆破、远程RCE流量特征
  • Flink Table API 编程实战详解
  • 哈尔滨做设计和网站的公司吗/最专业的seo公司
  • wordpress删除主题/seo必备软件
  • 做淘宝客服的网站/长沙seo网站
  • 网站数据库修改密码要怎么做/广告推广营销网站
  • 制作注册会员的网站/seo站长网怎么下载
  • 网站公安备案是否强制/类聚seo