Jimu: Introduction to. Net Core Distributed microservices Framework

Source: Internet
Author: User
Tags system log zookeeper

First, preface

In recent years, has been dipping in the. NET Platform for enterprise application development (8 years), the use of the. NET Framework is not many (the number of unclear, impressive with the ASP. MVC,WPF, many others are based on the Microsoft Development Framework to do some encapsulation and form a new framework, Most of them have not had a good name to annihilate in the history of the river, and the framework of their own development is quite a lot (5-8), change the frame and change the company in direct proportion (each change a company, at least a frame), many are dirty (at that time feel tall, and later feel very low). Drifting from the WinForm era to the current buzz of MicroServices, last winter saw the surging project (another open-source. Net Core microservices framework), suddenly whim want to implement one (like repeating the wheel), so online reading a variety of blogs, news, comments, code, Understand the concept of micro-service, combined with online open source code and ideas, reluctantly pieced together a little feature of the micro-service framework-Jimu (Chinese name: building blocks).

But the frame features a cleavage, squeeze or something: Jimu is a simple, easy-to-use microservices framework based on. Net Core 2.0, which uses a large number of open source libraries to support distributed, high concurrency, and load balancing, enabling service governance and RPC calls. Jimu in continuous iterative development, many features are still in the queue (such as visual monitoring and management tools, hot updates, service fusing, current limiting, and demotion ...). ), such as non-love Toss the master, not recommended on the production environment. As its Chinese name-building blocks, I hope to use her to develop projects like building blocks as simple and fast and controllable, so that the project is safe and reliable and stable, the overall architecture can be extended, high concurrency, distributed.

Second, functional technology
    • Service registration: Specify the server address, or extend support for other applications (such as consul-supported, Redis, zookeeper)
    • Service discovery: Specify the server address, or extend support for other apps (such as Consul-supported, Redis, zookeeper)
    • Service invocation: Implementing RPC calls with Dotnetty or flurl.http
    • Service Proxy: Microsoft.codeanalysis parsing service Interface generating dynamic agent
    • Health monitoring: Quartz.net scheduled Socket task Check server heartbeat
    • Load balancing: Polling algorithm (not implemented: Weighted polling method, minimum connection number method, random method, weighted random method, source address hash method)
    • Authorized authentication: JOSE-JWT for JWT authorization
    • Serialization: Json.NET
    • System log: Log4net, NLog
Three, the frame diagram

, there are four kinds of characters:

    • Client: A variety of clients, which is abstract, as long as the access to apigateway all belong to the client (mobile, computer ..., if service Server1 and service Server2 access Apigateway also belong to the client)
    • Apigateway: Service Gateway, external to access internal services. A gateway is a very important role that is an intermediary between external and internal services, responsible for accepting and responding to external requests, invoking internal services, and service governance.
    • Service Discovery Server: Responsible for saving registered services, equivalent to the roster of services, service Server registration service is to record their name to the roster, Apigateway Discovery service is to go to the roster to find which registered services
    • Service Server: This is microservices, where all business requirements are implemented
Iv. How to use

Service registration and discovery have been implemented in two ways:

    1. Register directly with a local service without relying on third-party applications, see Quick Start on GitHub
    2. Depending on consul, you need to start Consul, then service registration Consul, Apigateway Access Consul Discovery service

The following uses consul as a service container, demonstrating how to implement a distributed micro-service using Jimu

1. Start Consul

Download Install Consul https://www.consul.io/downloads.html
Start

consul agent -dev
2. Micro-Service Project

Create a class library project that is based on. Net Core 2.0 and add Jimu dependencies

Install-Package  Jimu

Add service, reference space: using Jimu;

[JimuServiceRoute("api/{Service}")] // RPC 调用路径 public class UserService : IJimuService {     [JimuService(CreatedBy = "grissom")] // 指定服务的元数据, 该服务调用路径为 api/user/getname?id=     public string GetName(string id)     {         return $"user id {id}, name enjoy!";     } }
3. MicroServices Service-side project

Create a. Net Core 2.0-based console project and add Jimu.server and Jimu.Common.Discovery.ConsulIntegration dependencies

Install-Package  Jimu.ServerInstall-Package  Jimu.Common.Discovery.ConsulIntegration

Add the server startup code to the Main function, reference space: using Jimu.server;

static void Main(string[] args){    var hostBuilder = new ServiceHostServerBuilder(new Autofac.ContainerBuilder())        .UseLog4netLogger()        .LoadServices("QuickStart.Services")        .UseDotNettyForTransfer("127.0.0.1", 8001)        .UseConsulForDiscovery("127.0.0.1", 8500, "JimuService")        ;    using (var host = hostBuilder.Build())    {        host.Run();        Console.ReadLine();    }}
4. Micro-service Client (Apigateway) project

Create an ASP. Net Core 2.0-based WEB application (optionally API project template) and add jimu.client and Jimu.Common.Discovery.ConsulIntegration dependencies

Install-Package  Jimu.ClientInstall-Package  Jimu.Common.Discovery.ConsulIntegration

Modify the code of the Startup.cs class to add support for Jimu

Using jimu.client;using Jimu.Client.ApiGateway; public class Startup {public Startup (IConfiguration configuration) {configuration = Configur        ation        } public iconfiguration Configuration {get;} This method gets called by the runtime.        Use this method to add services to the container. public void Configureservices (iservicecollection services) {//services.            Addmvc (); Services.        Usejimu (); }//This method gets called by the runtime.        Use this method to configure the HTTP request pipeline. public void Configure (Iapplicationbuilder app, ihostingenvironment env) {if (env. Isdevelopment ()) {app.            Usedeveloperexceptionpage (); }//app.            Usemvc (); var host = new Servicehostclientbuilder (new Autofac.containerbuilder ()). Uselog4netlogger (). Usepollingaddressselector (). UsedotNettyfortransfer (). Useserverhealthcheck (1). Setdiscoveryautoupdatejobinterval (1). Useconsulfordiscovery ("127.0.0.1", 8500, "Jimuservice").            Build (); App.            Usejimu (host); Host.        Run (); }    }
5. Start the server and client at the same time

Then in the browser access: http://localhost:58156/api/user/getname?id=666

6. More Demos

More powerful use of the function, please see the demo, because of limited time, here is not a detailed explanation, if you are interested in the future or there will be more space and documentation to introduce.
Please download Jimu source code, or download the project Jimu.demo

Five, the source code

Please go to GitHub pull source, which contains a "huge" demo

Jimu: Introduction to. Net Core Distributed microservices Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.