{"id":201840,"date":"2025-05-10T08:56:03","date_gmt":"2025-05-10T00:56:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201840\/"},"modified":"2025-05-10T08:56:03","modified_gmt":"2025-05-10T00:56:03","slug":"spring-boot%e5%a6%82%e4%bd%95%e6%95%b4%e5%90%88redis%e4%b8%bb%e4%bb%8esentinel%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201840\/","title":{"rendered":"spring\u00a0boot\u5982\u4f55\u6574\u5408redis\u4e3b\u4ecesentinel\u65b9\u5f0f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>spring&nbsp;boot\u5982\u4f55\u6574\u5408redis\u4e3b\u4ecesentinel\u65b9\u5f0f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aspring&nbsp;boot\u5982\u4f55\u6574\u5408redis\u4e3b\u4ecesentinel\u65b9\u5f0f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<h3>springboot\u6574\u5408redis\u4e3b\u4ecesentinel<\/h3>\n<h4>\u4e00\u4e3b\u4e8c\u4ece\u4e09sentinel\u914d\u7f6e<\/h4>\n<ul>\n<li>\n<p>1\u3001master:127.0.0.1:6379<\/p>\n<\/li>\n<li>\n<p>2\u3001slave1:127.0.0.1:6380<\/p>\n<\/li>\n<li>\n<p>3\u3001slave2:127.0.0.1:6381<\/p>\n<\/li>\n<li>\n<p>4\u3001sentinel1:127.0.0.1:26379<\/p>\n<\/li>\n<li>\n<p>5\u3001sentinel2:127.0.0.1:26479<\/p>\n<\/li>\n<li>\n<p>6\u3001sentinel3:127.0.0.1:26579<\/p>\n<\/li>\n<li>\n<p>7\u3001\u76d1\u542c\u7684\u4e3b\u673a\u540d:mymaster<\/p>\n<\/li>\n<li>\n<p>8\u3001\u9644\u4e0asentinel1\u7684\u914d\u7f6e<\/p>\n<\/li>\n<\/ul>\n<pre>port&nbsp;26379\nsentinel&nbsp;monitor&nbsp;mymaster&nbsp;127.0.0.1&nbsp;6379&nbsp;2\nsentinel&nbsp;down-after-milliseconds&nbsp;mymaster&nbsp;5000\nsentinel&nbsp;parallel-syncs&nbsp;mymaster&nbsp;1\nsentinel&nbsp;failover-timeout&nbsp;mymaster&nbsp;15000<\/pre>\n<h4>\u65b0\u5efaspring boot\u5de5\u7a0b\uff0c\u5e76\u52a0\u5165Redis\u4f9d\u8d56<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230515\/16841212356461a6935b330.png\" class=\"aligncenter\"><\/p>\n<h4>\u5de5\u7a0b\u7ed3\u6784<\/h4>\n<p>\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230515\/16841212356461a69364fb1.png\" class=\"aligncenter\"><\/p>\n<p>pom\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?xml&nbsp;version=\"1.0\"&nbsp;encoding=\"UTF-8\"?&gt;\n&lt;project&nbsp;xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"&nbsp;xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0&nbsp;http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n&nbsp;\n\t&lt;groupId&gt;com.chhliu.springboot.redis&lt;\/groupId&gt;\n\t&lt;artifactId&gt;springboot-redis&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;packaging&gt;jar&lt;\/packaging&gt;\n&nbsp;\n\t&lt;name&gt;springboot-redis&lt;\/name&gt;\n\t&lt;description&gt;Demo&nbsp;project&nbsp;for&nbsp;Spring&nbsp;Boot&nbsp;redis&lt;\/description&gt;\n&nbsp;\n\t&lt;parent&gt;\n\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n\t\t&lt;version&gt;1.4.3.RELEASE&lt;\/version&gt;\n\t\t&lt;relativePath\/&gt;&nbsp;&lt;!--&nbsp;lookup&nbsp;parent&nbsp;from&nbsp;repository&nbsp;--&gt;\n\t&lt;\/parent&gt;\n&nbsp;\n\t&lt;properties&gt;\n\t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n\t\t&lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;\n\t\t&lt;java.version&gt;1.7&lt;\/java.version&gt;\n\t&lt;\/properties&gt;\n&nbsp;\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;\n\t\t&lt;\/dependency&gt;\n&nbsp;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n&nbsp;\n\t&lt;build&gt;\n\t\t&lt;plugins&gt;\n\t\t\t&lt;plugin&gt;\n\t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/build&gt;\n&lt;\/project&gt;<\/pre>\n<h4>\u4fee\u6539application.properties\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u914d\u7f6e\u6587\u4ef6\u6dfb\u52a0\u5185\u5bb9\u5982\u4e0b:<\/p>\n<pre>########################################################\n###REDIS&nbsp;(RedisProperties)&nbsp;redis\u57fa\u672c\u914d\u7f6e\uff1b\n########################################################\n#&nbsp;database&nbsp;name\nspring.redis.database=0\n#&nbsp;server&nbsp;host1&nbsp;\u5355\u673a\u4f7f\u7528\uff0c\u5bf9\u5e94\u670d\u52a1\u5668ip\n#spring.redis.host=127.0.0.1&nbsp;&nbsp;\n#&nbsp;server&nbsp;password&nbsp;\u5bc6\u7801\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u53ef\u4e0d\u914d\n#spring.redis.password=\n#connection&nbsp;port&nbsp;&nbsp;\u5355\u673a\u4f7f\u7528\uff0c\u5bf9\u5e94\u7aef\u53e3\u53f7\n#spring.redis.port=6379\n#&nbsp;pool&nbsp;settings&nbsp;...\u6c60\u914d\u7f6e\nspring.redis.pool.max-idle=8\nspring.redis.pool.min-idle=0\nspring.redis.pool.max-active=8\nspring.redis.pool.max-wait=-1\n#&nbsp;name&nbsp;of&nbsp;Redis&nbsp;server&nbsp;&nbsp;\u54e8\u5175\u76d1\u542c\u7684Redis&nbsp;server\u7684\u540d\u79f0\nspring.redis.sentinel.master=mymaster\n#&nbsp;comma-separated&nbsp;list&nbsp;of&nbsp;host:port&nbsp;pairs&nbsp;&nbsp;\u54e8\u5175\u7684\u914d\u7f6e\u5217\u8868\nspring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26479,127.0.0.1:26579<\/pre>\n<h4>\u65b0\u5efaRedis\u670d\u52a1<\/h4>\n<pre>package&nbsp;com.chhliu.springboot.redis;&nbsp;\nimport&nbsp;org.springframework.beans.factory.annotation.Autowired;\nimport&nbsp;org.springframework.data.redis.core.RedisTemplate;\nimport&nbsp;org.springframework.data.redis.core.StringRedisTemplate;\nimport&nbsp;org.springframework.stereotype.Service;\n&nbsp;\n@Service(\"redisService\")\npublic&nbsp;class&nbsp;RedisService&nbsp;{\n\t@Autowired&nbsp;\/\/\u64cd\u4f5c\u5b57\u7b26\u4e32\u7684template\uff0cStringRedisTemplate\u662fRedisTemplate\u7684\u4e00\u4e2a\u5b50\u96c6\n\tprivate&nbsp;StringRedisTemplate&nbsp;stringRedisTemplate;\n\t\n\t@Autowired&nbsp;&nbsp;\/\/&nbsp;RedisTemplate\uff0c\u53ef\u4ee5\u8fdb\u884c\u6240\u6709\u7684\u64cd\u4f5c\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;RedisTemplate&lt;Object,Object&gt;&nbsp;redisTemplate;&nbsp;&nbsp;\n\t\n\tpublic&nbsp;void&nbsp;set(String&nbsp;key,&nbsp;String&nbsp;value){\n\t\tstringRedisTemplate.opsForValue().set(key,&nbsp;value);\n\t}\n\t\n\tpublic&nbsp;void&nbsp;set(Student&nbsp;s){\n\t\tredisTemplate.opsForValue().set(s.getId(),&nbsp;s);\n\t}\n\t\n\tpublic&nbsp;String&nbsp;get(String&nbsp;key){\n\t\treturn&nbsp;stringRedisTemplate.opsForValue().get(key);\n\t}\n\t\n\tpublic&nbsp;Student&nbsp;getStudent(String&nbsp;key){\n\t\treturn&nbsp;(Student)&nbsp;redisTemplate.opsForValue().get(key);\n\t}\n}<\/pre>\n<p>\u4f9d\u8d56\u7684vo\u5982\u4e0b\uff1a<\/p>\n<pre>package&nbsp;com.chhliu.springboot.redis;&nbsp;\nimport&nbsp;java.io.Serializable;&nbsp;\npublic&nbsp;class&nbsp;Student&nbsp;implements&nbsp;Serializable{&nbsp;\n\t\/**\n\t&nbsp;*&nbsp;\n\t&nbsp;*\/\n\tprivate&nbsp;static&nbsp;final&nbsp;long&nbsp;serialVersionUID&nbsp;=&nbsp;1L;\n\tprivate&nbsp;String&nbsp;id;\t\n\tprivate&nbsp;String&nbsp;name;\t\n\tprivate&nbsp;String&nbsp;age;\t\n\tprivate&nbsp;String&nbsp;grade;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u7701\u7565getter,setter\n\t\/**\n\t&nbsp;*&nbsp;attention:\n\t&nbsp;*&nbsp;Details\uff1aTODO\n\t&nbsp;*&nbsp;@author&nbsp;chhliu\n\t&nbsp;*&nbsp;\u521b\u5efa\u65f6\u95f4\uff1a2017-1-18&nbsp;\u4e0b\u53482:24:39\n\t&nbsp;*&nbsp;@return\n\t&nbsp;*\/\n\t@Override\n\tpublic&nbsp;String&nbsp;toString()&nbsp;{\n\t\treturn&nbsp;\"Student&nbsp;[id=\"&nbsp;+&nbsp;id&nbsp;+&nbsp;\",&nbsp;name=\"&nbsp;+&nbsp;name&nbsp;+&nbsp;\",&nbsp;age=\"&nbsp;+&nbsp;age\n\t\t\t\t+&nbsp;\",&nbsp;grade=\"&nbsp;+&nbsp;grade&nbsp;+&nbsp;\"]\";\n\t}\n}<\/pre>\n<h4>\u6d4b\u8bd5\u7c7b<\/h4>\n<pre>package&nbsp;com.chhliu.springboot.redis;&nbsp;\nimport&nbsp;org.junit.Test;\nimport&nbsp;org.junit.runner.RunWith;\nimport&nbsp;org.springframework.beans.factory.annotation.Autowired;\nimport&nbsp;org.springframework.boot.test.context.SpringBootTest;\nimport&nbsp;org.springframework.test.context.junit4.SpringRunner;\n&nbsp;\n@RunWith(SpringRunner.class)\n@SpringBootTest\npublic&nbsp;class&nbsp;SpringbootRedisApplicationTests&nbsp;{\t\n\t@Autowired\n\tprivate&nbsp;RedisService&nbsp;service;\n&nbsp;\n\t@Test\n\tpublic&nbsp;void&nbsp;contextLoads()&nbsp;{\n\t\tservice.set(\"myname\",&nbsp;\"chhliu\");\n\t\tStudent&nbsp;s&nbsp;=&nbsp;new&nbsp;Student();\n\t\ts.setId(\"001\");\n\t\ts.setName(\"chhliu\");\n\t\ts.setGrade(\"\u4e00\u5e74\u7ea7\");\n\t\ts.setAge(\"28\");\n\t\tservice.set(s);\n\t\t\n\t\tString&nbsp;name&nbsp;=&nbsp;service.get(\"myname\");\n\t\tSystem.out.println(\"name:\"+name);\n\t\t\n\t\tStudent&nbsp;stu&nbsp;=&nbsp;service.getStudent(\"001\");\n\t\tSystem.out.println(stu);\n\t}&nbsp;\n}<\/pre>\n<h4>\u6d4b\u8bd5\u7ed3\u679c<\/h4>\n<blockquote>\n<p>name:chhliu<\/p>\n<p>Student [id=001, name=chhliu, age=28, grade=\u4e00\u5e74\u7ea7]<\/p>\n<\/blockquote>\n<h3>redis\u54e8\u5175\u6a21\u5f0fsentinel\u4e0espringboot\u96c6\u6210<\/h3>\n<p>Redis\u7684\u54e8\u5175\u6a21\u5f0f\u662f\u5b98\u65b9\u63d0\u4f9b\u7684\u4e00\u79cd\u9ad8\u53ef\u7528\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u914d\u7f6e\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<h4>\u5b89\u88c5Redis\u96c6\u7fa4<\/h4>\n<p>\u672c\u6587\u4f7f\u7528redis-5.0.5\uff0credis\u5b89\u88c5\u5728\/soft\/redis\u76ee\u5f55\u4e0b\uff0c\u9700\u65b0\u5efa\/soft\/redis\/data\u76ee\u5f55<\/p>\n<p><strong>\u4e3b\u8282\u70b9\u914d\u7f6e &nbsp;<\/strong><\/p>\n<p>vim config\/redis-6379.conf<\/p>\n<pre>#&nbsp;bind&nbsp;127.0.0.1&nbsp;\nport&nbsp;6379\nprotected-mode&nbsp;no\ndaemonize&nbsp;yes\npidfile&nbsp;\"\/var\/run\/redis_6379.pid\"\ndir&nbsp;\"\/soft\/redis\/data\"\ndbfilename&nbsp;\"dump-6379.rdb\"\nlogfile&nbsp;\"log-6379.log\"<\/pre>\n<p><strong>\u4ece\u8282\u70b91\u914d\u7f6e<\/strong><\/p>\n<p>vim config\/redis-6380.conf<\/p>\n<pre>#&nbsp;bind&nbsp;127.0.0.1\nport&nbsp;6380\nprotected-mode&nbsp;no\ndaemonize&nbsp;yes\npidfile&nbsp;\"\/var\/run\/redis_6380.pid\"\ndir&nbsp;\"\/soft\/redis\/data\"\ndbfilename&nbsp;\"dump-6380.rdb\"\nlogfile&nbsp;\"log-6380.log\"\n&nbsp;\nreplicaof&nbsp;192.168.4.176&nbsp;6379<\/pre>\n<p><strong>\u4ece\u8282\u70b92\u914d\u7f6e<\/strong><\/p>\n<p>vim config\/redis-6381.conf<\/p>\n<pre>#&nbsp;bind&nbsp;127.0.0.1\nport&nbsp;6381\nprotected-mode&nbsp;no\ndaemonize&nbsp;yes\npidfile&nbsp;\"\/var\/run\/redis_6381.pid\"\ndir&nbsp;\"\/soft\/redis\/data\"\ndbfilename&nbsp;\"dump-6381.rdb\"\nlogfile&nbsp;\"log-6381.log\"\n&nbsp;\nreplicaof&nbsp;192.168.4.176&nbsp;6379<\/pre>\n<p><strong>\u914d\u7f6e\u8bf4\u660e<\/strong><\/p>\n<p># bind 127.0.0.1 &nbsp;\u6ce8\u91ca\u6389\u8fd9\u914d\u7f6e\uff0c\u4ee5\u4fbf\u5176\u4ed6\u673a\u5668\u7684\u80fd\u8fde\u63a5redis<\/p>\n<p>protected-mode no \u5173\u95ed\u4fdd\u62a4\u6a21\u5f0f\uff0c\u4ee5\u4fbf\u5176\u4ed6\u673a\u5668\u7684\u80fd\u8fde\u63a5redis<\/p>\n<p>daemonize\u540e\u53f0\u6a21\u5f0f\u542f\u52a8<\/p>\n<p>redis-v5\u7248\u672c\u4f7f\u7528replicaof\u66ff\u6362\u65e7\u7684slaveof\u6307\u4ee4\u3002<\/p>\n<p>\u542f\u52a8\u8fd93\u4e2a\u8282\u70b9\uff0c\u5728\/soft\/redis\u76ee\u5f55\u4e0b\u8fd0\u884c<\/p>\n<pre>redis-server&nbsp;config\/redis-6379.conf\nredis-server&nbsp;config\/redis-6380.conf\nredis-server&nbsp;config\/redis-6381.conf<\/pre>\n<p>\u6253\u5f00\u4e3b\u8282\u70b9\u5ba2\u6237\u7aef\u770b\u770b\u914d\u7f6e\u662f\u5426\u6210\u529f<\/p>\n<pre>redis-cli&nbsp;-p&nbsp;6379\ninfo&nbsp;replication<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230515\/16841212356461a6936fbde.png\" class=\"aligncenter\"><\/p>\n<p>\u518d\u914d\u7f6e3\u4e2a\u54e8\u5175\uff0c\u76d1\u63a7\u96c6\u7fa4<\/p>\n<p><strong>\u54e8\u5175\u8282\u70b91<\/strong><\/p>\n<p>vim config\/redis-sentinel-26379.conf<\/p>\n<pre>port&nbsp;26379\ndaemonize&nbsp;yes\npidfile&nbsp;\"\/var\/run\/redis-sentinel-26379.pid\"\ndir&nbsp;\/tmp\nlogfile&nbsp;\"log-sentinel-26379.log\"\nsentinel&nbsp;monitor&nbsp;mymaster&nbsp;192.168.4.176&nbsp;6379&nbsp;2\nsentinel&nbsp;down-after-milliseconds&nbsp;mymaster&nbsp;30000\nsentinel&nbsp;parallel-syncs&nbsp;mymaster&nbsp;1\nsentinel&nbsp;failover-timeout&nbsp;mymaster&nbsp;180000\nsentinel&nbsp;deny-scripts-reconfig&nbsp;yes<\/pre>\n<p><strong>\u54e8\u5175\u8282\u70b92<\/strong><\/p>\n<p>vim config\/redis-sentinel-26380.conf<\/p>\n<pre>port&nbsp;26380\ndaemonize&nbsp;yes\npidfile&nbsp;\"\/var\/run\/redis-sentinel-26380.pid\"\ndir&nbsp;\/tmp\nlogfile&nbsp;\"log-sentinel-26380.log\"\nsentinel&nbsp;monitor&nbsp;mymaster&nbsp;192.168.4.176&nbsp;6379&nbsp;2\nsentinel&nbsp;down-after-milliseconds&nbsp;mymaster&nbsp;30000\nsentinel&nbsp;parallel-syncs&nbsp;mymaster&nbsp;1\nsentinel&nbsp;failover-timeout&nbsp;mymaster&nbsp;180000\nsentinel&nbsp;deny-scripts-reconfig&nbsp;yes<\/pre>\n<p><strong>\u54e8\u5175\u8282\u70b93<\/strong><\/p>\n<p>vim config\/redis-sentinel-26381.conf<\/p>\n<pre>port&nbsp;26381\ndaemonize&nbsp;yes\npidfile&nbsp;\"\/var\/run\/redis-sentinel-26381.pid\"\ndir&nbsp;\/tmp\nlogfile&nbsp;\"log-sentinel-26381.log\"\nsentinel&nbsp;monitor&nbsp;mymaster&nbsp;192.168.4.176&nbsp;6379&nbsp;2\nsentinel&nbsp;down-after-milliseconds&nbsp;mymaster&nbsp;30000\nsentinel&nbsp;parallel-syncs&nbsp;mymaster&nbsp;1\nsentinel&nbsp;failover-timeout&nbsp;mymaster&nbsp;180000\nsentinel&nbsp;deny-scripts-reconfig&nbsp;yes<\/pre>\n<p><strong>\u914d\u7f6e\u8bf4\u660e<\/strong><\/p>\n<pre>monitor&nbsp;mymaster&nbsp;192.168.4.176&nbsp;6379&nbsp;2<\/pre>\n<p>master\u7684\u540d\u79f0\u4e3amymaster\uff0c\u5176\u4e3b\u673aIP\u5730\u5740\u4e3a192.168.4.176\u3002\u540e\u9762\u76842\u8868\u793a\u67092\u4e2asentinel\u8ba4\u4e3amaster\u4e0b\u7ebf\u4e86\uff0c\u5219\u7ebf\u4e0bmaster\uff0c\u5efa\u8bae\u8bbe\u7f6e\u4e3a sentinel\u8282\u70b9\u6570\/2 + 1<\/p>\n<pre>down-after-milliseconds<\/pre>\n<p>\u53d1\u9001ping\u8bf7\u6c42\u7ed9redis\u8282\u70b9\uff0c\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u672a\u6536\u5230\u56de\u590d\uff0c\u5219\u8ba4\u4e3a\u8be5\u8282\u70b9\u5e94\u8be5\u88ab\u4e0b\u7ebf<\/p>\n<pre>parallel-syncs<\/pre>\n<p>\u5728\u6267\u884c\u6545\u969c\u8f6c\u79fb\u65f6\uff0c\u6700\u591a\u53ef\u4ee5\u6709\u591a\u5c11\u4e2a\u4ece\u8282\u70b9\u540c\u65f6\u5bf9\u65b0\u7684\u4e3b\u670d\u52a1\u5668\u8fdb\u884c\u540c\u6b65\u3002<\/p>\n<p><strong>\u542f\u52a8\u54e8\u5175<\/strong><\/p>\n<pre>redis-sentinel&nbsp;config\/redis-sentinel-26379.conf\nredis-sentinel&nbsp;config\/redis-sentinel-26380.conf\nredis-sentinel&nbsp;config\/redis-sentinel-26381.conf<\/pre>\n<p>\u914d\u7f6espring-boot<\/p>\n<p>pom.xml\u4e2d\u5bfc\u5165\u4f9d\u8d56<\/p>\n<pre>&lt;dependency&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/pre>\n<p>application.properties\u52a0\u5165\u4e24\u884c\u914d\u7f6e<\/p>\n<pre>#&nbsp;\u4f7f\u7528\u54e8\u5175\u6a21\u5f0f\u4e0d\u80fd\u52a0\u4ee5\u4e0b\u4e24\u884c\u914d\u7f6e\uff0c\u5176\u4ed6\u914d\u7f6e\u53ef\u4ee5\u52a0\n#&nbsp;spring.redis.host=192.168.4.176\n#&nbsp;spring.redis.port=6379\n&nbsp;\nspring.redis.sentinel.master=mymaster\nspring.redis.sentinel.nodes=192.168.4.176:26379,&nbsp;192.168.4.176:26380,&nbsp;192.168.4.176:26381<\/pre>\n<p>\u5199\u4e00\u4e2a\u6d4b\u8bd5\u7c7b\u8fd0\u884c<\/p>\n<pre>@RunWith(SpringRunner.class)\n@SpringBootTest\npublic&nbsp;class&nbsp;Sentinel001&nbsp;{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;@Autowired\n&nbsp;&nbsp;&nbsp;&nbsp;RedisTemplate&nbsp;redisTemplate;\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;@Test\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test001()&nbsp;throws&nbsp;Exception{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(true){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;key&nbsp;=&nbsp;\"time:\"&nbsp;+&nbsp;new&nbsp;Date().getTime();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.opsForValue().set(key,&nbsp;new&nbsp;Date().getTime());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeUnit.MILLISECONDS.sleep(100L);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(redisTemplate.opsForValue().get(key));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;\n}<\/pre>\n<p>\u7136\u540e\u6740\u6389master\u5b9e\u4f8b\uff08\u7aef\u53e3\u53f7\u4e3a6379\u7684redis\uff09\u7684\u8fdb\u7a0b<\/p>\n<pre>ps&nbsp;-ef|grep&nbsp;redis\nkill&nbsp;-9&nbsp;11110<\/pre>\n<p>\u89c2\u5bdf\u4ee3\u7801\u7f16\u8f91\u5668\u63a7\u5236\u53f0\u8f93\u51fa\uff0c\u7ecf\u8fc7\u77ed\u6682\u7684\u65f6\u95f4\uff08\u5927\u6982\u662f50s\uff09\u540e\uff0c\u7a0b\u5e8f\u91cd\u65b0\u8fd0\u884c\u6b63\u5e38<\/p>\n<p>\u57286380\u548c6381\u8282\u70b9\u6267\u884cinfo replication\uff0c\u53d1\u73b06381\u53d8\u6210\u4e86\u4e3b\u8282\u70b9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230515\/16841212356461a6937a4e6.png\" class=\"aligncenter\"><\/p>\n<p>\u67e5\u770b\u4e0b6380\u30016381\u7684\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre>cat&nbsp;config\/redis-6380.conf\nreplicaof&nbsp;192.168.4.176&nbsp;6381\nreplicaof&nbsp;\u53d8\u6210\u4e86192.168.4.176&nbsp;6381\uff0c\u800c\u4e0d\u662f\u521a\u5f00\u59cb\u914d\u7f6e\u65f6\u7684192.168.4.176&nbsp;6379\ncat&nbsp;config\/redis-6381.conf&nbsp;\nreplicaof&nbsp;\u7684\u914d\u7f6e\u88ab\u5220\u9664\u4e86<\/pre>\n<p>\u91cd\u542f\u4e0b6379\u8fd9\u4e2aredis\u5b9e\u4f8b<\/p>\n<pre>redis-server&nbsp;config\/redis-6379.conf<\/pre>\n<p>6379\u53d8\u6210\u4e866381\u7684\u4ece\u8282\u70b9<\/p>\n<p>RedisTemplate\u5e76\u672a\u5b9e\u73b0\u8bfb\u5199\u5206\u79bb\uff0c\u56e0\u6b64\u8bfb\u5199\u64cd\u4f5c\u90fd\u4f1a\u5728\u4e3b\u8282\u70b9\u4e0a\u6267\u884c\uff0c\u8fd9\u6709\u70b9\u8ba9\u4eba\u4e0d\u723d\u3002\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u57283\u4e2aredis\u5ba2\u6237\u7aef\u8fd0\u884cmonitor\u53d1\u73b0\uff0c\u53ea\u6709master\u4f1a\u8fd0\u884cget\u3001set\u547d\u4ee4\uff0c\u4ece\u8282\u70b9\u53ea\u8fd0\u884c\u4e86set\u547d\u4ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>spring&nbsp;boot&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201840","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201840"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201840\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}