0. Preface
Overall architecture directory: ASP. NET core distributed project-Directory
First, prepare
prerequisites need to download and install Consul, project needs to understand add swagger
Unified in the gateway to configure multiple microservices swagger, need to use the service registration and discovery, of course ocelot inside and have the service discovery, so need to put the ASP. NET core Project first registered in Consul.
The landlord's project is used to consul. The following case I put on the window system, the formal environment needs to deploy the project to its own server.
1, explanation: What is called Service registration and discovery
In the framework of micro-service, service discovery is a module that cannot be mentioned. I believe that knowing or being familiar with the micro-service of children's shoes should be aware of its importance. Here I simply mention, after all, this is not our focus. Let's look at one of the following pictures:
In the diagram, an interface for the client needs to invoke the service a-n. The client must know the network location of all the services, the previous practice is that the configuration is in the configuration file, or some configuration is in the database. Here are a few questions:
Need to configure network locations for n services to increase configuration complexity
Changes in the network location of the service need to change the configuration of each caller
In the case of clustering, it is difficult to do load (except in the way of reverse proxy)
Summed up a sentence: more services, configuration is troublesome, a lot of problems
Since there are these problems, service discovery is the solution to these problems. Say, how to solve it? Let's look at one more picture.
Unlike the previous one, a service discovery module was added. The diagram is relatively simple, here the text description under. Service A-n to register the current network location to the Service Discovery module (here to register the meaning is to tell), the service found in the way K-v records, K is generally the service name, V is ip:port. The service Discovery module periodically polls to see if these services can be accessed (this is the health check). When the client invokes the service A-n, it runs to the service Discovery module and asks for their network location before invoking their service. Is this a way to solve the problem above? The client does not need to log these service network locations at all, and the client and server are fully decoupled!
This process is generally the case, of course, the Service Discovery module is not so simple. It contains a lot of stuff. This expression is only convenient to understand.
The service discovery module in the figure is basically the role of service discovery in the MicroServices architecture.
ii. start of deployment
First create an ASP. NET core Web API project that I have created. Then add the Nugut package consul.
1, in the project inside the Startup.cs file inside write our service registration, I put the registration information in the Appsetting.json file inside.
Then write the service registration in Configureservices.
The Configure method writes the service registration.
Consulapp Method:
Registration Service Registerservice
At this point, Consul registration is OK.
2, configuration swagger specific content
I put Swagger's micro-service information inside the Appsetting.json.
So the data in the Startup.cs is obtained from this side.
The Configureservices method is always filled in as follows:
The contents of "" in the inside of it can be replaced.
The Configure method fills in the following content:
At this point, the swagger configuration is complete.
3. The next step is to deploy the contents of the Ocelot gateway.
The Apigateway project also creates an ASP. NET core Web API project, and then adds the Swaggernuget package.
And then write it in the Configureservices method.
Then write in the Configure method:
The contents of the routing table are:
Since this configuration is complete.
third, the Operation1. Run Consul first
Open Browser URL address: Consul run successfully.
2, run Userapi Project and Apigateway project.
Found one more Userapi service registration in consul
Then open the Apigateway swagger project, you can see the Userapi interface, to complete the configuration.
Specific swagger How to configure, you can refer to my previous article (hard to write)
Reference URL:
Http://www.cnblogs.com/focus-lei/p/9047410.html
asp:787464275 Welcome Dabigatran AC
If you think this article is good or something, you can click on the "Recommend" button in the bottom right corner to support the spirit, because this support is the biggest motivation for me to continue to write and share!
Louieguo
Disclaimer: Original Blog Please keep the original link or at the beginning of the article add my blog address, such as found errors, welcome criticism. General in my article, can not set a reward function, if there are special needs please contact me!