In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
The editor today takes you to understand what is the basic concept of the YoyoGomicro service framework. The knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "what is the basic concept of YoyoGomicro service framework".
Starting from a simple Web service Demo
Copypackage main
Import...
Func main () {
YoyoGo.CreateDefaultBuilder (func (router Router.IRouterBuilder) {
Router.GET ("/ info", func (ctx * Context.HttpContext) {/ / supports Group mode
Ctx.JSON (200, Context.M {"info": "ok"})
})
}) .Build () .Run () / default port number: 8080
}
Basic Framework Concepts 1.HostBuilder
HostBuilder itself is an abstract concept (class) that can derive a variety of HostBuilder.
For example: Web Host Builder, RPC Host Builder, General Host Builder and so on.
The above code uses the CreateDefaultBuilder function to create a default WebHostBuilder. Since it is the most important function by default for WebHostBuilder, it is, of course, the declared route function for Http routing. HostBuilder itself contains a number of functions that define the life cycle of a program:
UseConfiguration: used to define configuration fil
Configure: used to define ApplicationBuilder, which is a set of methods for how hypervisors are built
ConfigureServices: used to define IOC containers
OnApplicationLifeEvent: event notification used to define the life cycle of a program
Build: used to generate the final runnable Host object
2.ApplicationBuilder
If the program is loaded by a Host object, the construction of the Host is done by ApplicationBuilder. Host itself is made up of ApplicationBuilder and Server. These two objects correspond one to one, ApplicationBuilder is used for construction, and Server is used to host specific application protocols. For example, WebHost is constructed by WebApplicationBuilder and provides the ServeHTTP function, which is carried by HttpServer and completed together.
3.RouterBuilder
RouterBuilder is a route definition object derived from WebApplicationBuilder that is used to declare request handlers of types such as GET,POST,PUT,DELETE. In other words, without the creation of WebHostBuilder, there is no WebApplicationBuilder, and there is no RouterBuilder; framework that supports a variety of Server protocols.
Routing function definition:
Copyfunc (router * Router.RouterGroup) {}
The above demo completes a GET request through it and returns JSON: {"info": "ok"}.
RouterBuilder itself also supports Group, which defines a group of API in a unified URL:
Copyrouter.Group ("/ v1/api", func (router * Router.RouterGroup) {
Router.GET ("/ info", GetInfo)
Router.GET ("/ hello", GetHello)
})
With the above code, two GET request routing addresses, / v1/api/info and / v1/api/hello, are generated.
4.Host
After creating the HostBuilder, the final runnable Host object (* * Build () .Run () * *) is obtained through the Build function. While creating the Host object, it also completes the creation of the Server object, which corresponds to Host and Server one by one. Host represents a runnable host that manages the entire life cycle of the program; Server is a more specific type of service, such as Http,xxRPC. Server is more of an expression of a communication protocol.
5. Custom WebHostBuilder
In the above demo, we define a default WebHostBuilder, but it supports less content and provides only the most basic Host definition, so how to customize a WebHostBuilder, for example:
Copyfunc CreateCustomBuilder () * Abstractions.HostBuilder {
Return YoyoGo.NewWebHostBuilder ().
Configure (func (app * YoyoGo.WebApplicationBuilder) {
App.UseEndpoints (unc (router Router.IRouterBuilder) {
Router.GET ("/ info", func (ctx * Context.HttpContext) {
Ctx.JSON (200, Context.M {"info": "ok"})
})
})
}
Through customization, we can use the complete HostBuilder builder function to build the application. Then run it with the following example:
Copypackage main
Import...
Func main () {
CreateCustomBuilder () .Build () .Run () / default port: 8080
}
So far we have introduced the basic concept of the YoyoGomicro services framework: a Web service is composed of WebHostBuilder-> WebApplicationBuilder-> HttpServer-> WebHost.
Thank you for reading, the above is the whole content of "what is the basic concept of YoyoGomicro service framework". Friends who learn it, let's get started. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.