Spring環境下操作Redis叢集
阿新 • • 發佈:2018-11-11
Spring環境下操作Redis叢集
1.java環境下操作單節點redis資料庫
<!-- jedis操作redis資料庫 --> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId><version>2.9.0</version> </dependency>
@Test public void testInert() { //叢集所需的主機以及埠號 Set<HostAndPort> nodes = new HashSet<HostAndPort>(); nodes.add(new HostAndPort("10.0.45.86",7000)); nodes.add(new HostAndPort("10.0.45.86",7001)); nodes.add(new HostAndPort("10.0.45.86",7002));nodes.add(new HostAndPort("10.0.45.86",7003)); nodes.add(new HostAndPort("10.0.45.86",7004)); nodes.add(new HostAndPort("10.0.45.86",7005)); nodes.add(new HostAndPort("10.0.45.86",7006)); //建立jedisCluster物件 JedisCluster jedisCluster = new JedisCluster(nodes); String string = jedisCluster.get("age");System.out.println("引數:"+string); }
2.Spring環境下操作redis叢集環境
<!-- abstract宣告不能直接使用可以被繼承 --> <bean id="commonNode" class="redis.clients.jedis.HostAndPort" abstract="true"> <constructor-arg index="0" name="host" value="10.0.45.86"/><!-- 主機 --> </bean> <bean id="node0" parent="commonNode"> <constructor-arg index="1" name="port" value="7000"></constructor-arg> </bean> <bean id="node1" parent="commonNode"> <constructor-arg index="1" name="port" value="7001"/><!-- 埠 --> </bean> <bean id="node2" parent="commonNode"> <constructor-arg index="1" name="port" value="7002"/> </bean> <bean id="node3" parent="commonNode"> <constructor-arg index="1" name="port" value="7003"/> </bean> <bean id="node4" parent="commonNode"> <constructor-arg index="1" name="port" value="7004"/> </bean> <bean id="node5" parent="commonNode"> <constructor-arg index="1" name="port" value="7005"/> </bean> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg index="0" name="nodes"> <set> <ref bean="node0"/> <ref bean="node1"/> <ref bean="node2"/> <ref bean="node3"/> <ref bean="node4"/> <ref bean="node5"/> </set> </constructor-arg> </bean>
@Test public void testInerts() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); JedisCluster bean = (JedisCluster)context.getBean("jedisCluster"); String string = bean.get("age"); System.out.println(string); }