当前位置:网站首页>Implementation of nginx version of microservice architecture

Implementation of nginx version of microservice architecture

2020-11-07 16:46:00 itread01

One 、 Introduction
         In the last article 《 A brief introduction to the true face of Lushan and its technology stack 》 in , We've explored the context of microservices , The technology stack needed to realize microservice architecture is also mentioned , Today we're starting to implement a microservice , Of course, this implementation is a simplified version , In this version, it doesn't involve Docker、K8S Something to wait for , We evolved , Today's issue is just to achieve a NGINX Version of the microservice function .

       1、 Explain
             There are too many technologies involved in microservice architecture , We can't discuss it completely in another article , therefore , Let's talk about it in multiple phases , Each issue advances the relevant technology , And then , It evolved step by step . If you are Daniel , You can just skip , Because these things are relative to you , This is too simple . Special notes , All of the code here has been tested , So you can use .

       2、 Development environment
             Here's the development environment , Needless to say , It's very simple , It's easy to see .
            (1)、 Development tools :Visual Studio 2019
            (2)、 Development language :C#
            (3)、 Development platform :Net Core3.1, Cross platform .
            (4)、 Gateway service :NGINX, Focus on high performance gateways .

        3、 Today's goal
              Our goal today is to build a system based on Nginx The gateway realizes , The service instance item does not contain any container , It's just an implementation of an architecture hosted by a stand-alone program .
             

