Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Service Fabric to carry eShop On Containers

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Service Fabric to carry eShop On Containers". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Service Fabric to carry eShop On Containers".

From Modularization to Micro-service

From Pet Shop to eShop on Container are Sample projects in which Microsoft shows developers the development and architecture capabilities of .net on the path of technology evolution. Petshop is more about showing the hierarchical architecture of the application, the abstraction of design and the communication between modules. When it comes to eShop on Container's focus on architecture design and microservice, let's take a look at the architecture diagram of eshop on Container.

In the figure above, we can see that the back-end service is divided into

Identity microservice (Verification Service)

Catalog microservice (Commodity Classification Service)

Ordering microservice (order Service)

Basket microservice (Shopping cart Service)

Marketing microservice (Marketing Services)

Locations microservice (Geographic location Information Service)

In the previous hierarchical architecture, these services were usually represented by a certain module, so why should they be split into 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, and even the most convenient and simple technology stack to implement these services may be different (of course, powerful. Net core is omnipotent, but different business lines within the company have different technology reserves, so it is possible to choose different technology implementations). This is because if we all integrate these modules into a program or service, we will encounter difficulties in expanding system capacity during peak service periods at different times, either insufficient resources or excess resources. For example, before the panic buying business starts, everyone logs in to the system one and a half hours in advance. At this time, the busiest part of the system is the login module. When it comes to the rush buying time, the busiest part of the system is the order module. Without the micro-service architecture, the resources intended for the login module half an hour ago may not be released to the order module in time. If both modules use a single program architecture, it is likely that all the resources are occupied by the rush-buying business, and even other user resources that normally access the system are occupied, resulting in a system crash. In today's Dev/Ops era, developers and architects need to think more about the impact of hardware architecture on program applications.

The first method of using Service Fabric to host eShop on Container micro-services is to directly manage Docker through Service Fabric

First, let's apply for a Container Registry on Azure to host the image of each eShop microservice program. To create an Azure Docker Registry, please refer to the official document: https://docs.microsoft.com/zh-cn/azure/container-registry/

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

1. Create a Service of type Container

two。 Clearly describe the path where image is located in servicemanifest.xml

Eshopsample.azurecr.io/catalog:latest

It is very simple to specify the location of the image. If you need a lot of configuration information in the Docker Image, such as database link strings, addresses of other services, and so on, you can configure them in the EnvironmentVariables.

3. Configure the access account password of Registry, which needs to be configured on ApplicationManifest.xml.

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

Service Fabric will automatically Pull Image and run Docker on top of Cluster based on configuration. It's simple

The second method of using Service Fabric to host eShop on Container micro-service: running eShop on Container micro-service with Service Fabric's Runtime

Service Fabric itself is a micro-service development framework, which is now directly supported. Net Core 2.0.So, after we update the SDK of Service Fabric, we can create the service of .net core directly.

The eShop on Container code is already a formed. Net core 2.0 code, so there is no need to rewrite the service.

1. Add the latest Service Fabric and the latest SDK through nuget.

two。 Modify programe.cs to start ServiceFabric Runtime instead of starting Asp.net WebHost directly

Public static void Main (string [] args)

{

Try

{

The / / ServiceManifest.XML file defines one or more service type names.

/ / Registration service maps the service type name to the .NET type.

/ / when Service Fabric creates an instance of this service type

An instance of the class 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)

/ / prevent this 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)

{}

/ / /

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

/ / /

/ The collection of listeners.

Protected override IEnumerable 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 (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 ()

.UseService FabricIntegration (listener, ServiceFabricIntegrationOptions.None)

.UseUrls (url)

.UseWebRoot ("Pics")

.build ()

}))

}

}

}

4. Write serviceManifest.xml to describe the service port and other information

Catalog.API.exe

CodePackage

5. Modify AppcationManifest.xml to add description information for several services

Add a ServiceImport section

Describe Service in DefaultService

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

Visit localhost:5100

Thank you for reading, the above is the content of "how to use Service Fabric to carry eShop On Containers". After the study of this article, I believe you have a deeper understanding of how to use Service Fabric to carry eShop On Containers, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report