分布式系统解决方案,学这个就够了

96 阅读3分钟

分布式、负载、消息队列等一些解决方案,在互联网公司应用已经非常普遍了,也是每一个程序员要成为技术专家、架构师必学的知识。

所以,今天给大家推荐一个开源项目,有关互联网项目常见的解决方案,通通都打包一起了。

项目简介

这是一个包含分布式锁,分布式ID,分布式消息队列、配置中心、注册中心、服务注册、超时、重试、熔断、负载均衡等解决方案的项目。

该项目把开发中,经常碰到的解决方案都归纳在一起了,大家可以根据自己项目的需求,选择相应的解决方案。

并且针对每一个解决方案,都有简单的例子,作为一个学习资料也是非常不错的,建议大家都收藏备用。

技术架构

1、跨平台:这是基于.NetCore开发的系统,可以部署在Docker,Windows,Linux,Mac;

2、基于.Net 5开发;

3、开发工具:visual studio 2022。

项目结构

图片

功能详细列表

  • 1、分布式锁

基于Redis

基于Consul

  • 2、分布式缓存

基于Redis

  • 3、分布式Id

基于Snowfake

  • 4、分布式追踪 Opentracing

基于Jaeger

  • 5、消息总线

基于MySql

基于SqlServer

基于Rabbitmq

基于Kafka

    • 6、消息队列

消息可靠性保证

  • 7、健康检查

Mongodb 健康检查

MySql 健康检查

SqlServer 健康检查

Redis 健康检查

Rabbitmq 健康检查

Kafka 健康检查

  • 8、负载均衡

随机负载均衡

轮训负载均衡

  • 9、配置中心

基于Apollo配置中心

基于Nacos配置中心

  • 10、服务注册

基于Consul服务注册和发现

基于Nacos服务注册和发现

  • 11、服务调用

基于HTTP弹性客户端(支持:服务发现、负载均衡、超时、重试、熔断)

基于HTTP非弹性客户端(支持:服务注册、负载均衡)

  • 12、Canal 数据集成

输出到控制台

输出到Rabbitmq(待实现)

输出到Kafka(待实现)

使用方法

下面以分布式锁和分布缓存举例,讲解该项目使用方法;更多功能使用方法看项目的例子。

1、分布式锁

1.1、安装

可以直接引用,或者通过Nuget直接安装

Install-Package Hummingbird.Extensions.DistributedLock -Version 1.15.0

1.2、配置信息

public class Startup
  {
public void ConfigureServices(IServiceCollection services)
        {
            services.AddHummingbird(hb =>
            {
                hb.AddDistributedLock(option =>
                 {
                     option.WithDb(0);
                     option.WithKeyPrefix("");
                     option.WithPassword("123456");
                     option.WithServerList("127.0.0.1:6379");
                     option.WithSsl(false);
                 });               
            });
        }
    }

1.3、分布锁使用例子

namespace Hummingbird.Example.Controllers{using Microsoft.AspNetCore.Mvc;using System;using System.Threading.Tasks;    [Route("api/[controller]")]public class DistributedLockController : Controller    {public DistributedLockController(Hummingbird.Extensions.DistributedLock.IDistributedLock distributedLock)        {this.distributedLock = distributedLock;        }private readonly Hummingbird.Extensions.DistributedLock.IDistributedLock distributedLock;        [HttpGet]        [Route("Test/{lockName}")]public async Task<string> Test(string lockName="key1")        {var lockToken = Guid.NewGuid().ToString("N");try            {if (distributedLock.Enter(                        lockName,                         lockToken))                {await System.Threading.Tasks.Task.Delay(15000);// do somethingreturn "ok";                }else                {return "error";                }            }catch(Exception ex)            {return ex.Message;            }finally            {               distributedLock.Exit(lockName, lockToken);            }        }    }}

2、分布式缓存

2.1、安装

可以直接引用,或者通过Nuget直接安装

Install-Package Hummingbird.Extensions.Cacheing -Version 1.15.0

2.2、配置信息

public class Startup
    {
public void ConfigureServices(IServiceCollection services)
        {

            services.AddHummingbird(hb =>
            {
                hb.AddCacheing(option =>
                {
                    option.WithDb(0);
                    option.WithKeyPrefix("");
                    option.WithPassword("123456");
                    option.WithReadServerList("192.168.109.44:6379");
                    option.WithWriteServerList("192.168.109.44:6379");
                    option.WithSsl(false);
                })

            });

        }
    }

2.3、配置Reids缓存

namespace Hummingbird.Example.Controllers
{
using Hummingbird.Extensions.Cacheing;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

    [Route("api/[controller]")]
public class CacheingController : Controller
    {
private readonly ICacheManager cacheManager;

public CacheingController(
            ICacheManager cacheManager)
        {
this.cacheManager = cacheManager;
        }

        [HttpGet]
        [Route("Test/{cacheKey}")]
public  string Test(string cacheKey="key1")
        {
var cacheValue = cacheManager.StringGet<string>(cacheKey);
if(cacheValue == null)
            {
                cacheValue = "value";
                cacheManager.StringSet(cacheKey, cacheValue);
            }
            return cacheValue;
        }
    }
}

项目地址

github.com/guoming/Hum…

最后

该项目涵盖的还是比较齐全的,对于初学者来说,也是比较容易入门,大家可以选择其中一个知识点学习,后面再扩展学习更多的知识。

今天就跟大家,分享到这边了,希望对您有帮助,欢迎点赞关注转发。

‍- End -