net builds a distributed framework (2) .net under Windows connects to Redis under Linux

Then the previous section

1. Modify the reids configuration file

// Modify the ip bind 127.0.0.1 in the reids configuration file to 0.0.0.0 
vi /etc/redis/ 6379 .conf
 // Close the redis service 
service redisd stop
 // Restart the redis service Note: If you use service redisd start to start, it may There is a problem, so start redis-server with the following command 
/etc/redis/ 6379 .conf &

 

2. Firewall port settings

// Open port 6379 
firewall-cmd --zone= public --add-port= 6379 /tcp -- permanent
 // View open ports 
firewall-cmd --list- ports
 // Restart firewall 
firewall-cmd --reload

Third, install the redis desktop manager management tool test

Download address: https://redisdesktop.com/download

Select to connect to the redis server, enter the ip address, and then click Test Connection. After success, enter the connection name and click OK

4. Open Visual Studio and select File - New - Project pop-up window and select Console Application

5. Right-click on the newly created project and select Manage NuGet Packages to install StackExchange.Redis 

6. Create a new class RedisHelper.cs, then call it in the main method and finally press F5 to run the program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace ConsoleApp2
{

   
   public  class RedisHelper
    {
       private  static ConfigurationOptions configuration = ConfigurationOptions.Parse("192.168.1.11:6379");
        private static readonly Object Locker = new object();
        private static ConnectionMultiplexer redisConn;
        public static ConnectionMultiplexer getRedisConn()
        {
            if (redisConn == null)
            {
                lock (Locker)
                {
                    if (redisConn == null || !redisConn.IsConnected)
                        redisConn = ConnectionMultiplexer.Connect(configuration);
                }
            }
            return redisConn;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var redisConn = RedisHelper.getRedisConn();
            var db = redisConn.GetDatabase();
           bool result = db.StringSet("name","我是刘明君");
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325141750&siteId=291194637