Garnet技术实战测试开发:像使用Redis一样使用Garnet

一、Garnet简介

最近一段时间,看到非常多的文章描述和转发Garnet项目,比如说:

  1. .NET的集群Redis实现版本: Garnet – 一种开源、下一代 …
  2. Garnet发布 Redis不再是唯一选择 - Setli - 博客园
  3. Garnet: 力压Redis的C#高性能分布式存储数据库 - InCerry …
  4. 蓝点网:微软研究院开源Garnet缓存系统 具有高吞吐量低延迟可扩展等特点
  5. 系统极客:Microsoft 开源 Garnet:新一代远程缓存系统
  6. IT之家: 3 月 20 日消息,微软近日推出了名为 Garnet 的全新缓存存储系统
  7. MSDN:微软推出 Garnet 缓存存储系统:高吞吐量、低延迟、可扩展
  8. 新浪科技:微软开源性能遥遥领先的Garnet!开抢年入上亿美元Redis饭碗
  9. QQ新闻:微软发布开源缓存存储系统Garnet,提升大数据处理性能
  10. 中关村在线:微软开源Garnet缓存系统:性能超高

关于Garnet详细介绍,我这里就不在赘述了。
在这里插入图片描述
根据其官网的信息,我们来进行实战测试。
开源仓库地址:https://github.com/microsoft/garnet
文档地址:https://microsoft.github.io/garnet/

二、部署Garnet Server

1、从GitHub仓库中Clone仓库到本地,然后编译构建项目。
在这里插入图片描述
本地编译无任何异常后,启用docker安装
2、查看docker-compose.yml和dockerfile文件内容

version: '3.4'

services:
  garnet:
    restart: always
    image: garnet
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3278:3278"
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source

# Copy files
COPY . .
RUN dotnet restore
RUN dotnet build -c Release

# Copy and publish app and libraries
WORKDIR /source/main/GarnetServer
RUN dotnet publish -c Release -o /app --self-contained false -f net8.0

# Final stage/image
FROM mcr.microsoft.com/dotnet/runtime:8.0
WORKDIR /app
COPY --from=build /app .

# Run GarnetServer with an index size of 128MB
ENTRYPOINT ["/app/GarnetServer", "-i", "128m"]

3、通过docker指令安装&成功启动

docker-compose up

在这里插入图片描述
4、Image安装成功
查看Images
在这里插入图片描述
查看Containers
在这里插入图片描述

三、测试连接到Garnet

Garnet默认端口为3278。因为Garnet使用Redis的RESP协议作为其主要通信协议,因此可以使用大多数编程语言中现成的Redis客户端。这里我就使用常规的工具。
1、测试连接
在这里插入图片描述
2、测试写入键值对
在这里插入图片描述

四、C#开发连接Garnet

1、新建GarnetApp项目

dotnet new console -n "GarnetApp"

2、引入StackExchange.Redis库
在这里插入图片描述3、编写测试代码

using StackExchange.Redis;

namespace GarnetApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string garnetConnectionString = "localhost:3278"; // garnet 服务器地址

            // 建立连接
            ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(garnetConnectionString);
            IDatabase garnet = connection.GetDatabase();

            // 写入数据
            garnet.StringSet("myKey", "Hello, Grant!");

            // 读取数据myKey
            string value = garnet.StringGet("myKey");
            Console.WriteLine(value);

            // 读取数据key01
            string value01 = garnet.StringGet("key01");
            Console.WriteLine(value01);

            connection.Close();

            Console.ReadKey();
        }
    }
}

4、运行查看测试结果
在这里插入图片描述

五、编写其他测试代码

1、将 C# 对象序列化为 JSON 或其他格式,并存储到 Garnet中,以及从 Garnet 中取出并反序列化对象。

