Skip to content
forked from shoy160/spear

Spear轻量级微服务框架,高扩展性,目前已支持TCP、HTTP、WebSocket以及GRPC协议,采用Consul/Nacos作为服务注册与发现组件,TCP协议采用DotNetty底层实现,HTTP协议采用ASP.NET CORE MVC实现。

License

Notifications You must be signed in to change notification settings

zhangbo27/spear

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spear

Spear轻量级微服务框架,高扩展性,目前已支持TCP、HTTP协议,采用Consul作为服务注册与发现组件,TCP协议采用DotNetty底层实现,HTTP协议采用ASP.NET CORE MVC实现。

Package Name NuGet Downloads
Spear.ProxyGenerator nuget stats
Spear.Core nuget stats Wiki
Spear.Codec.MessagePack nuget stats
Spear.Codec.ProtoBuffer nuget stats
Spear.Consul nuget stats
Spear.Nacos nuget stats
Spear.Protocol.Http nuget stats
Spear.Protocol.Tcp nuget stats
Spear.Protocol.WebSocket nuget stats
Spear.Protocol.Grpc nuget stats

Contracts

[ServiceRoute("test")] //自定义路由键
public interface ITestContract : ISpearService
{
    Task Notice(string name);
    Task<string> Get(string name);
}

Server

var services = new MicroBuilder();
//服务协议
var protocol = ServiceProtocol.Tcp;
services.AddMicroService(builder =>
{
    //服务端需指定编解码器和使用协议
    builder
        .AddJsonCoder()             //Json编解码
        //.AddMessagePackCodec()    //MessagePack
        //.AddProtoBufCodec()       //ProtoBuf
        .AddSession()
        //.AddNacos()
        .AddConsul("http://127.0.0.1:8500"); //Consul服务注册与发现
    switch (protocol)
    {
        case ServiceProtocol.Tcp:
            builder.AddTcpProtocol();       //TCP
            break;
        case ServiceProtocol.Http:
            builder.AddHttpProtocol();      //Http
            break;
        case ServiceProtocol.Ws:
            builder.AddWebSocketProtocol(); //WebSocket
            break;
        case ServiceProtocol.Grpc:
            builder.AddGrpcProtocol();      //GRpc
            break;
    }
});

services.AddTransient<ITestContract, TestService>();

var provider = services.BuildServiceProvider();

provider.UseMicroService(address =>
{
    address.Service = "192.168.1.xx";   //服务注册地址,需要保持与客户端的网络访问
    address.Host = "localhost";         //主机地址
    address.Port = 5001;                //端口地址
    address.Weight = 1.5;               //服务权重
    address.Gzip = true;                //是否启用GZip压缩
});

Client

var services = new MicroBuilder()
    .AddMicroClient(builder =>
    {
        //支持多编解码&多协议
        builder
            .AddJsonCodec()
            .AddMessagePackCodec()
            .AddProtoBufCodec()
            .AddHttpProtocol()          //Http
            .AddTcpProtocol()           //TCP
            .AddWebSocketProtocol()     //WebSocket
            .AddGrpcProtocol()          //GRpc
            .AddSession()
            //.AddNacos()
            .AddConsul("http://127.0.0.1:8500");
    });
var provider = services.BuildServiceProvider();
var proxy = provider.GetService<IProxyFactory>();
var service = proxy.Create<ITestContract>();

BenchMark

Protocol:Tcp,Codec:Json,Gzip:False

image

Protocol:Tcp,Codec:Json,Gzip:True

image

Protocol:Tcp,Codec:MessagePack,Gzip:True

image

Protocol:Tcp,Codec:ProtoBuf,Gzip:True

image

Protocol:Http,Codec:Json,Gzip:False

image

Protocol:Http,Codec:Json,Gzip:True

image

Protocol:Http,Codec:MessagePack,Gzip:True

image

Protocol:Http,Codec:ProtoBuf,Gzip:True

image

Protocol:WebSocket,Codec:Json,Gzip:False

image

Protocol:WebSocket,Codec:Json,Gzip:True

image

Protocol:WebSocket,Codec:MessagePack,Gzip:True

image

Protocol:WebSocket,Codec:ProtoBuf,Gzip:True

image

Protocol:GRpc

image

About

Spear轻量级微服务框架,高扩展性,目前已支持TCP、HTTP、WebSocket以及GRPC协议,采用Consul/Nacos作为服务注册与发现组件,TCP协议采用DotNetty底层实现,HTTP协议采用ASP.NET CORE MVC实现。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%