spring boot整合redis主从sentinel

一、一主二从三sentinel配置

1、master:127.0.0.1:6379

2、slave1:127.0.0.1:6380

3、slave2:127.0.0.1:6381

4、sentinel1:127.0.0.1:26379

5、sentinel2:127.0.0.1:26479

6、sentinel3:127.0.0.1:26579

7、监听的主机名:mymaster

8、附上sentinel1的配置

port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 15000
二、新建spring boot工程,并加入Redis依赖

spring boot整合redis主从sentinel_第1张图片

三、工程结构

工程结构如下:

spring boot整合redis主从sentinel_第2张图片

pom文件如下:



	4.0.0

	com.chhliu.springboot.redis
	springboot-redis
	0.0.1-SNAPSHOT
	jar

	springboot-redis
	Demo project for Spring Boot redis

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.3.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.7
	

	
		
			org.springframework.boot
			spring-boot-starter-data-redis
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

四、修改application.properties配置文件

配置文件添加内容如下:

########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
# database name
spring.redis.database=0
# server host1 单机使用,对应服务器ip
#spring.redis.host=127.0.0.1  
# server password 密码,如果没有设置可不配
#spring.redis.password=
#connection port  单机使用,对应端口号
#spring.redis.port=6379
# pool settings ...池配置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
# name of Redis server  哨兵监听的Redis server的名称
spring.redis.sentinel.master=mymaster
# comma-separated list of host:port pairs  哨兵的配置列表
spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26479,127.0.0.1:26579
五、新建Redis服务

package com.chhliu.springboot.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service("redisService")
public class RedisService {
	@Autowired //操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired  // RedisTemplate,可以进行所有的操作
        private RedisTemplate redisTemplate;  
	
	public void set(String key, String value){
		stringRedisTemplate.opsForValue().set(key, value);
	}
	
	public void set(Student s){
		redisTemplate.opsForValue().set(s.getId(), s);
	}
	
	public String get(String key){
		return stringRedisTemplate.opsForValue().get(key);
	}
	
	public Student getStudent(String key){
		return (Student) redisTemplate.opsForValue().get(key);
	}
}
依赖的vo如下:

package com.chhliu.springboot.redis;

import java.io.Serializable;

public class Student implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	
	private String name;
	
	private String age;
	
	private String grade;

       // 省略getter,setter
	/**
	 * attention:
	 * Details:TODO
	 * @author chhliu
	 * 创建时间:2017-1-18 下午2:24:39
	 * @return
	 */
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age
				+ ", grade=" + grade + "]";
	}
}
六,测试类

package com.chhliu.springboot.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
	
	@Autowired
	private RedisService service;

	@Test
	public void contextLoads() {
		service.set("myname", "chhliu");
		Student s = new Student();
		s.setId("001");
		s.setName("chhliu");
		s.setGrade("一年级");
		s.setAge("28");
		service.set(s);
		
		String name = service.get("myname");
		System.out.println("name:"+name);
		
		Student stu = service.getStudent("001");
		System.out.println(stu);
	}

}
七、测试结果

name:chhliu
Student [id=001, name=chhliu, age=28, grade=一年级]

你可能感兴趣的:(redis,spring,boot,spring,cloud微服务)