Using Service Fabric to carry eShop On Containers, eshopcontainers

Source: Internet
Author: User
Tags webhost docker registry

Using Service Fabric to carry eShop On Containers, eshopcontainers

From Pet Shop to eShop on Container, Microsoft presents the technology evolution path to developers. the Sample project of Net's development and architecture capabilities, Petshop is more about displaying the layered architecture of applications, design abstraction and communication between modules. EShop on Container focuses more on Architecture Design and microservices. Let's take a look at the architecture diagram of eshop on Container.

In, we can see that the backend service is divided

1 Identity microservice (Authentication Service)

2 Catalog microservice)

3 Ordering microservice)

4 Basket microservice (Shopping Cart Service)

5. Marketing microservice)

6 Locations microservice (geographic location information service)

In the previous layered architecture, these services are usually embodied in a certain module. Why should they be split into various services now? When we look at these services from the business scenario, we will find that the access peak time interval and capacity planning of each service are different, even the most convenient and simple technology stack for implementing these services may be different (of course powerful. net core is omnipotent, but the technical reserves of different online businesses in the company are different, it is possible to choose different technical implementation ). This is because if we integrate these modules into a program or service, we will encounter difficulties in expanding the system capacity during service peaks in different time periods, or lack of resources, or excessive resources. For example, you logged on to the system half an hour before the start of the flash sales business. The busiest part of the system was the logon module. When the flash sales started, the busiest part of the system was the order module. If the microservice architecture is not used, resources that are prepared for the login module half an hour ago may not be promptly released to the order module. If both modules use a single program architecture, it is likely that all resources are occupied by the flash sales business, and other user resources that normally access the system are occupied, cause system crash. With regard to Dev/Ops, developers and architects need to consider the impact of the hardware architecture layer on application.

Method 1 of using Service Fabric to carry eShop on Container microservice, using Service Fabric to directly manage Docker

First, we first apply for a Container Registry on Azure to host the image of each eShop microservice program. Create Azure Docker Registry refer to the official documentation: https://docs.microsoft.com/zh-cn/azure/container-registry/

Now, the latest Service Fabric version can directly manage the orchestration Docker.

1. Create a Service of the Container type

2. Describe the path of the image in servicemanifest. xml.

<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>

It is very simple to specify the location of the image. If Docker Image requires a lot of configuration information, such as the database connection string and other service addresses, you can configure it in EnvironmentVariables.

3. Configure the access account password for the Registry, which must be configured on 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>

The whole process will not be too complex, as long as the ServiceManifest of Catalog microserivce is configured. xm and ApplicationManifest. after the xml file, we can configure other services one by one in the same way, and then we can configure the Service Fabric Publish to the Cluster.

Service Fabric automatically runs the Pull Image on the Cluster according to the configuration and Docker. Very simple

Method 2: Use Service Fabric's Runtime to run eShop on Container's microservice

Service Fabric is a microservice development framework and is now directly supported. net Core 2.0. Therefore, we can directly create the SDK after updating the Service Fabric SDK. net core service

EShop on Container Code is already a. net core 2.0 code, so you do not need to rewrite the service.

1. Add the latest Service Fabric SDK through nuget.

2. Modify programe. cs and start ServiceFabric Runtime instead of starting Asp.net WebHost directly.

Public static void Main (string [] args) {try {// ServiceManifest. XML file defines one or more service type names. // The registration service maps the service type name to the. NET type. // When you create an instance of this Service type in Service Fabric, // a class instance is created in this host process. ServiceRuntime. registerServiceAsync ("Catalog. API ", context => new CatalogAPI (context )). getAwaiter (). getResult (); ServiceEventSource. current. serviceTypeRegistered (Process. getCurrentProcess (). id, typeof (CatalogAPI ). name); // prevents the host process from terminating to keep the service running. Thread. Sleep (Timeout. Infinite);} catch (Exception e) {ServiceEventSource. Current. ServiceHostInitializationFailed (e. ToString (); throw ;}}

3. Write

The CatalogAPI class is used to start 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. Write serviceManifest. xml to describe the service port and other information.

<?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. Modify AppcationManifest. xml to add descriptions of several services.

Add ServiceImport Section

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

Describe Service in defaservice Service

<Service Name="Catalog.API" ServiceDnsName="catalog.fabric.api">  <StatelessService ServiceTypeName="Catalog.API" InstanceCount="[Catalog.API_InstanceCount]">  <SingletonPartition />  </StatelessService> </Service>

In this way, we can transform the Catalog Service into a microservice that can be managed through Service Fabric. Through Publish, we can see that several services have been managed and orchestrated under Service Fabric.

Access localhost: 5100

The above Implementation Method of Using Service Fabric to carry eShop On Containers is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.