When building a large application, we usually need to implement service-oriented and modular design for the application, and divide the layers and functions of the application. During application deployment, server hardware is configured at different levels based on the load type and quantity, then, install the corresponding software environment on different servers, such as Web server software, middleware, and database software, and then deploy the application code to be extremely configured. In Windows Azure, application servers also follow similar design principles. However, to simplify the deployment of application servers, Windows Azure provides different types of runtime environment templates based on application functions and features, these different running environment templates correspond to different computing roles in Windows Azure. A user's Windows Azure service role is defined in the corresponding service model. Each Service can define one or more service roles as needed. The definition of each service role includes the role type, virtual machine size, communication endpoint, and local storage information. From the perspective of developers, we can think that the service role of Windows Azure is composed of service definition, service configuration, and service code. Windows Azure provided Web Role and Worker Role when it was just launched. Later, a third Role VM Role was added to reduce the difficulty of migrating old applications. For VM Role, its service code is embodied by a VHD image containing the application.
To simplify the development of Windows Azure applications by developers, after installing corresponding development plug-ins in Visual Studio, templates with different roles will appear under the cloud computing option in the new project template, as shown in
The following is a detailed introduction.
1. ASP. NET Web Role. This role template is mainly used to create an Application Based on ASP. NET Web.
2. ASP. net mvc Web Role. This role template is used to create an ASP. net mvc Web program.
3. WCF Service Web Role. This role is used to create a hosted WCF Service.
4. Worker Role. This Role template is used to create a Worker-based application. If the application service to be developed is not suitable for the previous Role template, the Worker Role can generally achieve this requirement. Because Worker Role is very flexible in design, developers can write almost any code here.
The four Role templates listed above are essentially Web Role templates, but classification makes application development easier. Applications developed using these Role templates will eventually run as Web Role instances in the Windows Azure environment.
Role instance
In object-oriented programming, developers Define the basic attributes and action requirements required by objects through classes. This is a static description. Then, a specific dynamic object is generated based on the class during the application running process. A class can generate one or more objects as needed. The specific functions of an application are implemented by the pain points and call methods between different objects.
In terms of concept understanding, we can compare service roles and instances with the relationship between classes and objects in object-oriented programming. If you think of a role as a running environment template or type, the role instance is an instance generated based on this template. In the end, the user's application service runs on Windows Azure by role instances, and all user requests are handled by these role instances. Application Services in Windows Azure can contain one or more service roles, and each role can have one or more instances.
Role instances in Windows Azure are independent operating units of user services. Although these Role instances are not directly bound to the underlying virtual machine, in the current implementation of Windows Azure, whether it is Web Role, Worker Role or VM Role, each of their instances corresponds to a specific virtual machine, that is, the instance and the virtual machine have a one-to-one relationship. Therefore, a role instance is a virtual machine that deploys User Service Code, service configuration, and local data.
Each published service has a virtual IP address mapped to the * .cloudapp.net domain name. Then, the Server Load balancer distributes user requests to different instances for processing. How does the Server Load balancer distribute access from external users to each instance? In fact, each service instance has its own IP address. This IP address is called a direct IP address, which is a private IP address. That is to say, the address cannot be accessed outside the windows azure data center. They are mainly used for internal communication, including communication between different instances, and communication between Server Load balancer and instances. The distribution rules of Server Load balancer are mainly based on the ing between virtual IP addresses and direct IP addresses. If the Service Code requires a specific address, it can be obtained through a dedicated programming interface.
Since there is a one-to-one relationship between the instance and the virtual machine, the instance size is also the size of the virtual machine. As shown in the following settings, WebRole1 is specified to use ultra-large scale instances, that is, virtual machine configurations.
<WebRole name = "webRole1" vmsize = "ExtraLarge">
Role Lifecycle
Role instances have their own lifecycles on Windows Azure. They can be divided into three stages: Start, Run, and stop. Both Web Role and Worker Role inherit the same basic RoleEntryPoint in implementation.
RoleEntryPoint has three state-related methods.
1. public virtual bool OnStart ();
This method is called by Windows Azure when initializing a role instance. If the initialization is successful, True is returned; otherwise, False is returned. Developers can override this method and add their own initialization code.
2. public virtual void Run ();
If the role instance is successfully initialized, Windows Azure will call this method to start running the role instance. This method should implement a long running ready-made role operation. By default, sleep is performed in a wireless loop. The status of the role instance during this method is "ready", that is, the request can be processed normally. If the role is returned from the Run () method, the instance will be recycled by Windows Azure.
3. public virtual void OnStop ();
This method is called by Windows Azure before stopping the instance. If you need to do some cleanup before stopping, developers can overwrite this method and put their own cleanup code in this method. This method must be returned within a certain period of time (currently 30 seconds), otherwise Windows Azure will forcibly stop the instance. For ASP. NET Web Role, the cleanup code can also be written in the Application () method of ASP. NET. This method is called before the OnStop () method.
From the heart, such as water, wood, and habitat