日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用Service Fabric承载eShop On Containers

發(fā)布時(shí)間:2023/12/4 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用Service Fabric承载eShop On Containers 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

從模塊化到微服務(wù)化


從Pet Shop 到eShop on Container都是Microsoft在技術(shù)演進(jìn)的路徑上給開發(fā)者展示.Net的開發(fā)能力和架構(gòu)能力的Sample工程,Petshop的時(shí)候更多的是展現(xiàn)應(yīng)用的分層架構(gòu),設(shè)計(jì)的抽象與模塊間的通訊。到了eShop on Container更多的關(guān)注在架構(gòu)設(shè)計(jì)與微服務(wù)化的,下面我們先來看看eshop on Container的架構(gòu)圖

在上圖,我們可以看到后端服務(wù)分成了

  • Identity microservice(驗(yàn)證服務(wù))

  • Catalog microservice(商品分類服務(wù))

  • Ordering microservice(訂單服務(wù))

  • Basket microservice(購物車服務(wù))

  • Marketing microservice(市場營銷服務(wù))

  • Locations microservice(地理位置信息服務(wù))

  • 在以前的分層架構(gòu)中,通常這些服務(wù)都是以某一模塊來體現(xiàn)的,為什么現(xiàn)在要將他們拆分成了各個(gè)服務(wù)呢?當(dāng)我們從業(yè)務(wù)場景上面來看這些服務(wù)時(shí),我們會發(fā)現(xiàn)每個(gè)服務(wù)的訪問峰值時(shí)間區(qū)間、容量規(guī)劃都是不一樣的,甚至實(shí)現(xiàn)這些服務(wù)最方便最簡單的技術(shù)棧都有可能是不一樣的(當(dāng)然強(qiáng)大的.net core無所不能,但是公司內(nèi)不同業(yè)務(wù)線上的技術(shù)儲備不一樣,就有可能選擇不同的技術(shù)實(shí)現(xiàn))。這是因?yàn)槿绻覀兌紝⑦@些模塊整合到了一個(gè)程序或者服務(wù)中的時(shí)候,就會碰到在不同時(shí)間內(nèi)服務(wù)高峰期擴(kuò)展系統(tǒng)容量困難,要不就是資源不足,要不就是資源過剩。譬如搶購業(yè)務(wù)開始前大家提前個(gè)半小時(shí)登錄了系統(tǒng),這時(shí)候系統(tǒng)最忙的是登錄模塊,到了開始搶購時(shí)間,系統(tǒng)最忙的是訂單模塊。不采用微服務(wù)架構(gòu)的話,半小時(shí)前準(zhǔn)備給登錄模塊使用的資源不一定能夠及時(shí)的釋放出來給訂單模塊。如果兩個(gè)模塊都使用單一程序架構(gòu)的話,很可能出現(xiàn)的情況就是搶購的業(yè)務(wù)把所有資源都占滿了了,連其他正常訪問系統(tǒng)的用戶資源都被占用掉,導(dǎo)致系統(tǒng)崩潰。在講究Dev/Ops的今天,開發(fā)人員和架構(gòu)師需要更多的考慮硬件架構(gòu)層面對程序應(yīng)用帶來的影響。

    用Service Fabric來承載eShop on Container微服務(wù)的方法一,通過Service Fabric直接管理Docker

    首先我們先到Azure上申請一個(gè)Container Registry來承載eShop各個(gè)微服務(wù)程序的鏡像(image).創(chuàng)建Azure Docker Registry可以參考官方文檔:https://docs.microsoft.com/zh-cn/azure/container-registry/

    現(xiàn)在最新版本Service Fabric已經(jīng)可以直接管理編排Docker了。

    1.創(chuàng)建一個(gè)類型為Container的Service

    2.在servicemanifest.xml中描述清楚image所在路徑

    <CodePackage Name="Code" Version="1.0.0">


    ? ? <!-- Follow this link for more information about deploying Windows containers to Service Fabric: https://aka.ms/sfguestcontainers -->

    ? ? <EntryPoint>

    ??

    ? ? ? <ContainerHost>

    ? ? ? ? <ImageName>eshopsample.azurecr.io/catalog:latest</ImageName> ? ? ??

    ? ? ? </ContainerHost> ? ? ?

    ? ? </EntryPoint>

    ? ? <!-- Pass environment variables to your container: --> ??

    ? ? <EnvironmentVariables>

    ? ? ? <EnvironmentVariable Name="HttpGatewayPort" Value=""/>

    ? ? </EnvironmentVariables>

    ? </CodePackage>

    這里非常簡單,指定了image所在位置就好了,如果本身Docker Image里需要很多配置信息譬如:數(shù)據(jù)庫鏈接串、其他服務(wù)的地址等等都可以在EnvironmentVariables里面去配置。

    3.配置Registry的訪問賬號密碼,需要在ApplicationManifest.xml上面來配置

    <ServiceManifestImport>

    ? ? <ServiceManifestRef ServiceManifestName="CatalogService_Pkg" ?ServiceManifestVersion="1.0.1" /> ? ? ?

    ? ? <Policies>

    ? ? ? <ContainerHostPolicies CodePackageRef="Code" Isolation="hyperv">

    ? ? ? ? <RepositoryCredentials AccountName="youraccount" Password="xxxxxxxxxxxxx" PasswordEncrypted="false"/>

    ? ? ? ? <PortBinding ContainerPort="80" EndpointRef="CatalogServieEndpoint"/>

    ? ? ??

    ? ? ? </ContainerHostPolicies>

    ? ? </Policies>

    ? </ServiceManifestImport>

    整個(gè)過程不會太復(fù)雜,只要配置好了Catalog microserivce的ServiceManifest.xm和ApplicationManifest.xml文件之后,我們可以用同樣的方法將其他服務(wù)一一配置完成,然后我們就可以將Service Fabric的配置Publish到Cluster上面了。

    Service Fabric會自動根據(jù)配置在Cluster上面Pull Image和將Docker運(yùn)行起來。非常簡單

    用Service Fabric承載eShop on Container微服務(wù)的方法二:用Service Fabric的Runtime運(yùn)行eShop on Container的微服務(wù)

    Service Fabric本身就是個(gè)微服務(wù)的開發(fā)框架,現(xiàn)在已經(jīng)直接支持了.net Core 2.0了所以,我們更新了Service Fabric的SDK之后就可以直接創(chuàng)建.net core的服務(wù)了

    eShop on Container的代碼都已經(jīng)是一份成型的.net core 2.0的代碼,所以不需要重新編寫服務(wù)。

    1.通過nuget添加最新的Service Fabric最新的SDK。

    2.修改programe.cs,啟動ServiceFabric Runtime而不是直接啟動Asp.net WebHost

    public static void Main(string[] args)

    ? ? ? ? {


    ? ? ? ? ? ? try

    ? ? ? ? ? ? {

    ? ? ? ? ? ? ? ? // ServiceManifest.XML 文件定義一個(gè)或多個(gè)服務(wù)類型名稱。

    ? ? ? ? ? ? ? ? // 注冊服務(wù)會將服務(wù)類型名稱映射到 .NET 類型。

    ? ? ? ? ? ? ? ? // 在 Service Fabric 創(chuàng)建此服務(wù)類型的實(shí)例時(shí),

    ? ? ? ? ? ? ? ? // 會在此主機(jī)進(jìn)程中創(chuàng)建類的實(shí)例。


    ? ? ? ? ? ? ? ? ServiceRuntime.RegisterServiceAsync("Catalog.API",

    ? ? ? ? ? ? ? ? ? ? context => new CatalogAPI(context)).GetAwaiter().GetResult();


    ? ? ? ? ? ? ? ? ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(CatalogAPI).Name);


    ? ? ? ? ? ? ? ? // 防止此主機(jī)進(jìn)程終止,以使服務(wù)保持運(yùn)行。?

    ? ? ? ? ? ? ? ? Thread.Sleep(Timeout.Infinite);

    ? ? ? ? ? ? }

    ? ? ? ? ? ? catch (Exception e)

    ? ? ? ? ? ? {

    ? ? ? ? ? ? ? ? ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());

    ? ? ? ? ? ? ? ? throw;

    ? ? ? ? ? ? }

    }

    3.編寫

    CatalogAPI 類用于啟動WebHost

    internal sealed class CatalogAPI : StatelessService

    ? ? {

    ? ? ? ? public CatalogAPI(StatelessServiceContext context)

    ? ? ? ? ? ? : base(context)

    ? ? ? ? { }


    ? ? ? ? /// <summary>

    ? ? ? ? /// Optional override to create listeners (like tcp, http) for this service instance.

    ? ? ? ? /// </summary>

    ? ? ? ? /// <returns>The collection of listeners.</returns>

    ? ? ? ? protected override IEnumerable<ServiceInstanceListener> 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

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .AddSingleton<StatelessServiceContext>(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);

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? })

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseStartup<Startup>()

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseUrls(url)

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseWebRoot("Pics")

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .Build(); ? ? ? ? ? ? ? ? ?

    ? ? ? ? ? ? ? ? ? ? }))

    ? ? ? ? ? ? };

    ? ? ? ? }

    ? ? }

    4.編寫serviceManifest.xml描述服務(wù)端口等信息

    <?xml version="1.0" encoding="utf-8"?>

    <ServiceManifest Name="Catalog.APIPkg"

    ? ? ? ? ? ? ? ? ?Version="1.0.3"

    ? ? ? ? ? ? ? ? ?xmlns="http://schemas.microsoft.com/2011/01/fabric"

    ? ? ? ? ? ? ? ? ?xmlns:xsd="http://www.w3.org/2001/XMLSchema"

    ? ? ? ? ? ? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    ? <ServiceTypes>

    ? ? ? ? <StatelessServiceType ServiceTypeName="Catalog.API" />

    ? </ServiceTypes>


    ? <!-- Code package is your service executable. -->

    ? <CodePackage Name="Code" Version="1.0.3">

    ? ? <EntryPoint>

    ? ? ? <ExeHost>

    ? ? ? ? <Program>Catalog.API.exe</Program>

    ? ? ? ? <WorkingFolder>CodePackage</WorkingFolder>

    ? ? ? </ExeHost>

    ? ? </EntryPoint>

    ? ? <EnvironmentVariables>

    ? ? ? <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="Development"/>

    ? ? </EnvironmentVariables>

    ? </CodePackage>



    ? <ConfigPackage Name="Config" Version="1.0.1" />


    ? <Resources>

    ? ?

    ? ? <Endpoints> ??

    ??

    ? ? ? <Endpoint Protocol="http" Name="ServiceEndpoint" ?Type="Input" ?Port="5101" />

    ? ? </Endpoints>

    ? </Resources>

    </ServiceManifest>

    5.修改AppcationManifest.xml增加幾個(gè)服務(wù)的描述信息

    添加ServiceImport節(jié)

    <ServiceManifestImport><ServiceManifestRef ServiceManifestName="Catalog.APIPkg" ServiceManifestVersion="1.0.3" /><ConfigOverrides /></ServiceManifestImport>

    在DefaultService中描述Service

    <Service Name="Catalog.API" ServiceDnsName="catalog.fabric.api">

    ? ? ? <StatelessService ServiceTypeName="Catalog.API" InstanceCount="[Catalog.API_InstanceCount]">

    ? ? ? ? <SingletonPartition />

    ? ? ? </StatelessService>

    ? ? </Service>

    這樣我們就可以將Catalog這個(gè)服務(wù)改造成可以通過Service Fabric來管理的微服務(wù)了。通過Publish,我們可看到幾個(gè)服務(wù)都已經(jīng)在Service Fabric下面接受管理和編排了。

    訪問localhost:5100


    原文地址:https://www.cnblogs.com/wing-ms/p/8243020.html


    .NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

    總結(jié)

    以上是生活随笔為你收集整理的利用Service Fabric承载eShop On Containers的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。