新聞中心
Redis注冊(cè)中心:追求極致的性能

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),祁東企業(yè)網(wǎng)站建設(shè),祁東品牌網(wǎng)站建設(shè),網(wǎng)站定制,祁東網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,祁東網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
隨著微服務(wù)架構(gòu)的流行,服務(wù)注冊(cè)已經(jīng)成為了一個(gè)很重要的問題。如果采用傳統(tǒng)的zookeeper或者consul等注冊(cè)中心,一旦服務(wù)數(shù)量增加,這些注冊(cè)中心就會(huì)變得越來越慢。為了解決這個(gè)問題,越來越多的人選擇了Redis注冊(cè)中心。
Redis是一個(gè)基于內(nèi)存的開源數(shù)據(jù)庫,在性能上具有得天獨(dú)厚的優(yōu)勢(shì)。作為注冊(cè)中心,Redis的性能無疑是追求極致的選擇。 在Redis中,可以使用hash結(jié)構(gòu)來存儲(chǔ)服務(wù)列表, 使用zset結(jié)構(gòu)來快速實(shí)現(xiàn)負(fù)載均衡, 使用發(fā)布訂閱模式來實(shí)現(xiàn)服務(wù)發(fā)現(xiàn),以及使用lua腳本來保證原子性操作。
以下是一個(gè)簡(jiǎn)單的Redis注冊(cè)示例:
class RedisRegistry implements ServiceRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisRegistry.class);
private final JedisPool jedisPool;
//Redis的存儲(chǔ)KEY
private final string redisKey = "ms-registry";
public RedisRegistry(String address) {
String[] array = address.split(":");
this.jedisPool = new JedisPool(new JedisPoolConfig(), array[0], Integer.parseInt(array[1]), Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}
@Override
public void register(ServiceInstance instance) throws exception {
String key = getServiceInstanceKey(instance);
try (Jedis jedis = jedisPool.getResource()) {
jedis.hset(redisKey, key, JSON.toJSONString(instance));
} catch (Exception e) {
LOGGER.error("register service instance [{}] fl.", key, e);
throw e;
}
}
@Override
public void unregister(ServiceInstance instance) throws Exception {
String key = getServiceInstanceKey(instance);
try (Jedis jedis = jedisPool.getResource()) {
jedis.hdel(redisKey, key);
} catch (Exception e) {
LOGGER.error("unregister service instance [{}] fl.", key, e);
throw e;
}
}
@Override
public List getServiceInstances(String serviceName) throws Exception {
try (Jedis jedis = jedisPool.getResource()) {
Map instancesMap = jedis.hgetAll(redisKey);
List instances = new ArrayList();
for (Map.Entry entry : instancesMap.entrySet()) {
ServiceInstance instance = JSON.parseObject(entry.getValue(), ServiceInstance.class);
if (instance.getServiceName().equals(serviceName)) {
instances.add(instance);
}
}
if (instances.isEmpty()) {
LOGGER.warn("cannot find any instance for service [" + serviceName + "]");
}
return instances;
} catch (Exception e) {
LOGGER.error("get service [" + serviceName + "] instance exception.", e);
throw e;
}
}
private String getServiceInstanceKey(ServiceInstance instance) {
return instance.getServiceName() + ":" + instance.getHost() + ":" + instance.getPort();
}
@Override
public void destroy() throws Exception {
jedisPool.destroy();
}
}
這是一個(gè)簡(jiǎn)單的Redis注冊(cè)實(shí)現(xiàn),使用了hash結(jié)構(gòu)來存儲(chǔ)服務(wù)實(shí)例,通過getServiceInstances方法可以獲取服務(wù)的實(shí)例列表。這個(gè)例子比較簡(jiǎn)單,還有很多細(xì)節(jié)可以優(yōu)化實(shí)現(xiàn),但是總體上這個(gè)Redis注冊(cè)實(shí)現(xiàn)已經(jīng)是非常高效的了。
Redis注冊(cè)中心的性能優(yōu)勢(shì)得到了越來越多的人的認(rèn)可和使用。如果你也在追求極致的性能,不妨嘗試一下Redis注冊(cè)中心。
創(chuàng)新互聯(lián)【028-86922220】值得信賴的成都網(wǎng)站建設(shè)公司。多年持續(xù)為眾多企業(yè)提供成都網(wǎng)站建設(shè),成都品牌網(wǎng)站設(shè)計(jì),成都高端網(wǎng)站制作開發(fā),SEO優(yōu)化排名推廣服務(wù),全網(wǎng)營銷讓企業(yè)網(wǎng)站產(chǎn)生價(jià)值。
當(dāng)前文章:Redis注冊(cè)中心追求極致的性能(redis注冊(cè)中心在哪里)
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/dpcdcdi.html


咨詢
建站咨詢
