GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - BaseCoreVueProject/InitQ: redis消息队列中间件

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

17 Commits
17 Commits

Folders and files

NameName
Last commit message
Last commit date
Abstractions
Abstractions
 
 
Attributes
Attributes
 
 
Cache
Cache
 
 
Internal
Internal
 
 
.gitignore
.gitignore
 
 
HostedService.cs
HostedService.cs
 
 
InitQ.Builder.cs
InitQ.Builder.cs
 
 
InitQ.Core.cs
InitQ.Core.cs
 
 
InitQ.Options.cs
InitQ.Options.cs
 
 
InitQ.csproj
InitQ.csproj
 
 
InitQ.sln
InitQ.sln
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
ServiceCollectionExtensions.cs
ServiceCollectionExtensions.cs
 
 

Repository files navigation

Redis消息队列中间件

安装环境

  • .net core版本:2.1
  • redis版本:3.0以上

特点

1.通过注解的方式,订阅队列
2.可以设置消费消息的频次
3.支持消息广播

应用场景

分布式环境,redis消息队列

使用介绍

  • 1.获取initQ包
    方案A. install-package InitQ 方案B. nuget包管理工具搜索 InitQ

  • 2.添加中间件(该中间件依赖StackExchange.Redis)

    services.AddInitQ(m=> 
    {
        m.SuspendTime = 1000;
        m.ConnectionString = "47.104.247.70,password=admin";
        m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeA), typeof(RedisSubscribeB) };
        m.ShowLog = false;
    });
    

消息发布/订阅

  • 1.定义发布者
      using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
      {
          //redis对象
          var _redis = scope.ServiceProvider.GetService<ICacheService>();
          //循环向 tibos_test_1 队列发送消息
          for (int i = 0; i < 1000; i++)
          {
              await _redis.ListRightPushAsync("tibos_test_1", $"我是消息{i + 1}号");
          }
      }
    
  • 2.定义消费者类 RedisSubscribeA
    public class RedisSubscribeA: IRedisSubscribe
    {
        [Subscribe("tibos_test_1")]
        private async Task SubRedisTest(string msg)
        {
            Console.WriteLine($"A类--->订阅者A消息消息:{msg}");
        }
    
        [Subscribe("tibos_test_1")]
        private async Task SubRedisTest1(string msg)
        {
            Console.WriteLine($"A类--->订阅者A1消息消息:{msg}");
        }
    
        [Subscribe("tibos_test_1")]
        private async Task SubRedisTest2(string msg)
        {
            Console.WriteLine($"A类--->订阅者A2消息消息:{msg}");
        }
    
        [Subscribe("tibos_test_1")]
        private async Task SubRedisTest3(string msg)
        {
            Console.WriteLine($"A类--->订阅者A3消息消息:{msg}");
        }
    }
    
  • 3.定义消费者类 RedisSubscribeB
    public class RedisSubscribeB : IRedisSubscribe
    {
        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [Subscribe("tibos_test_1")]
        private async Task SubRedisTest(string msg)
        {
            Console.WriteLine($"B类--->订阅者B消费消息:{msg}");
        }
    }
    

消息广播/订阅

  • 1.订阅消息通道,订阅者需要在程序初始化的时候启动一个线程侦听通道,这里使用HostedService来实现,并注册到容器
      public class ChannelSubscribeA : IHostedService, IDisposable
      {
          private readonly IServiceProvider _provider;
          private readonly ILogger _logger;
    
          public ChannelSubscribeA(ILogger<TestMain> logger, IServiceProvider provider)
          {
              _logger = logger;
              _provider = provider;
          }
          public void Dispose()
          {
              _logger.LogInformation("退出");
          }
    
          public Task StartAsync(CancellationToken cancellationToken)
          {
              _logger.LogInformation("程序启动");
              Task.Run(async () =>
              {
                  using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
                  {
                      //redis对象
                      var _redis = scope.ServiceProvider.GetService<ICacheService>();
                      await _redis.SubscribeAsync("test_channel", new Action<RedisChannel, RedisValue>((channel, message) =>
                      {
                          Console.WriteLine("test_channel" + " 订阅服务A收到消息:" + message);
                      }));
    
                  }
              });
              return Task.CompletedTask;
          }
    
          public Task StopAsync(CancellationToken cancellationToken)
          {
              _logger.LogInformation("结束");
              return Task.CompletedTask;
          }
      }
    
      public class ChannelSubscribeB : IHostedService, IDisposable
      {
          private readonly IServiceProvider _provider;
          private readonly ILogger _logger;
    
          public ChannelSubscribeB(ILogger<TestMain> logger, IServiceProvider provider)
          {
              _logger = logger;
              _provider = provider;
          }
          public void Dispose()
          {
              _logger.LogInformation("退出");
          }
    
          public Task StartAsync(CancellationToken cancellationToken)
          {
              _logger.LogInformation("程序启动");
              Task.Run(async () =>
              {
                  using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
                  {
                      //redis对象
                      var _redis = scope.ServiceProvider.GetService<ICacheService>();
                      await _redis.SubscribeAsync("test_channel", new Action<RedisChannel, RedisValue>((channel, message) =>
                      {
                          Console.WriteLine("test_channel" + " 订阅服务B收到消息:" + message);
                      }));
    
                  }
              });
              return Task.CompletedTask;
          }
    
          public Task StopAsync(CancellationToken cancellationToken)
          {
              _logger.LogInformation("结束");
              return Task.CompletedTask;
          }
      }
    
  • 2.将HostedService类注入到容器
      services.AddHostedService<ChannelSubscribeA>();
      services.AddHostedService<ChannelSubscribeB>();
    
  • 3.广播消息
      using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
      {
          //redis对象
          var _redis = scope.ServiceProvider.GetService<ICacheService>();
          for (int i = 0; i < 1000; i++)
          {
              await _redis.PublishAsync("test_channel", $"往通道发送第{i}条消息");
          }
      }
    

版本

  • V1.0 更新时间:2019-12-30

版本库:

  • Git获取:https://github.com/wmowm/InitQ

作者:提伯斯

About

redis消息队列中间件

Resources

Readme
Apache-2.0 license
Activity
Custom properties

Stars

1 star

Watchers

0 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.