写这篇博客主要目的是记录一下本身的学习过程,只能是简单入门级别的,由于水平有限就写到哪算哪吧,写的不对之处欢迎指正。
代码放在:https://github.com/xiajingren/NetCoreMicroserviceDemohtml
关于微服务的概念解释网上有不少...
我的理解,微服务是一种系统架构模式,它和语言无关,和框架无关,和工具无关,和服务器环境无关...
微服务思想是将传统的单体系统按照业务拆分红多个职责单1、且可独立运行的接口服务,各个服务之间不耦合。至于服务如何拆分,没有明确的定义。
几乎任何后端语言都能作微服务开发。
微服务也并非天衣无缝的,微服务架构会带来更多的问题,增长系统的复杂度,引入更多的技术栈...nginx
一个客户端,一个产品服务,一个订单服务。3个项目都是asp.net core web应用程序。建立项目的时候记得启用一下Docker支持,或者后面添加也行。git
为产品、订单服务添加一些基础代码,就简单的返回一下 服务名称,当前时间,服务的ip、端口。
github
为了方便,我使用Docker来运行服务,不用Docker也行,关于docker的安装及基本使用就不介绍了。web
在项目根目录打开PowerShell窗口执行:docker build -t productapi -f ./Product.API/Dockerfile .
Successfully表明build成功了。docker
执行:docker run -d -p 9050:80 --name productservice productapi
数据库
执行:docker ps
查看运行的容器:
小程序
没问题,使用浏览器访问一下接口:
也没问题,其中的ip端口是Docker容器内部的ip端口,因此端口是80,这个无所谓。后端
build镜像:docker build -t orderapi -f ./Order.API/Dockerfile .
运行容器:docker run -d -p 9060:80 --name orderservice orderapi
浏览器访问一下:
OK,订单服务也部署完成了。api
客户端我这里只作了一个web客户端,实际多是各类业务系统、什么PC端、手机端、小程序。。。这个明白就好,为了简单就不搞那么多了。
由于客户端须要http请求服务端接口,因此须要一个http请求客户端,我我的比较习惯RestSharp,安利一波:https://github.com/restsharp/RestSharp
添加基础代码:
IServiceHelper.cs:
public interface IServiceHelper { /// <summary> /// 获取产品数据 /// </summary> /// <returns></returns> Task<string> GetProduct(); /// <summary> /// 获取订单数据 /// </summary> /// <returns></returns> Task<string> GetOrder(); }
ServiceHelper.cs:
public class ServiceHelper : IServiceHelper { public async Task<string> GetOrder() { string serviceUrl = "http://localhost:9060";//订单服务的地址,能够放在配置文件或者数据库等等... var Client = new RestClient(serviceUrl); var request = new RestRequest("/orders", Method.GET); var response = await Client.ExecuteAsync(request); return response.Content; } public async Task<string> GetProduct() { string serviceUrl = "http://localhost:9050";//产品服务的地址,能够放在配置文件或者数据库等等... var Client = new RestClient(serviceUrl); var request = new RestRequest("/products", Method.GET); var response = await Client.ExecuteAsync(request); return response.Content; } }
Startup.cs:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); //注入IServiceHelper services.AddSingleton<IServiceHelper, ServiceHelper>(); } // 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()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } }
HomeController.cs:
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IServiceHelper _serviceHelper; public HomeController(ILogger<HomeController> logger, IServiceHelper serviceHelper) { _logger = logger; _serviceHelper = serviceHelper; } public async Task<IActionResult> Index() { ViewBag.OrderData = await _serviceHelper.GetOrder(); ViewBag.ProductData = await _serviceHelper.GetProduct(); return View(); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }
Index.cshtml:
@{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <h1 class="display-4">Welcome</h1> <p> @ViewBag.OrderData </p> <p> @ViewBag.ProductData </p> </div>
代码比较简单,这里就不用docker了,直接控制台启动,使用浏览器访问:
中止一下订单服务:docker stop orderservice
订单服务中止,致使客户端业务系统没法获取订单数据。
要解决这个问题,很容易想到:集群。
既然单个服务实例有挂掉的风险,那么部署多个服务实例就行了嘛,只要你们不一样时全挂就行。
docker run -d -p 9061:80 --name orderservice1 orderapi
docker run -d -p 9062:80 --name orderservice2 orderapi
docker run -d -p 9051:80 --name productservice1 productapi
docker run -d -p 9052:80 --name productservice2 productapi
如今订单服务和产品服务都增长到3个服务实例。
public class ServiceHelper : IServiceHelper { public async Task<string> GetOrder() { string[] serviceUrls = { "http://localhost:9060", "http://localhost:9061", "http://localhost:9062" };//订单服务的地址,能够放在配置文件或者数据库等等... //每次随机访问一个服务实例 var Client = new RestClient(serviceUrls[new Random().Next(0, 3)]); var request = new RestRequest("/orders", Method.GET); var response = await Client.ExecuteAsync(request); return response.Content; } public async Task<string> GetProduct() { string[] serviceUrls = { "http://localhost:9050", "http://localhost:9051", "http://localhost:9052" };//产品服务的地址,能够放在配置文件或者数据库等等... //每次随机访问一个服务实例 var Client = new RestClient(serviceUrls[new Random().Next(0, 3)]); var request = new RestRequest("/products", Method.GET); var response = await Client.ExecuteAsync(request); return response.Content; } }
固然拿到这些服务地址能够本身作复杂的负载均衡策略,好比轮询,随机,权重等等 都行,甚至在中间弄个nginx也能够。这些不是重点,因此就简单作一个随机吧,每次请求来了随便访问一个服务实例。
浏览器测试一下:
能够看到请求被随机分配了。可是这种作法依然不安全,若是随机访问到的实例恰好挂掉,那么业务系统依然会出问题。
简单处理思路是:
1.若是某个地址请求失败了,那么换一个地址接着执行。
2.若是某个地址的请求连续屡次失败了,那么就移除这个地址,下次就不会访问到它了。
。。。。。。
业务系统实现以上逻辑,基本上风险就很低了,也算是大大增长了系统可用性了。
而后思考另外一个问题:
实际应用中,上层的业务系统可能很是多,为了保证可用性,每一个业务系统都去考虑服务实例挂没挂掉吗?
并且实际应用中服务实例的数量或者地址大可能是不固定的,例如双十一来了,流量大了,增长了一堆服务实例,这时候每一个业务系统再去配置文件里配置一下这些地址吗?双十一过了又去把配置删掉吗?显然是不现实的,服务必需要作到可灵活伸缩。
未完待续...