公司域名备案网站名称,做网站服务好,修改WordPress图片上传,深圳工信部网站备案从模块化到微服务化从Pet Shop 到eShop on Container都是Microsoft在技术演进的路径上给开发者展示.Net的开发能力和架构能力的Sample工程#xff0c;Petshop的时候更多的是展现应用的分层架构#xff0c;设计的抽象与模块间的通讯。到了eShop on Container更多的关注在架构设… 从模块化到微服务化从Pet Shop 到eShop on Container都是Microsoft在技术演进的路径上给开发者展示.Net的开发能力和架构能力的Sample工程Petshop的时候更多的是展现应用的分层架构设计的抽象与模块间的通讯。到了eShop on Container更多的关注在架构设计与微服务化的下面我们先来看看eshop on Container的架构图在上图我们可以看到后端服务分成了Identity microservice(验证服务)Catalog microservice商品分类服务Ordering microservice订单服务Basket microservice购物车服务Marketing microservice市场营销服务Locations microservice地理位置信息服务在以前的分层架构中通常这些服务都是以某一模块来体现的为什么现在要将他们拆分成了各个服务呢当我们从业务场景上面来看这些服务时我们会发现每个服务的访问峰值时间区间、容量规划都是不一样的甚至实现这些服务最方便最简单的技术栈都有可能是不一样的当然强大的.net core无所不能但是公司内不同业务线上的技术储备不一样就有可能选择不同的技术实现。这是因为如果我们都将这些模块整合到了一个程序或者服务中的时候就会碰到在不同时间内服务高峰期扩展系统容量困难要不就是资源不足要不就是资源过剩。譬如抢购业务开始前大家提前个半小时登录了系统这时候系统最忙的是登录模块到了开始抢购时间系统最忙的是订单模块。不采用微服务架构的话半小时前准备给登录模块使用的资源不一定能够及时的释放出来给订单模块。如果两个模块都使用单一程序架构的话很可能出现的情况就是抢购的业务把所有资源都占满了了连其他正常访问系统的用户资源都被占用掉导致系统崩溃。在讲究Dev/Ops的今天开发人员和架构师需要更多的考虑硬件架构层面对程序应用带来的影响。用Service Fabric来承载eShop on Container微服务的方法一通过Service Fabric直接管理Docker首先我们先到Azure上申请一个Container Registry来承载eShop各个微服务程序的镜像(image).创建Azure Docker Registry可以参考官方文档https://docs.microsoft.com/zh-cn/azure/container-registry/现在最新版本Service Fabric已经可以直接管理编排Docker了。1.创建一个类型为Container的Service2.在servicemanifest.xml中描述清楚image所在路径CodePackage NameCode Version1.0.0 !-- Follow this link for more information about deploying Windows containers to Service Fabric: https://aka.ms/sfguestcontainers -- EntryPoint ContainerHost ImageNameeshopsample.azurecr.io/catalog:latest/ImageName /ContainerHost /EntryPoint !-- Pass environment variables to your container: -- EnvironmentVariables EnvironmentVariable NameHttpGatewayPort Value/ /EnvironmentVariables /CodePackage这里非常简单指定了image所在位置就好了如果本身Docker Image里需要很多配置信息譬如数据库链接串、其他服务的地址等等都可以在EnvironmentVariables里面去配置。3.配置Registry的访问账号密码,需要在ApplicationManifest.xml上面来配置ServiceManifestImport ServiceManifestRef ServiceManifestNameCatalogService_Pkg ServiceManifestVersion1.0.1 / Policies ContainerHostPolicies CodePackageRefCode Isolationhyperv RepositoryCredentials AccountNameyouraccount Passwordxxxxxxxxxxxxx PasswordEncryptedfalse/ PortBinding ContainerPort80 EndpointRefCatalogServieEndpoint/ /ContainerHostPolicies /Policies /ServiceManifestImport整个过程不会太复杂只要配置好了Catalog microserivce的ServiceManifest.xm和ApplicationManifest.xml文件之后我们可以用同样的方法将其他服务一一配置完成然后我们就可以将Service Fabric的配置Publish到Cluster上面了。Service Fabric会自动根据配置在Cluster上面Pull Image和将Docker运行起来。非常简单用Service Fabric承载eShop on Container微服务的方法二用Service Fabric的Runtime运行eShop on Container的微服务Service Fabric本身就是个微服务的开发框架现在已经直接支持了.net Core 2.0了所以我们更新了Service Fabric的SDK之后就可以直接创建.net core的服务了eShop on Container的代码都已经是一份成型的.net core 2.0的代码所以不需要重新编写服务。1.通过nuget添加最新的Service Fabric最新的SDK。2.修改programe.cs,启动ServiceFabric Runtime而不是直接启动Asp.net WebHostpublic static void Main(string[] args) { try { // ServiceManifest.XML 文件定义一个或多个服务类型名称。 // 注册服务会将服务类型名称映射到 .NET 类型。 // 在 Service Fabric 创建此服务类型的实例时 // 会在此主机进程中创建类的实例。 ServiceRuntime.RegisterServiceAsync(Catalog.API, context new CatalogAPI(context)).GetAwaiter().GetResult(); ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(CatalogAPI).Name); // 防止此主机进程终止以使服务保持运行。 Thread.Sleep(Timeout.Infinite); } catch (Exception e) { ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString()); throw; }}3.编写CatalogAPI 类用于启动WebHostinternal sealed class CatalogAPI : StatelessService { public CatalogAPI(StatelessServiceContext context) : base(context) { } /// summary /// Optional override to create listeners (like tcp, http) for this service instance. /// /summary /// returnsThe collection of listeners./returns protected override IEnumerableServiceInstanceListener CreateServiceInstanceListeners() { return new ServiceInstanceListener[] { new ServiceInstanceListener(serviceContext new KestrelCommunicationListener(serviceContext, ServiceEndpoint, (url, listener) { ServiceEventSource.Current.ServiceMessage(serviceContext, $Starting WebListener on {url}); return new WebHostBuilder() .UseKestrel() .ConfigureServices( services services .AddSingletonStatelessServiceContext(serviceContext)) .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((builderContext, config) { IHostingEnvironment env builderContext.HostingEnvironment; config.AddJsonFile(settings.json, optional: false, reloadOnChange: true) .AddJsonFile($appsettings.{env.EnvironmentName}.json, optional: true, reloadOnChange: true); }) .UseStartupStartup() .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None) .UseUrls(url) .UseWebRoot(Pics) .Build(); })) }; } }4.编写serviceManifest.xml描述服务端口等信息?xml version1.0 encodingutf-8?ServiceManifest NameCatalog.APIPkg Version1.0.3 xmlnshttp://schemas.microsoft.com/2011/01/fabric xmlns:xsdhttp://www.w3.org/2001/XMLSchema xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance ServiceTypes StatelessServiceType ServiceTypeNameCatalog.API / /ServiceTypes !-- Code package is your service executable. -- CodePackage NameCode Version1.0.3 EntryPoint ExeHost ProgramCatalog.API.exe/Program WorkingFolderCodePackage/WorkingFolder /ExeHost /EntryPoint EnvironmentVariables EnvironmentVariable NameASPNETCORE_ENVIRONMENT ValueDevelopment/ /EnvironmentVariables /CodePackage ConfigPackage NameConfig Version1.0.1 / Resources Endpoints Endpoint Protocolhttp NameServiceEndpoint TypeInput Port5101 / /Endpoints /Resources/ServiceManifest5.修改AppcationManifest.xml增加几个服务的描述信息添加ServiceImport节ServiceManifestImportServiceManifestRef ServiceManifestNameCatalog.APIPkg ServiceManifestVersion1.0.3 /ConfigOverrides //ServiceManifestImport在DefaultService中描述ServiceService NameCatalog.API ServiceDnsNamecatalog.fabric.api StatelessService ServiceTypeNameCatalog.API InstanceCount[Catalog.API_InstanceCount] SingletonPartition / /StatelessService /Service这样我们就可以将Catalog这个服务改造成可以通过Service Fabric来管理的微服务了。通过Publish我们可看到几个服务都已经在Service Fabric下面接受管理和编排了。访问localhost:5100原文地址:https://www.cnblogs.com/wing-ms/p/8243020.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com