• notice
  • Congratulations on the launch of the Sought Tech site

Use Redis source code to compile and release Redis For Windows distribution package for Windows

Redis is a high-performance key-value database. In the daily project development process, Redis is basically used in personal development projects, mainly for the underlying support of caching and distributed locks. I personally like to use the .NET technology system, so generally Most of the deployments use Windows servers, but at present, Redis does not officially provide a Windows version of the Redis distribution package. Some Windows versions of Redis that can be found on the Internet are older versions, and the latest Redis master is already version 7.x. , After in-depth research, I found that Redis was developed in C language, so it is still possible to compile it to the Windows version. Here I will share with you the operation of compiling Redis for Windows.

First, the following basic ideas are introduced. The overall process is mainly as follows:

  1. Get the latest version of Redis source code https://github.com/redis/redis/releases

  2. Install MSYS2 and compile Redis source code

  3. Use .NET6 to develop a RedisService to integrate compiled Redis into a service, support running as a Windows service

 

First, we download the source code of Redis to a local folder. You can download the latest version of the source code directly from the https://github.com/redis/redis/releases page. As of now, the latest version is 7.0.4, here we download tar.gz zip file format

   

After the source code is downloaded, install the latest version of MSYS2, which is a compilation tool that can be used to compile some software in Linux under Windows. This time we will use it to compile the Redis source code to the Windows version.

https://www.msys2.org This is the official website address, you can download it from here

 

The installation process of MSYS2 will not be expanded. After downloading the exe, click the next step directly, and the next step will be fine. There is nothing to pay special attention to.

After MSYS2 is installed, find MSYS2 from the start menu. MSYS starts to run. After entering MSYS2, it mainly depends on the operation of the command line. During the process, you will be asked whether to continue, select Y

 

#Update MSYS2 system

pacman-Syu

 

# install gcc make pkg-config

pacman -Sy gcc make pkg-config

 

# Switch to the compiled directory (MSYS2 /d is the D drive; /e is the E drive) I have created a redis empty folder on the D drive in advance for this compilation operation
cd /d/redis

 

Cut the downloaded source code zip package to the corresponding compilation directory, here we are d:/redis/

# Unzip the redis source code

tar -xvf redis-7.0.4.tar.gz

 

# Enter the source directory
cd redis-7.0.4

 

Then you need to go to the installation directory of MSYS2 to modify the dlfcn.h file, as follows:

Open the  C:\msys64\usr\include\usr\include\dlfcn.h  file, modify the source code, comment or delete the macro to determine #if __GNU_VISIBLE (line 49) and #endif (line 61), otherwise it will report the structure Dl_info error not found.

The default MSYS2 directory is: C:\msys64\  , which is actually the installation directory of msys2. If the installation directory is modified during the installation process, go to the corresponding installation directory and then find the dlfcn in the include folder under the usr folder  in turn. h file 

 

You need to comment out lines 49 and 61 of this code, as shown below

 

After commenting, save and exit, and then go back to our MSYS2 terminal console to start compiling Redis

# Compile
make PREFIX=/d/redis/dist install

 

There will be some warnings and other information in the process, which can be ignored directly, as long as the compilation can be successful.

After the compilation is successful, you can see the compiled exe file for windows in the d:\redis\dist\bin folder of the computer

After compiling, you need to copy the following three files

msys-2.0.dll is located in c:\msys64\usr\bin
redis.conf is located in the redis source code folder, d:\redis\redis-7.0.4
sentinel.conf is located in the redis source code folder, d:\redis\redis-7.0 .4

Copy the above three files to our d:\redis\dist\bin folder

  

Our d:\redis\dist\bin folder should have these files in total.

It should be noted that due to the regulations of MSYS2, our software must be placed in at least 2-level directory folders before it can run normally. For example, d:/redis/dist/bin/ can, but d:/redis/ cannot.

RedisServer can be started in the following two ways

cmd start

d:cd d:\redis\dist\binredis -server.exe redis.conf

powershell start

d:cd d:\redis\dist\bin./redis-server.exe redis.conf

 

 When the above interface appears after startup, it means that our redis-server has been officially compiled and released successfully. You can use the redis connection tool you are used to try to connect to 127.0.0.1:6379. I personally use Another Redis Desktop Manager. After connecting, you can see to the following status information

  

From the status, it can be seen that it is the 7.0.4 version. So far, the operation of using Redis source code to compile and release the Windows version of Reidis is completed. The following describes how to combine .NET to develop a Windows service and compile this redis-server for us. exe implements the installation and running mode of the windows service. Otherwise, it is cumbersome to manually start redis-server.exe every time you use it.

First use VisualStudio to create a .NET 6 console program, and then install the Microsoft.Windows.Compatibility component in Nuget

There is not much code in the whole project, mainly the content of the Program.cs code is as follows:

copy code

using System.Diagnostics; using System.ServiceProcess;namespace RedisService{
   class Program
   {
       [System.Diagnostics.CodeAnalysis.SuppressMessage( " Interoperability " , " CA1416: Verify Platform Compatibility " , Justification = " <Suspended> " )]         static  void Main()
       {
           ServiceBase.Run( new RedisService());
       }
   }
   partial  class RedisService : ServiceBase
   {
       private Process? process = new ();
       protected  override  void OnStart( string [] args)
       {
           var basePath = Path.Combine(AppContext.BaseDirectory).Replace( " \\ " , " / " );             var diskSymbol = basePath[..basePath.IndexOf( " : " )];             var confPath = basePath.Replace(diskSymbol + " : " , " /cygdrive/ " + diskSymbol);
           ProcessStartInfo processStartInfo = new (basePath + " redis-server.exe " , confPath + " redis.conf " );
           process = Process.Start(processStartInfo);
       }
       protected  override  void OnStop()
       {
           if (process != null )
           {
               process.Kill();
               process.Dispose();
           }
       }
   }}

copy code

 

The following settings are recommended when publishing: use standalone mode, enable precompile and publish as a single file

 

After publishing, the following files are obtained

 

Copy the published files to the d:\redis\dist\bin folder and put the Redis files we started publishing in the same directory

 

In this way, our entire RedisServer is completed, and all these files can be packaged and saved. When you want to install it on a computer in the future, you only need to execute the following installation, uninstallation, start, and stop commands.

Suppose we put this folder in d:/Software/Redis/

It should be noted that due to the regulations of MSYS2, our software must be placed in at least 2-level directory folders before it can run normally. For example, d:/Software/Redis/ can, but d:/Software/ cannot.

install service

sc.exe create Redis binpath= 'D:\Software\Redis\RedisService.exe' start= auto

In this way, we have successfully installed the Windows version of Redis we made ourselves, and it will start automatically every time we boot.

start the service

net start Redis

Out of service

net stop Redis

uninstall service

sc.exe delete Redis

 

So far, the use of Redis source code to compile and release the Windows version of Redis For Windows distribution package is finished. If you have any questions, you can comment below the article or privately message me. You are welcome to actively discuss and exchange. Interested friends can pay attention to what I am currently maintaining A .net basic framework project, the project address is as follows

https://github.com/berkerdong/NetEngine.git

https://gitee.com/berkerdong/NetEngine.git


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+