namespace Server { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // 堆代码 duidaima.com // Add services to the container. builder.Services.AddControllers().AddDapr(); //关键的服务注册,只需要引入Dapr.AspNetCore包 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } //app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); } } } using Microsoft.AspNetCore.Mvc; namespace Server.Controllers { [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } [HttpGet(Name = "GetWeatherForecast")] public IEnumerable<WeatherForecast> Get() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray(); } } } namespace Server { public class WeatherForecast { public DateTime Date { get; set; } public int TemperatureC { get; set; } public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); public string? Summary { get; set; } } }下面就是客户端调用的代码,只需要引入包Dapr.Client包,当然consul作为服务之间调用就是httpclient调用了。
namespace Client { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } //app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); } } } using Dapr.Client; using Microsoft.AspNetCore.Mvc; namespace Client.Controllers { [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } [HttpGet(Name = "GetWeatherForecast")] public IEnumerable<WeatherForecast> Get() { //服务之间没有用httpclient调用,用特有的dapr调用。 var daprClient = new DaprClientBuilder().Build(); var content = daprClient.InvokeMethodAsync<IEnumerable<WeatherForecast>>(HttpMethod.Get, "getwf", "WeatherForecast").Result; _logger.LogInformation($"获取wf成功:{content.ToArray().ToString()}"); return content.ToArray(); } } } namespace Client { public class WeatherForecast { public DateTime Date { get; set; } public int TemperatureC { get; set; } public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); public string? Summary { get; set; } } }这个例子仅仅只说明了用Dapr微服务之间的调用,这个不是很服务,但是部署和配置等一系列操作就需要docker基础了。首先要有虚拟机,linux系统,安装好docker,本文没有用到任何yaml文件,所以没用docker-compose。dapr的安装看官网,还有初始化,安装完docker ps看看这几个服务在不在。
dapr run --app-id clientservice --dapr-http-port 5882 --app-port 5883 dotnet Client.dll dapr run --app-id getwf --dapr-http-port 5880 --app-port 5881 dotnet Server.dll //服务之间没有用httpclient调用,用特有的dapr调用。 var daprClient = new DaprClientBuilder().Build(); var content = daprClient.InvokeMethodAsync<IEnumerable<WeatherForecast>>(HttpMethod.Get, "getwf", "WeatherForecast").Result; _logger.LogInformation($"获取wf成功:{content.ToArray().ToString()}");
查看dapr list可以看到有两个服务在运行中。正常情况我们调用虚拟机ip:5883/weatherforecast就可以了正常访问客户端拿到数据,但是很不幸我失败了,而且还没找到原因。
以上的部署仅仅体现它的服务之间是怎么调用的代码实现。它的其他核心功能状态管理、缓存、异步通信、分布式锁、链路、监控、安全等一系列中间件几乎涵盖了微服务的零零碎碎。
以前一直以为这个是运维的管理工具,去研究实践才认识到代码层面也是需要大量时间学习,就是各种中间件的使用。作为开发不去学习确实有点跟不上时代了。从docker、k8s 到dapr,对于面试开发也挺不容易。