.net core cross-platform development microservice architecture based on Nginx reverse proxy service cluster load balancing

  1. Overview
  Reverse proxy (Reverse Proxy) means that the proxy server is used to accept the connection request on the Internet, and then forward the request to the server on the internal network, and return the result obtained from the server to the Internet to request the connection Client, the proxy server appears as a server externally.

  Server clustering refers to bringing many servers together to perform the same service, and it appears to the client that there is only one server. The cluster can use multiple computers to perform parallel calculations to obtain a high calculation speed, and can also use multiple computers for backup, so that any one machine is broken and the entire system can still operate normally.

  Load balancing, the English name is Load Balance, which means that the load (work task) is balanced and distributed to multiple operating units to run, such as FTP server, Web server, enterprise core application server and other major task servers, etc. So as to complete the work task collaboratively. Load balancing is built on the original network structure, which provides a transparent and inexpensive and effective method to expand the bandwidth of servers and network devices, strengthen network data processing capabilities, increase throughput, and increase network availability and flexibility.

  Nginx is a lightweight Web server / reverse proxy server and email (IMAP / POP3) proxy server, released under the BSD-like protocol. Nginx was developed by Igor Sesoyev for the second most visited Rambler.ru site (Russian: Рамблер) in Russia. The first public version 0.1.0 was released on October 4, 2004.

  Below I will create the .net core weapi service, then use the same service program source code, configure and start the service program of three different ports 5177, 5178, 5179 as a service cluster, then download and install the Nginx program, reverse proxy service cluster, through Access the Nginx server and jump to the service cluster consisting of three service programs.

  2. Create .net core webapi microservice
  I use Visual Studio 2019 .net core 3.1 development environment.
  2.1. Create .net core webapi service project

  

  2.2, create the Default api controller

  

  2.3. Add Get method to return configuration information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DefaultController : ControllerBase
    {
        private readonly ILogger<DefaultController> _logger;
        private readonly IConfiguration _configuration;

        public DefaultController(ILogger<DefaultController> logger
            , IConfiguration config)
        {
            this._logger = logger;

            this._configuration = config;
        }
        [HttpGet()]
        public OkObjectResult Get()
        {
            var result = new
            {
                id = this._configuration["ip"],
                port = this._configuration["port"],
                now = DateTime.Now
            };
            return this.Ok(result);
        }
    }
}

 

  2.4. Use the console command to start, the same service program source code, configure 5177, 5178, 5179 three different ports to form three service application clusters.
  dotnet webapp.dll --urls = "http: // *: 5177" --ip = "127.0.0.1" --port = 5177
  dotnet webapp.dll --urls = "http: // *: 5178"- ip = "127.0.0.1" --port = 5178
  dotnet webapp.dll --urls = "http: // *: 5179" --ip = "127.0.0.1" --port = 5179


  3. Download, install and configure Nginx.   I don't use Linux or virtual machines here. I just install and configure
in Windows. Download address: http://nginx.org/en/download.html. After downloading and decompressing, configure the Nginx server to listen to the local server, port 8080.

 

 

  Start the Nginx server program: start nginx.exe, after normal startup, we can visit the Nginx welcome home page on the browser:

  After the Nginx server is normal, we develop and configure Nginx reflection agent 5177, 5178, 5179 three service programs. When we visit http: // localhost: 8080, the program will jump to 5177 or 5178 or 5179 service program.


  So far, it's that simple, we have implemented .net core based on Nginx reverse proxy, service cluster load balancing. If there is something wrong, please correct me.

 

Guess you like

Origin www.cnblogs.com/henxiao25/p/12686624.html