/// <summary> /// Builds the default RPC client. /// </summary> /// <param name="target">The target.</param> /// <param name="clientTracingInterceptor">The client tracing interceptor, if any.</param> /// <param name="additionalMetadata">The additional metadata, if any.</param> /// <returns> /// The gRPC client. /// </returns> public static Foobar.FoobarClient ConstructRpcClient( string target, ClientTracingInterceptor clientTracingInterceptor = null, IEnumerable <Metadata.Entry> additionalMetadata = null) { var channel = new Channel(target, ChannelCredentials.Insecure); var callInvoker = channel.CreateCallInvoker(); if (clientTracingInterceptor != null) { callInvoker = callInvoker.Intercept(clientTracingInterceptor); } // The metadata injector comes first if (additionalMetadata != null) { callInvoker = callInvoker.Intercept( metadata => { foreach (var m in additionalMetadata) { metadata.Add(m); } return(metadata); }); } return(new Foobar.FoobarClient(callInvoker)); }
public static async Task Main(string[] args) { var loggerFactory = new LoggerFactory().AddConsole(); Tracer tracer = TracingHelper.InitTracer("client", loggerFactory); ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor(tracer); Channel channel = new Channel($"{Server}:{Port}", ChannelCredentials.Insecure); var client = new gRPCService.gRPCServiceClient(channel.Intercept(tracingInterceptor)); string user = "******"; var reply = client.SayHello(new HelloRequest { Name = user, SendDate = DateTime.UtcNow.ToTimestamp() }); Console.WriteLine("Greeting: " + reply.ResponseMsg); var response = client.SayGoodbye(new GoodByeRequest() { Name = user }); Console.WriteLine("Response: " + response.ResponseMsg); await channel.ShutdownAsync(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public UserController() { var tracer = GlobalTracer.Instance; ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor(tracer); var host = AppSettingsHelper.Instance.GetValueFromKey(HostNameConst.TestKey); var port = AppSettingsHelper.Instance.GetValueFromKey(PortConst.TestKey); var channel = new Channel($"{host}:{port}", ChannelCredentials.Insecure); _serviceClient = new UserService.UserServiceClient(channel.Intercept(tracingInterceptor)); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy(CorsPolicy, builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); }); // 加入 OpenTracing services.AddOpenTracing().AddSingleton <ITracer>(serviceProvider => { const string serviceName = "MockSite.Web"; var tracer = new Tracer.Builder(serviceName) .WithSampler(new ConstSampler(true)) .Build(); // 註冊 Jaeger tracer GlobalTracer.Register(tracer); return(tracer); }); services.AddMvc().AddNewtonsoftJson(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "MockSite API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "Bearer", //The name of the previously defined security scheme. Type = ReferenceType.SecurityScheme } }, new List <string>(0) } }); }); // In production, the React files will be served from this directory services.AddSpaStaticFiles(config => { config.RootPath = "ClientApp/build"; }); // Configure JWT authentication services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateIssuerSigningKey = true, ValidIssuer = _configuration.GetSection(AppSetting.JwtIssuerKey).Value, ValidAudience = _configuration.GetSection(AppSetting.JwtAudienceKey).Value, IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(_configuration.GetSection(AppSetting.JwtSecretKey).Value) ) }; }); // Configure DI for application services services.AddScoped <IUserService, UserService>(); var consulIp = _configuration.GetSection(ConsulConfigConst.ConsulIp).Value; var consulPort = _configuration.GetSection(ConsulConfigConst.ConsulPort).Value; var consulModule = _configuration.GetSection(ConsulConfigConst.ConsulModule).Value; var consulProvider = ConsulConfigProvider.LoadConsulConfig($"http://{consulIp}:{consulPort}/v1/kv/", consulModule.Split(',')); var consul = new ConfigurationBuilder() .AddJsonFile(consulProvider, "test.json", true, false) .Build(); // Register gRPC Service var userHost = $"{consul[HostNameConst.TestKey]}:{consul[PortConst.TestKey]}"; var tracingInterceptor = new ClientTracingInterceptor(GlobalTracer.Instance); services.AddSingleton(new Message.UserService.UserServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); services.AddSingleton(new CurrencyService.CurrencyServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); services.AddSingleton(new LocalizationService.LocalizationServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { // 加入 OpenTracing services.AddOpenTracing().AddSingleton <ITracer>(serviceProvider => { const string serviceName = "MockSite.Web"; var tracer = new Tracer.Builder(serviceName) .WithSampler(new ConstSampler(true)) .Build(); // 註冊 Jaeger tracer GlobalTracer.Register(tracer); return(tracer); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "MockSite API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" }); c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > { { "Bearer", Enumerable.Empty <string>() } }); }); // In production, the React files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; }); // Configure JWT authentication services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateIssuerSigningKey = true, ValidIssuer = AppSettingsHelper.Instance.GetValueFromKey(AppSetting.JwtIssuerKey), ValidAudience = AppSettingsHelper.Instance.GetValueFromKey(AppSetting.JwtAudienceKey), IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(AppSettingsHelper.Instance.GetValueFromKey(AppSetting.JwtSecretKey)) ) }; }); // Configure DI for application services services.AddScoped <IUserService, UserService>(); // Register gRPC Service var userHost = $"{ConsulSettingHelper.Instance.GetValueFromKey(HostNameConst.TestKey)}:{ConsulSettingHelper.Instance.GetValueFromKey(PortConst.TestKey)}"; var tracingInterceptor = new ClientTracingInterceptor(GlobalTracer.Instance); services.AddSingleton(new Message.UserService.UserServiceClient( CreateKeepAliveWithoutCallChannel(userHost).Intercept(tracingInterceptor)) ); }