private static void TestObject(IDatabase garnet)
{
    // 定义一个示例对象
    var person = new { Name = "Alice", Age = 30 };

    // 将对象序列化为 JSON
    string json = JsonConvert.SerializeObject(person);

    // 存储 JSON 数据到 garnet
    garnet.StringSet("person:1", json);

    // 从 garnet 中获取 JSON 数据
    string jsonFromgarnet = garnet.StringGet("person:1");

    // 将 JSON 数据反序列化为对象
    var personFromgarnet = JsonConvert.DeserializeObject<Person>(jsonFromgarnet);

    // 输出反序列化后的对象属性
    Console.WriteLine($"Name: {personFromgarnet.Name}, Age: {personFromgarnet.Age}");
}

在这里插入图片描述
2、常用的数据结构
列表(List):使用 Garnet 的列表数据结构实现消息队列或者简单的日志记录功能。

string garnetConnectionString = "localhost:3278"; // garnet 服务器地址
// 建立连接
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(garnetConnectionString);
IDatabase garnet = connection.GetDatabase();
// 向列表中推入消息
for (int i = 1; i <= 5; i++)
{
    garnet.ListLeftPush("message_queue", $"Message {i}");
}
// 从列表中弹出消息
string message = garnet.ListRightPop("message_queue");
Console.WriteLine("Popped message: " + message);
connection.Close();

在这里插入图片描述

六、关于Garnet Server的配置项

默认配置下内容如下:
在这里插入图片描述
官网解释基本用法:
Garnet 服务器(GarnetServer.exe)可以使用配置文件进行配置(例如,garnet.conf、redis.conf、defaults.conf),同时命令行参数可用于覆盖文件中指定的任何设置。未在配置文件或命令行参数中指定的任何设置将被设置为文件中指定的默认值(可以通过命令行参数覆盖此文件的路径)。

Garnet 目前支持两种配置文件格式:

  • garnet.conf 文件格式(默认)- 一组以 JSON 格式排列的设置
  • redis.conf 文件格式 - 以 Redis 配置文件格式排列的设置:
    keyword argument1 argument2 argument3 … argumentN

请参阅 Redis 文档以获取参考信息。重要提示:并非所有 redis.conf 关键字都受到 Garnet 的支持。为了实现完整的配置设置覆盖,请使用 garnet.conf 格式。

通过命令行参数可以指定配置文件路径(和默认文件路径)。

对于 garnet.conf:

GarnetServer.exe --config-import-path <file-path>

对于 redis.conf:

GarnetServer.exe --config-import-path <file-path> --config-import-format RedisConf

注意:要更改默认配置文件的路径(和/或格式),分别使用 config-default-import-path 和 config-default-import-format 关键字。

七、总结

本文关于如何使用Garnet 作为Server,以及使用大多数编程语言中现成的Redis客户端连接和使用StackExchange.Redis库连接,进行简单的测试和使用。如果本文对你有任何帮助,我将非常荣幸。

如果你喜欢我的文章,谢谢三连:关注、点赞、分享吧!!!

相关推荐

  1. 【Swift】如何让实例对象函数一样使用

    2024-03-25 07:00:02       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 07:00:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 07:00:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 07:00:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 07:00:02       20 阅读

热门阅读

  1. MongoDB聚合运算符:$isNumber

    2024-03-25 07:00:02       19 阅读
  2. 利用K8S Statefulset搭建Etcd集群 - PVC存储

    2024-03-25 07:00:02       19 阅读
  3. AtCoder - C - Many Replacement (字符串)

    2024-03-25 07:00:02       19 阅读
  4. CloudCompare 二次开发(29)——最小二乘拟合平面

    2024-03-25 07:00:02       19 阅读
  5. 卷积神经网络基础

    2024-03-25 07:00:02       23 阅读
  6. 基于PyTorch深度学习实战入门系列-PyTorch基础全

    2024-03-25 07:00:02       20 阅读
  7. jQuery选择器

    2024-03-25 07:00:02       19 阅读
  8. Android 10.0 mt8788关于摄像头方向旋转功能实现

    2024-03-25 07:00:02       17 阅读