Пример #1
0
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            app.UseAbp(options => { options.UseAbpRequestLocalization = false; }); // Initializes ABP framework.

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService <ImageRpcService>();

                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");
                });
            });

            // register this service to Consul
            var           lifetime      = app.ApplicationServices.GetService(typeof(IHostApplicationLifetime));
            ServiceEntity serviceEntity = new ServiceEntity
            {
                IP          = "192.168.1.60",
                Port        = 5001,
                ServiceName = "CargoX-RPC",
                ConsulIP    = "192.168.1.200",
                ConsulPort  = 8500
            };

            app.RegisterConsul(lifetime as IHostApplicationLifetime, serviceEntity);
        }
Пример #2
0
        public static IApplicationBuilder RegisterConsul(
            this IApplicationBuilder app,
            IHostApplicationLifetime lifetime,
            ServiceEntity serviceEntity)
        {
            var consulClient = new ConsulClient(x =>
                                                x.Address = new Uri($@"http://{serviceEntity.ConsulIP}:{serviceEntity.ConsulPort}"));

            var httpCheck = new AgentServiceCheck()
            {
                DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),
                Interval = TimeSpan.FromSeconds(10),
                HTTP     = $"http://{serviceEntity.IP}:{serviceEntity.Port}/api/health",
                Timeout  = TimeSpan.FromSeconds(5)
            };

            // Register service with consul
            var registration = new AgentServiceRegistration()
            {
                Checks  = new[] { httpCheck },
                ID      = Guid.NewGuid().ToString(),
                Name    = serviceEntity.ServiceName,
                Address = serviceEntity.IP,
                Port    = serviceEntity.Port,
                Tags    = new[] { $"urlprefix-/{serviceEntity.ServiceName}" }
            };

            consulClient.Agent.ServiceRegister(registration).Wait();
            lifetime.ApplicationStopping.Register(() =>
            {
                consulClient.Agent.ServiceDeregister(registration.ID).Wait();
            });

            return(app);
        }