Skip to content
FROM mcr.microsoft.com/dotnet/sdk:6.0.422 as builder
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster as builder
WORKDIR /app
COPY . .
RUN dotnet nuget Disable source "nuget.org" && \
dotnet nuget add source "http://nuget.cdn.azure.cn/v3/index.json" -n "azure"
# RUN dotnet restore && \
# dotnet build && \
# dotnet publish -c release -r linux-musl-x64 -o /cartservice
RUN dotnet restore cartservice.csproj -r linux-musl-x64
COPY . .
RUN dotnet publish cartservice.csproj -r linux-musl-x64 --self-contained true -p:PublishTrimmed=False -p:TrimMode=Link -c release -o /cartservice --no-restore
RUN dotnet restore && \
dotnet build && \
dotnet publish -c release -r linux-musl-x64 -o /cartservice
# cartservice
FROM mcr.microsoft.com/dotnet/runtime-deps:6.0.28-alpine3.18-amd64
FROM alpine:3.8
# RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
# wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
......
......@@ -16,28 +16,17 @@ using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Security.Authentication;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using cartservice.cartstore;
using cartservice.interfaces;
using CommandLine;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using System.Net;
using System.Security.Authentication;
using Microsoft.AspNetCore.Connections.Features;
using Grpc.Core;
using OpenTelemetry;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.Http;
using Microsoft.Extensions.Configuration;
namespace cartservice
{
......@@ -57,7 +46,6 @@ namespace cartservice
switch (args[0])
{
case "start":
{
Console.WriteLine($"Started as process with id {System.Diagnostics.Process.GetCurrentProcess().Id}");
// Set hostname/ip address
......@@ -85,19 +73,9 @@ namespace cartservice
port = int.Parse(portStr);
}
Console.WriteLine($"Trying to start a grpc server at {hostname}:{port}");
Console.WriteLine("Insecure mode");
Console.WriteLine("Insecure mode!");
string[] warpperArgs = new string[args.Length + 1];
args.CopyTo(warpperArgs, 0);
var loggerFactory = LoggerFactory.Create(builder => {
builder.AddConsole()
.SetMinimumLevel(LogLevel.Debug); // 设置日志记录级别为Debug
});
var logger = loggerFactory.CreateLogger<Program>();
logger.LogDebug("Starting application...");
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
......@@ -112,12 +90,10 @@ namespace cartservice
// webBuilder.UseUrls($"http://{hostname}:{port}");
}).Build().Run();
break;
}
default:
{
Console.WriteLine("Invalid command");
break;
}
}
}
}
......
......@@ -7,11 +7,6 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.Http;
using Grpc.Core;
namespace cartservice
{
......@@ -29,40 +24,38 @@ namespace cartservice
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
Console.WriteLine("service" + Environment.GetEnvironmentVariable("JAEGER_HOST") + ":" + Environment.GetEnvironmentVariable("JAEGER_PORT"));
var resourcebuilder =
ResourceBuilder
.CreateDefault()
.AddService(this.Configuration.GetValue<string>("SERVICE_NAME"))
.AddAttributes(new Dictionary<string, object>
{
["exporter"] = "jaeger",
["float"] = 312.23,
["ip"] = Environment.GetEnvironmentVariable("POD_IP"),
["podName"] = Environment.GetEnvironmentVariable("POD_NAME"),
["nodeName"] = Environment.GetEnvironmentVariable("NODE_NAME")
});
// services.AddSingleton<ICartStore>();
services.AddGrpc();
services.AddSingleton<CartStore>();
services.AddOpenTelemetry().WithTracing(builder => {
builder.AddAspNetCoreInstrumentation();
builder.AddHttpClientInstrumentation();
builder.AddOtlpExporter((options) => {
options.Endpoint = new Uri("http://" + Environment.GetEnvironmentVariable("JAEGER_HOST") + ":" + Environment.GetEnvironmentVariable("JAEGER_PORT") + "/api/traces");
// options.Protocol = OtlpExportProtocol.HttpProtobuf;
services.AddOpenTelemetryTracerProvider(telemetry =>
{
telemetry.AddJaegerExporter((jaegerOptions) =>
{
jaegerOptions.ServiceName = this.Configuration.GetValue<string>("SERVICE_NAME");
jaegerOptions.AgentHost = this.Configuration.GetValue<string>("JAEGER_HOST");
jaegerOptions.AgentPort = this.Configuration.GetValue<int>("JAEGER_PORT");
jaegerOptions.ProcessTags = new Dictionary<string, object>{
{"exporter","jaeger"},
{"float", 312.23},
{"ip",this.Configuration.GetValue<string>("POD_IP")},
{"podName",this.Configuration.GetValue<string>("POD_NAME")},
{"nodeName",this.Configuration.GetValue<string>("NODE_NAME")},
};
});
builder.SetResourceBuilder(resourcebuilder);
telemetry.AddAspNetCoreInstrumentation();
telemetry.AddGrpcClientInstrumentation();
telemetry.AddHttpClientInstrumentation();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (!env.IsDevelopment())
if (env.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseDeveloperExceptionPage();
}
app.UseRouting();
......@@ -71,6 +64,11 @@ namespace cartservice
{
endpoints.MapGrpcService<CartServiceImpl>();
endpoints.MapGrpcService<HealthImpl>();
// endpoints.MapGet("/", async context =>
// {
// await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
// });
});
}
}
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<UseAppHost>true</UseAppHost>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableLongPaths>true</EnableLongPaths>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.60.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.60.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.60.0" />
<PackageReference Include="Grpc.Tools" Version="2.60.0">
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.30.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.30.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.30.0" />
<PackageReference Include="Grpc.Tools" Version="2.30.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.5.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.10" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="0.5.0-beta.2" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="0.5.0-beta.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="0.5.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="0.5.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Grpc" Version="0.4.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="0.5.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="0.5.0-beta.2" />
<PackageReference Include="StackExchange.Redis" Version="2.1.58" />
</ItemGroup>
<ItemGroup>
......
......@@ -19,11 +19,6 @@ using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using cartservice.interfaces;
using Google.Protobuf;
using Grpc.Core;
......@@ -31,10 +26,6 @@ using Hipstershop;
using StackExchange.Redis;
using OpenTelemetry;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.Http;
namespace cartservice.cartstore
......@@ -100,31 +91,31 @@ namespace cartservice.cartstore
openTelemetry = null;
}
var resourcebuilder =
ResourceBuilder
.CreateDefault()
.AddService("redis")
.AddAttributes(new Dictionary<string, object>
{
["exporter"] = "jaeger",
["float"] = 312.23,
["ip"] = Environment.GetEnvironmentVariable("POD_IP"),
["podName"] = Environment.GetEnvironmentVariable("POD_NAME"),
["nodeName"] = Environment.GetEnvironmentVariable("NODE_NAME")
});
Console.WriteLine("redis" + Environment.GetEnvironmentVariable("JAEGER_HOST") + ":" + Environment.GetEnvironmentVariable("JAEGER_PORT"));
openTelemetry = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(resourcebuilder)
.AddOtlpExporter((options) =>
.AddJaegerExporter(jaegerOptions =>
{
options.Endpoint = new Uri("http://" + Environment.GetEnvironmentVariable("JAEGER_HOST") + ":" + Environment.GetEnvironmentVariable("JAEGER_PORT") + "/api/traces");
if (!int.TryParse(Environment.GetEnvironmentVariable("JAEGER_PORT"), out int port))
{
port = 8080;
}
jaegerOptions.ServiceName = "redis";
jaegerOptions.AgentHost = Environment.GetEnvironmentVariable("JAEGER_HOST");
jaegerOptions.AgentPort = port;
jaegerOptions.ProcessTags = new Dictionary<string, object>{
{"exporter","jaeger"},
{"float", 312.23},
{"ip",Environment.GetEnvironmentVariable("POD_IP")},
{"podName",Environment.GetEnvironmentVariable("POD_NAME")},
{"nodeName",Environment.GetEnvironmentVariable("NODE_NAME")},
};
})
.AddRedisInstrumentation(redis, options =>
{
options.FlushInterval = TimeSpan.FromSeconds(5);
})
.AddSource("redis")
.Build();
ActivitySource activitySource = new ActivitySource("redis");
if (redis == null || !redis.IsConnected)
......@@ -197,7 +188,7 @@ namespace cartservice.cartstore
}
catch (Exception ex)
{
throw new RpcException(new Grpc.Core.Status(Grpc.Core.StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
throw new RpcException(new Grpc.Core.Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
}
}
......@@ -215,7 +206,7 @@ namespace cartservice.cartstore
}
catch (Exception ex)
{
throw new RpcException(new Grpc.Core.Status(Grpc.Core.StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
throw new RpcException(new Grpc.Core.Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
}
}
......@@ -242,7 +233,7 @@ namespace cartservice.cartstore
}
catch (Exception ex)
{
throw new RpcException(new Grpc.Core.Status(Grpc.Core.StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
throw new RpcException(new Grpc.Core.Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
}
}
......
......@@ -24,8 +24,7 @@ const options = {
tags: [{key: 'ip', value: process.env.POD_IP},
{key: 'name', value: process.env.POD_NAME},
{key: 'node_name', value: process.env.NODE_NAME}], // optional
host: process.env.JAEGER_HOST,
port: process.env.JAEGER_PORT, // optional
endpoint: 'http://' + process.env.JAEGER_HOST + ':' + process.env.JAEGER_PORT + '/api/traces',
maxPacketSize: 65000 // optional
}
......
......@@ -39,8 +39,7 @@ const options = {
tags: [{key: 'ip', value: process.env.POD_IP},
{key: 'name', value: process.env.POD_NAME},
{key: 'node_name', value: process.env.NODE_NAME}], // optional
host: process.env.JAEGER_HOST,
port: process.env.JAEGER_PORT, // optional
endpoint: 'http://' + process.env.JAEGER_HOST + ':' + process.env.JAEGER_PORT + '/api/traces',
maxPacketSize: 65000 // optional
}
......@@ -49,7 +48,7 @@ const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
console.log("zipkin tracing initialized");
console.log("jaeger tracing initialized, " + process.env.JAEGER_HOST + ':' + process.env.JAEGER_PORT);
const path = require('path');
const HipsterShopServer = require('./server');
......