Skip to content

Commit

Permalink
Merge pull request #24 from AIDotNet/feature/aspire
Browse files Browse the repository at this point in the history
Feature/aspire
  • Loading branch information
239573049 authored Oct 7, 2024
2 parents ba111e5 + 43da5c2 commit caca74f
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Thor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thor.Core", "src\framework\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thor.RabbitMQEvent", "src\framework\Thor.RabbitMQEvent\Thor.RabbitMQEvent.csproj", "{C1F516B3-E06A-4F2A-8465-FC3D8C7645D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thor.AppHost", "src\Thor.AppHost\Thor.AppHost.csproj", "{40A5DC80-CF4A-4491-93EF-D2AD0AF8F2BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thor.ServiceDefaults", "src\Thor.ServiceDefaults\Thor.ServiceDefaults.csproj", "{BE7E011E-E469-44B4-B06B-CE908762D7A3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -137,6 +141,14 @@ Global
{C1F516B3-E06A-4F2A-8465-FC3D8C7645D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1F516B3-E06A-4F2A-8465-FC3D8C7645D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1F516B3-E06A-4F2A-8465-FC3D8C7645D3}.Release|Any CPU.Build.0 = Release|Any CPU
{40A5DC80-CF4A-4491-93EF-D2AD0AF8F2BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40A5DC80-CF4A-4491-93EF-D2AD0AF8F2BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40A5DC80-CF4A-4491-93EF-D2AD0AF8F2BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40A5DC80-CF4A-4491-93EF-D2AD0AF8F2BB}.Release|Any CPU.Build.0 = Release|Any CPU
{BE7E011E-E469-44B4-B06B-CE908762D7A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE7E011E-E469-44B4-B06B-CE908762D7A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE7E011E-E469-44B4-B06B-CE908762D7A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE7E011E-E469-44B4-B06B-CE908762D7A3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 5 additions & 0 deletions src/Thor.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.Thor_Service>("thor-service");

builder.Build().Run();
29 changes: 29 additions & 0 deletions src/Thor.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17179;http://localhost:15075",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21041",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22210"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15075",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19090",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20196"
}
}
}
}
20 changes: 20 additions & 0 deletions src/Thor.AppHost/Thor.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>40741226-dfce-4c8b-ae4f-10b201336e27</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Thor.Service\Thor.Service.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions src/Thor.AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/Thor.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
15 changes: 9 additions & 6 deletions src/Thor.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@
throw new Exception("不支持的数据库类型");
}

builder.AddServiceDefaults();

builder.Services.AddResponseCompression();

var app = builder.Build();

app.MapDefaultEndpoints();

using var scope = app.Services.CreateScope();

Expand Down Expand Up @@ -653,13 +656,13 @@ await service.GetAsync(page, pageSize))
.WithDescription("获取限流策略")
.WithOpenApi();

rateLimitModel.MapPost(string.Empty, async (RateLimitModelService service, RateLimitModel rateLimitModel) =>
await service.CreateAsync(rateLimitModel))
rateLimitModel.MapPost(string.Empty, async (RateLimitModelService service, RateLimitModel limitModel) =>
await service.CreateAsync(limitModel))
.WithDescription("创建限流策略")
.WithOpenApi();

rateLimitModel.MapPut(string.Empty, async (RateLimitModelService service, RateLimitModel rateLimitModel) =>
await service.UpdateAsync(rateLimitModel))
rateLimitModel.MapPut(string.Empty, async (RateLimitModelService service, RateLimitModel limitModel) =>
await service.UpdateAsync(limitModel))
.WithDescription("更新限流策略")
.WithOpenApi();

Expand Down Expand Up @@ -689,15 +692,15 @@ await service.ShareAsync(userId, context))

#endregion

// 对话补全请求
// 对话补全请求
app.MapPost("/v1/chat/completions",
async (ChatService service, HttpContext httpContext, ThorChatCompletionsRequest request) =>
await service.ChatCompletionsAsync(httpContext, request))
.WithGroupName("OpenAI")
.WithDescription("Get completions from OpenAI")
.WithOpenApi();

// 文本补全接口,不建议使用,使用对话补全即可
// 文本补全接口,不建议使用,使用对话补全即可
app.MapPost("/v1/completions", async (ChatService service, HttpContext context) =>
await service.CompletionsAsync(context))
.WithGroupName("OpenAI")
Expand Down
2 changes: 2 additions & 0 deletions src/Thor.Service/Thor.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.1.1" />
<PackageReference Include="SharpToken" Version="2.0.3" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
Expand Down Expand Up @@ -58,6 +59,7 @@
<ProjectReference Include="..\framework\Thor.RabbitMQEvent\Thor.RabbitMQEvent.csproj" />
<ProjectReference Include="..\framework\Thor.RedisMemory.Cache\Thor.RedisMemory.Cache.csproj" />
<ProjectReference Include="..\Thor.Abstractions\Thor.Abstractions.csproj" />
<ProjectReference Include="..\Thor.ServiceDefaults\Thor.ServiceDefaults.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
113 changes: 113 additions & 0 deletions src/Thor.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

return builder;
}

public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry()
.UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}

public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
22 changes: 22 additions & 0 deletions src/Thor.ServiceDefaults/Thor.ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.9.1" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.2.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>

</Project>

0 comments on commit caca74f

Please sign in to comment.