/Consul.AspNetCore.ServiceDiscovery

AspNet Core extension to implement service discovery with Consul easily

Primary LanguageC#Apache License 2.0Apache-2.0

Consul.AspNetCore.ServiceDiscovery

NuGet

Consul.AspNetCore.ServiceDiscovery is an extension that helps you to implement Service Discovery with Consul very easy.

If you need to register a service in Consul you've got to add this lines in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
	...
	services.AddConsul(Configuration);
	services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	...
	app.UseMvc();
	app.UseConsul();
}

and then add this in the appsettings.json

	"ServiceConfig": {
		"ServiceDiscoveryAddress": "http://consul:8500",
		"ServiceAddress": "http://example.api:80",
		"ServiceName": "example",
		"ServiceId": "example-v1"
	  }

And that's all. Now your service is being registered to Consul.

After that if you want to retrieve your service's url from your API Gateway or App you can do it like this:

var svcDiscovery = new ServiceDiscovery(configuration);
var yourService = svcDiscovery.GetServiceInfo("example");