{"id":16918680,"url":"https://github.com/hailin0/redis-sentinel-client","last_synced_at":"2025-04-11T11:31:15.491Z","repository":{"id":186994380,"uuid":"63494240","full_name":"hailin0/redis-sentinel-client","owner":"hailin0","description":"redis-sentinel(哨兵)模式的分片连接池，根据sentinel集群的master变更自动切换主从，并提供与ShardedJedisPool一样的API。","archived":false,"fork":false,"pushed_at":"2016-11-06T13:41:06.000Z","size":30,"stargazers_count":13,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T13:21:10.793Z","etag":null,"topics":["jedis-plugin"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hailin0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-07-16T17:20:05.000Z","updated_at":"2024-09-14T09:56:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"d8d7f539-e6dd-4e99-90e3-2a888ecd5f84","html_url":"https://github.com/hailin0/redis-sentinel-client","commit_stats":null,"previous_names":["hailin0/redis-sentinel-client"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hailin0%2Fredis-sentinel-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hailin0%2Fredis-sentinel-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hailin0%2Fredis-sentinel-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hailin0%2Fredis-sentinel-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hailin0","download_url":"https://codeload.github.com/hailin0/redis-sentinel-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248383917,"owners_count":21094631,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jedis-plugin"],"created_at":"2024-10-13T19:41:01.620Z","updated_at":"2025-04-11T11:31:15.222Z","avatar_url":"https://github.com/hailin0.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# redis-sentinel-client\n  redis-sentinel(哨兵)模式的分片连接池，根据sentinel集群的master变更自动切换主从，并提供与ShardedJedisPool一样的API。\n\n\n# 简述\n redis2.x的高可用方案sentinel（哨兵），本身提供redis主从集群的自动管理功能，以及主从自动切换的高可用功能。\n 而在java客户端使用时需要用到sentinel的客户端，因为调用方式不一样，直接使用jedis老的连接池是无法做到主从切换的效果。\n jedis客户端中自带了一个sentinel单实例连接池实现redis.clients.jedis.JedisSentinelPool.java，\n 使用JedisSentinelPool.java可以很轻松的使用sentinel的功能，达到主从切换的效果，但是jedis包没有提供分片时使用的连接池,并且JedisSentinelPool.java有一个bug,主从切换时消息重复接收，导致连接池初始化2次。\n\nShardedJedisSentinelPool.java就是在JedisSentinelPool.java的基础上，增加了对分片的支持并修改bug，可以很少的改动老代码进行升级。\n\t\n# 工作原理\n  ShardedJedisSentinelPool.java的实现思路参考jedis包JedisSentinelPool.java，池需要配置sentinel集群地址以及\n  sentinel集群中的master-name，然后初始化时根据sentinel提供的api去请求master-name当前对应的ip和端口，\n  然后与普通的jedispool初始化一样，拿着请求回来的ip和端口初始化pool并保存在池中的map（本地路由表）。\n  同时也根据sentinel的数量启动对应的后台线程，去订阅sentinel集群的master变更消息，收到变更消息之后与\n  本地路由表对比排除无用的消息，然后开始重新初始化ShardedJedisSentinelPool.java中的连接。\n\n\n# 测试代码\n         // sentinel配置文件中的master-name列表\n        List\u003cString\u003e masters = new ArrayList\u003cString\u003e();\n        masters.add(\"master1\");\n        masters.add(\"master2\");\n\n        // sentinel集群列表\n        Set\u003cString\u003e sentinels = new HashSet\u003cString\u003e();\n        sentinels.add(\"192.168.1.112:26379\");\n\n        //初始化连接池\n        ShardedJedisSentinelPool pool = new ShardedJedisSentinelPool(masters, sentinels);\n        \n        //获取jedis客户端\n        ShardedJedis jedis = pool.getResource();\n\n# 代码实现\n\u003ca href=\"https://github.com/hailin0/redis-sentinel-client/blob/master/src/main/java/redis/clients/jedis/JedisSentinelPool.java\"\u003e单实例连接池-JedisSentinelPool.java\u003c/a\u003e\n\u003cbr\u003e\n\u003ca href=\"https://github.com/hailin0/redis-sentinel-client/blob/master/src/main/java/redis/clients/jedis/ShardedJedisSentinelPool.java\"\u003e分片连接池-ShardedJedisSentinelPool.java\u003c/a\u003e\n\n\n\n# 参考资料\n\u003ca href=\"http://doc.redisfans.com/topic/sentinel.html\"\u003eSentinel官方文档\u003c/a\u003e\n\u003cbr\u003e\n\u003ca href=\"http://blog.csdn.net/wtyvhreal/article/details/46517483\"\u003eSentinel集群搭建过程\u003c/a\u003e\n\n# 注意\n1.调用ShardedJedis.close()方法需要try-catch，因为当master发生变更后，监控线程会重新初始化连接池中的连接，造成close()方法抛出异常。\n2.JedisSentinelPool.java最好不要使用，有重复接收master变更消息，造成pool多次初始化的bug。\n使用ShardedJedisSentinelPool.java就可以满足需求了\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhailin0%2Fredis-sentinel-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhailin0%2Fredis-sentinel-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhailin0%2Fredis-sentinel-client/lists"}