Two 、 Microservice architecture NGINX Version implementation
           
          Before the article begins , We still want to briefly explain . Last file 《 A brief introduction to the true face of Lushan and its technology stack 》 We said , There are three microservices , The difference is : Monomer architecture 、 Vertically split design and microservices , Based on SOA Let's not discuss . In fact , There is no essential difference between the second edition and the first one , It's all monomer architecture , therefore , Let's simply implement the version of microservices today , Because there are many technologies involved , I have divided the version of microservice into several versions , Today is the simplest .

          Today we are mainly discussing on the basis of NGINX Decentralized implementation 、 Advantages and disadvantages of microservice Architecture , The code for each project is posted independently , The logic is simple , Because we focus on Architecture , Let's start .

        1、 Our solution , contain 5 A project .

             (1)、PatrickLiu.MicroService.Client(ASP.NET CORE MVC), Client application .
                         This project is very simple , There are almost no changes , It's just to access the service instance item here .
                           

                           1)、startup.cs The only code added in the class

 1     public class Startup
 2     {
 3 
 4         /// <summary>
 5         ///  Register the service into the container .
 6         /// </summary>
 7         /// <param name="services"></param>
 8         public void ConfigureServices(IServiceCollection services)
 9         {
10             services.AddSingleton<IUserService, UserService>();
11         }
12     }

                           2)、HomeController.cs Class                                 

 1 public class HomeController : Controller
 2     {
 3         private readonly ILogger<HomeController> _logger;
 4         private readonly IUserService _userService;
 5         private static int index;
 6 
 7         /// <summary>
 8         ///  Initializes a new instance of the type .
 9         /// </summary>
10         /// <param name="logger"> Inject log objects .</param>
11         /// <param name="userService"> Inject user service objects .</param>
12         public HomeController(ILogger<HomeController> logger, IUserService userService)
13         {
14             _logger = logger;
15             _userService = userService;
16         }
17 
18         /// <summary>
19         ///  Home .
20         /// </summary>
21         /// <returns></returns>
22         public IActionResult Index()
23         {
24             #region 1、 Monomer architecture 
25 
26             //this.ViewBag.Users = _userService.UserAll();
27 
28             #endregion
29 
30             #region 2、 Decentralized architecture 
31 
32             #region 2.1、 Direct access to service instances 
33 
34             //string url = string.Empty;
35             //url = "http://localhost:5726/api/users/all";
36             //url = "http://localhost:5726/api/users/all";
37             //url = "http://localhost:5726/api/users/all";
38 
39             #endregion
40 
41             #region  Through  Ngnix Gateway access service example item , Preset rotation .
42 
43             string url = "http://localhost:8080/api/users/all";
44 
45             #endregion
46 
47             string content = InvokeAPI(url);
48             this.ViewBag.Users = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<User>>(content);
49             Console.WriteLine($"This is {url} Invoke.");
50             
51             #endregion
52 
53             return View();
54         }
55 
56 
57         /// <summary>
58         /// 
59         /// </summary>
60         /// <param name="url"></param>
61         /// <returns></returns>
62         public static string InvokeAPI(string url)
63         {
64             using (HttpClient client = new HttpClient())
65             {
66                 HttpRequestMessage message = new HttpRequestMessage();
67                 message.Method = HttpMethod.Get;
68                 message.RequestUri = new Uri(url);
69                 var result = client.SendAsync(message).Result;
70                 string conent = result.Content.ReadAsStringAsync().Result;
71                 return conent;
72             }
73         }
74     }
75 }

                          3)、Index.cshtml Code to view

 1 @{
 2     ViewData["Title"] = "Home Page";
 3 }
 4 
 5 <div class="text-center">
 6     <h1 class="display-4">Welcome</h1>
 7     <ul>
 8         @{
 9 
10             foreach (var item in ViewBag.Users)
11             {
12                 <li>@item.ID [email protected]  [email protected] </li>
13             }
14         }
15     </ul>
16     <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
17 </div>


             (2)、PatrickLiu.MicroService.Interfaces(NETCORE Class library project ), Define the service interface .
                          This project is very simple , Only one interface type is defined , Name :IUserService.cs.
                         

         IUserService Code for

 1 using PatrickLiu.MicroService.Models;
 2 using System.Collections.Generic;
 3 
 4 namespace PatrickLiu.MicroService.Interfaces
 5 {
 6     /// <summary>
 7     ///  User service interface definition .
 8     /// </summary>
 9     public interface IUserService
10     {
11         /// <summary>
12         ///  Query the user instance object for the specified primary key .
13         /// </summary>
14         /// <param name="id"> Primary key of the consumer .</param>
15         /// <returns> Returns the user instance object queried .</returns>
16         User FindUser(int id);
17 
18         /// <summary>
19         ///  Get instance collection of all users .
20         /// </summary>
21         /// <returns> Returns all user instances .</returns>
22         IEnumerable<User> UserAll();
23     }
24 }


             (3)、PatrickLiu.MicroService.Models (NETCORE Class library project ), Define entity class model .
                         This project is very simple , Only one entity type is defined , Class name :User.cs.
                        

                         User.cs Code for
                        

 1 using System;
 2 
 3 namespace PatrickLiu.MicroService.Models
 4 {
 5     /// <summary>
 6     ///  User model .
 7     /// </summary>
 8     public class User
 9     {
10         /// <summary>
11         ///  Get or set the user primary key .
12         /// </summary>
13         public int ID { get; set; }
14 
15         /// <summary>
16         ///  Get or set the user name .
17         /// </summary>
18         public string Name { get; set; }
19 
20         /// <summary>
21         ///  Get or set the user account name .
22         /// </summary>
23         public string Account { get; set; }
24 
25         /// <summary>
26         ///  Get or set the user password .
27         /// </summary>
28         public string Password { get; set; }
29 
30         /// <summary>
31         ///  Get or set the user's email address .
32         /// </summary>
33         public string Email { get; set; }
34 
35         /// <summary>
36         ///  Get or set the user role .
37         /// </summary>
38         public string Role { get; set; }
39 
40         /// <summary>
41         ///  Get or set the login time of the user .
42         /// </summary>
43         public DateTime LoginTime { get; set; }
44     }
45 }



             (4)、PatrickLiu.MicroService.Services(NetCore Class library project ), Implementation interface type .
                         This project is very simple , Only one type is defined , Realize IUserService Interface , Class name :UserService.cs.
                        

        UserService.cs Code for

 1 using PatrickLiu.MicroService.Interfaces;
 2 using PatrickLiu.MicroService.Models;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 
 7 namespace PatrickLiu.MicroService.Services
 8 {
 9     /// <summary>
10     ///  The implementation type that implements the user service interface .
11     /// </summary>
12     public class UserService : IUserService
13     {
14         private IList<User> dataList;
15 
16         /// <summary>
17         ///  Initializes an instance of a type 
18         /// </summary>
19         public UserService()
20         {
21             dataList = new List<User>()
22             { new User {ID=1,Name=" Huang Feihong ",Account="HuangFeiHong",Password="HuangFeiHong123456",Email="[email protected]", Role="Admin", LoginTime=DateTime.Now },
23             new User {ID=2,Name=" Hongxiguan ",Account="HongXiGuan",Password="HongXiGuan54667",Email="[email protected]", Role="Admin", LoginTime=DateTime.Now.AddDays(-5) },
24             new User {ID=3,Name=" Fang Shiyu ",Account="FangShiYu",Password="FangShiYu112233",Email="[email protected]", Role="Admin", LoginTime=DateTime.Now.AddDays(-30) },
25             new User {ID=4,Name=" Miao Cuihua ",Account="MiaoCuiHua",Password="MiaoCuiHua887766",Email="[email protected]", Role="Admin", LoginTime=DateTime.Now.AddDays(-90) },
26             new User {ID=5,Name=" Yan Yongchun ",Account="YanYongChun",Password="YanYongChun09392",Email="[email protected]", Role="Admin", LoginTime=DateTime.Now.AddMinutes(-50) }};
27         }
28 
29         /// <summary>
30         ///  Query the user instance object for the specified primary key .
31         /// </summary>
32         /// <param name="id"> Primary key of the consumer .</param>
33         /// <returns> Returns the user instance object queried .</returns>
34         public User FindUser(int id)
35         {
36             return dataList.FirstOrDefault(user => user.ID == id);
37         }
38 
39         /// <summary>
40         ///  Get instance collection of all users .
41         /// </summary>
42         /// <returns> Returns all user instances .</returns>
43         public IEnumerable<User> UserAll()
44         {
45             return dataList;
46         }
47     }
48 }

             
             (5)、PatrickLiu.MicroService.ServiceInstance(ASP.NET CORE WEB API Project ).
                          This project is very simple , Reference the other three projects , Make Restfull API, The client can access .
                         Reference project :PatrickLiu.MicroService.Interfaces
                                          PatrickLiu.MicroService.Services
                                          PatrickLiu.MicroService.Models
                         

          1)、Startup.cs Code for

 1     public class Startup
 2     {
 3 
 4         /// <summary>
 5         /// 
 6         /// </summary>
 7         /// <param name="services"></param>
 8         public void ConfigureServices(IServiceCollection services)
 9         {
10             services.AddControllers();
11             services.AddSingleton<IUserService, UserService>();
12         }
13     }

          2)、Program.cs Code for

 1     public class Program
 2     {
 3         public static void Main(string[] args)
 4         {
 5             new ConfigurationBuilder()
 6                 .SetBasePath(Directory.GetCurrentDirectory())
 7                 .AddCommandLine(args)// Support command line 
 8                 .Build();
 9                 
10             CreateHostBuilder(args).Build().Run();
11         }
12 
13         public static IHostBuilder CreateHostBuilder(string[] args) =>
14             Host.CreateDefaultBuilder(args)
15                 .ConfigureWebHostDefaults(webBuilder =>
16                 {
17                     webBuilder.UseStartup<Startup>();
18                 });
19     }

          3)、UsersController.cs Code for

 1     /// <summary>
 2     ///  Of the user  API  Type .
 3     /// </summary>
 4     [Route("api/[controller]")]
 5     [ApiController]    
 6     public class UsersController : ControllerBase
 7     {
 8         #region  Private fields 
 9 
10         private readonly ILogger<UsersController> _logger;
11         private readonly IUserService _userService;
12         private IConfiguration _configuration;
13 
14         #endregion
15 
16         #region  Construction function 
17 
18         /// <summary>
19         ///  Initializes a new instance of the type .
20         /// </summary>
21         /// <param name="logger"> Loggers .</param>
22         /// <param name="userService"> User service interface .</param>
23         /// <param name="configuration"> Configure services .</param>
24         public UsersController(ILogger<UsersController> logger, IUserService userService, IConfiguration configuration)
25         {
26             _logger = logger;
27             _userService = userService;
28             _configuration = configuration;
29         }
30 
31         #endregion
32 
33         #region  Example method 
34 
35         /// <summary>
36         ///  Get a record 
37         /// </summary>
38         /// <param name="id"></param>
39         /// <returns></returns>
40         [HttpGet]
41         [Route("Get")]
42         public User Get(int id)
43         {
44             return _userService.FindUser(id);
45         }
46 
47         /// <summary>
48         ///  Get all the records .
49         /// </summary>
50         /// <returns></returns>
51         [HttpGet]
52         [Route("All")]
53         //[Authorize]
54         public IEnumerable<User> Get()
55         {
56             Console.WriteLine($"This is UsersController {this._configuration["port"]} Invoke");
57 
58             return this._userService.UserAll().Select((user => new User
59             {
60                 ID = user.ID,
61                 Name = user.Name,
62                 Account = user.Account,
63                 Password = user.Password,
64                 Email = user.Email,
65                 Role = $"{this._configuration["ip"]}:{this._configuration["port"]}",
66                 LoginTime = user.LoginTime
67             })); ;
68         }
69 
70         /// <summary>
71         ///  Timeout processing 
72         /// </summary>
73         /// <returns></returns>
74         [HttpGet]
75         [Route("Timeout")]
76         public IEnumerable<User> Timeout()
77         {
78             Console.WriteLine($"This is Timeout Start");
79             // Timeout settings .
80             Thread.Sleep(3000);
81 
82             Console.WriteLine($"This is Timeout End");
83 
84             return this._userService.UserAll().Select((user => new User
85             {
86                 ID = user.ID,
87                 Name = user.Name,
88                 Account = user.Account,
89                 Password = user.Password,
90                 Email = user.Email,
91                 Role = $"{this._configuration["ip"]}:{this._configuration["port"]}",
92                 LoginTime = user.LoginTime
93             })); ;
94         }
95 
96         #endregion
97     }

         

         2、 Compile project , Start the four service cases .
                These four service examples are PatrickLiu.MicroService.ServiceInstance Examples of projects , Execute dotnet The command should be in the current directory .
                My catalog :E:\Visual Studio 2019\Project\SourceCode\PatrickLiu.MicroService\PatrickLiu.MicroService.ServiceInstance\bin\Debug\netcoreapp3.1
              (1)、dotnet PatrickLiu.MicroService.ServiceInstance.dll --urls="http://*:5726" --ip="127.0.0.1" --port=5726

                        

                         Accessible WebAPI Address , Verify whether it is successful .
                         Address :http://localhost:5726/api/users/all
                         The effect is as shown in the picture :
                   
              
              (2)、dotnet PatrickLiu.MicroService.ServiceInstance.dll --urls="http://*:5727" --ip="127.0.0.1" --port=5727

                         

                         Accessible WebAPI Address , Verify whether it is successful .
                         Address :http://localhost:5727/api/users/all
                         The effect is as shown in the picture :
            
             (3)、dotnet PatrickLiu.MicroService.ServiceInstance.dll --urls="http://*:5728" --ip="127.0.0.1" --port=5728

                        

                         Accessible WebAPI Address , Verify whether it is successful .
                         Address :http://localhost:5728/api/users/all
                         The effect is as shown in the picture :
          
             (4)、dotnet PatrickLiu.MicroService.ServiceInstance.dll --urls="http://*:5729" --ip="127.0.0.1" --port=5729

                        

                         Accessible WebAPI Address , Verify whether it is successful .
                         Address :http://localhost:5729/api/users/all
                         The effect is as shown in the picture :
          
         3、 Download NGINX Service components , Preset Download Windows edition .
     
                  
                        Official website : http://nginx.org/en/download.html


         4、 Deploy NGINX Server .
                   Copy the downloaded file to a directory without Chinese , Just unzip the file .
                   My storage directory :D:\Programs\MicroServices
                   
         5、 modify NGINX.CONF Archives .               
      
                 Modify the directory D:\Programs\MicroServices\Nginx-1.18.0\conf Under configuration file . File name :nginx.conf
                  
               The added configuration is as follows :

 1  upstream MicroService{
 2         server localhost:5726;
 3         server localhost:5727;
 4         server localhost:5728;
 5         server localhost:5729;
 6  }
 7 
 8 server {
 9         listen       8080;
10         server_name  localhost;
11 
12          location / {
13              proxy_pass http://MicroService;
14          }
15  }

       

         6、 Start Nginx Server .
                 Be careful : I was in Nginx Under the current directory .
                 Start nginx
                 
                NGINX Port presets :80, I changed it to 8080, No hint , It usually starts normally .

                Accessible NGINX Address , Verification NGINX Whether it is configured and started successfully .
                Address :http://localhost:8080
                The effect is as shown in the picture :

        7、 Open browser , Enter address :http://localhost:8080/api/users/all, Rearrange the page many times , You'll see the change . We've achieved decentralized 、 Load balancing .
                As shown in the picture :5726 Port
                    

               As shown in the picture :5727 Port
                   

               As shown in the picture :5728 Port
                    

               As shown in the picture :5729 Port
                    

          8、 We test NGINX High availability and extensible suite of , Come to the following conclusion .
         (1)、Nginx The client configuration is simple , Direct access Nginx The gateway address will be routed to the registration service instance .
                   (2)、 If the service instance is offline or abnormal , Can automatically detect conditions , You can skip it next time , There's no need for human participation .
                   (3)、 If a new service instance is added to the system ,Nginx Can't automatically discover , You need to manually modify the configuration file , Then restart , Can only be .
       Due to the shortcomings of the third point , Is the biggest drawback of our version , We can't do these things all the time in a huge system . Unable to automatically discover services , We need to continue to improve , That's the service registry discovery center .

3、 ... and 、  Conclusion
          Okay , That's all for today , This is a very simple architecture implementation between distributed and microservices , It's very simple , But what we want to express and what we want to get, we have got . Everything is from simple to complex , Next issue , Let's continue with our evolution of microservices . Efforts! , Make progress every day

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