java .net 互通redis_C# servicestack.redis 互通 java jedis
本文是基于jedis的一致性環哈希來修改的,.net選的是servicestack.redis組件來修改
無奈兩個組件都有各自的一致性環哈希算法,不兼容,那就選一個作為標準,修改另一個咯。本文選擇jedis的一致性環哈希作為標準,進而修改.net來適應jedis
jedis的邏輯是給每個redis節點構造160個虛擬節點,放入一顆二叉樹中(key/value:key是一個long值,根據哈希算法算出來的一個long、value是節點id,是個string)。
OK,邏輯清楚了,那就簡單了,給c#端寫個一模一樣的一致性環哈希算法。
public?class?Sharded
{????????private?object?nodes_lock?=?new?object();????????private?RedBlackTreeMap?nodes?=?new?RedBlackTreeMap();????????private?IHash?hashAlgo?=?new?MD5_LongSUM_Multiply_Hash();
public?void?AddTarget(int?index,?string?shard)
{????????????lock?(nodes_lock)
{????????????????for?(int?n?=?0;?n?
{????????????????????var?hashKey?=?"SHARD-"?+?index?+?"-NODE-"?+?n;????????????????????long?hashValue?=?this.hashAlgo.Hash(hashKey);
nodes.SetOrAddValue(hashValue,?shard);
}
}
}????????public?string?GetShardInfo(string?key)
{????????????long?searchHashKey?=?this.hashAlgo.Hash(key);????????????long?nearestKey;????????????string?shard;????????????lock?(nodes_lock)
{????????????????if?(this.nodes.NearestGreater(searchHashKey,?out?nearestKey))
{
shard?=?this.nodes.GetValue(nearestKey);????????????????????return?shard;
}????????????????if?(this.nodes.Least(out?searchHashKey,?out?shard))????????????????????return?shard;
}????????????throw?new?Exception("GetShardInfo?exception");
}
}
其中RedBlackTreeMap這個是TreeLib中的組件,需要在nuget上引用。
MD5_LongSUM_Multiply_Hash,這是個MD5算法,輸入為string,輸出為long。
此處由于考慮到輸出不是string,因此自己又改了改,讓他輸出long
public?class?MD5_LongSUM_Multiply_Hash?:?IHash
{????????public?long?Hash(string?key)
{????????????var?md5=?Md5Hash(key);????????????if?(string.IsNullOrEmpty(md5))
Log.GetLog().Info("Hash,?md5?is?null?or?empty");????????????var?convertedKeyBytes?=?Encoding.UTF8.GetBytes(md5);
long?value?=?1;????????????foreach(var?b?in?convertedKeyBytes)
value?*=?b*-1;????????????return?value;
}????????private?string?Md5Hash(string?input)
{
MD5CryptoServiceProvider?md5Hasher?=?new?MD5CryptoServiceProvider();????????????if?(string.IsNullOrEmpty(input))
Log.GetLog().Info("Md5Hash,?input?is?null?or?empty");????????????byte[]?data?=?md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder?sBuilder?=?new?StringBuilder();????????????for?(int?i?=?0;?i?
{
sBuilder.Append(data[i].ToString("x2"));
}????????????return?sBuilder.ToString();
}
}
剩下的就是java端的這個輸入string,輸出long的算法,需要和.net的輸入輸出一致了。
那就也寫一個哈希算法,讓他輸入string,輸出long,和.net的一致,這里只要java/.net用同一種md5算法,后續的md5變成long就很容易了。
import?org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;import?redis.clients.util.Hashing;import?redis.clients.util.SafeEncoder;import?java.io.UnsupportedEncodingException;/**
*?Created?by?z?on?2017/4/12.?*/public?class?MD5_SUM_Hash?implements?Hashing?{
MessageDigestPasswordEncoder?encoder=new?MessageDigestPasswordEncoder("MD5");????public?long?hash(String?key)?{????????return?this.hash(SafeEncoder.encode(key));
}????public?long?hash(byte[]?bytes)?{
String?converted_str=?null;????????try?{
converted_str?=?new?String(bytes,?"UTF8");
}?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace();
}
String?result=encoder.encodePassword(converted_str,?null);????????try?{
bytes=result.getBytes("UTF8");
}?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace();
}????????long?value?=?1;????????for(byte?b?:?bytes)
value?*=?b*-1;????????return?value;
}
}
org.springframework.security
spring-security-core
總結
以上是生活随笔為你收集整理的java .net 互通redis_C# servicestack.redis 互通 java jedis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 区块链技术 好文收藏
- 下一篇: 本博客正式开通 Chat快问 功能