spring boot学习系列之整合Redis集群3

现在学习springboot项目与Redis K-V存储系统的整合。在虚拟机上模拟了Redis集群,如果觉得启动不方便,可以用SecureCRT,XShell等终端仿真程序工具去连虚拟机。如果虚拟机没有安装SSH,使用NAT模式连接虚拟机网络。

安装SSH

sudo apt-get install openssh-server

安装完后自动启动。然后就可以用终端仿真程序工具连了,如果是Ubuntu无法更新,可以参考:http://wenqy.com/2016/03/16/ubuntu%e6%95%b0%e6%8d%ae%e6%ba%90%e5%88%97%e8%a1%a8.html  Ubuntu如何修改数据源列表。

开发

添加依赖,加载Jedis

  1. <!– https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis –>
  2. <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-redis</artifactId>
  5.     <version>1.4.7.RELEASE</version>
  6. </dependency>

添加redis配置

springboot配置文件中添加集群的信息

  1. #整合redis,使用redis实现缓存
  2. # Redis数据库索引(默认为0)
  3. #spring.redis.database=0
  4. # Redis服务器地址,单机
  5. #spring.redis.host=192.168.56.101
  6. # Redis服务器连接端口,单机
  7. #spring.redis.port=6379
  8. spring.redis.cluster.nodes=192.168.56.101:30001,192.168.56.101:30002,192.168.56.101:30003,192.168.56.101:30004,192.168.56.101:30005,192.168.56.101:30006
  9. # Redis服务器连接密码(默认为空)
  10. spring.redis.password=
  11. # 连接池最大连接数(使用负值表示没有限制)
  12. spring.redis.pool.max-active=8
  13. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  14. spring.redis.pool.max-wait=-1
  15. # 连接池中的最大空闲连接
  16. spring.redis.pool.max-idle=8
  17. # 连接池中的最小空闲连接
  18. spring.redis.pool.min-idle=0
  19. # 连接超时时间(毫秒)
  20. spring.redis.timeout=0
  21. spring.redis.commandTimeout=5000

项目入口类加入注解@EnableCaching,添加到缓存

  1. @SpringBootApplication
  2. @MapperScan(basePackages = “com.wenqy.mapper”)
  3. @EnableCaching
  4. public class HelloWorldApp {
  5.     public static void main(String[] args) {
  6.         SpringApplication.run(HelloWorldApp.class, args);
  7.     }
  8. }
  9. /**
  10.      * 
  11.     * @Title: findAll 
  12.     * @Description: 加入redis,测试缓存 
  13.     * @param @return    设定文件 
  14.     * @return List<User>    返回类型 
  15.     * @throws
  16.      */
  17.     @RequestMapping(“/findAll”)
  18.     @Cacheable(value=“redis”)
  19.     public List<User> findAll() {
  20.         System.out.println(“进入findAll…”);
  21.         List<User> findAll = userService.findAll();
  22.         return findAll;
  23.     }

自定义redis配置,加入集群

  1. @Configuration
  2. @ConditionalOnClass({JedisCluster.class})
  3. public class RedisConfig {
  4.     @Value(“${spring.redis.cluster.nodes}”)
  5.     private String clusterNodes;
  6.     @Value(“${spring.redis.timeout}”)
  7.     private int timeout;
  8.     @Value(“${spring.redis.pool.max-idle}”)
  9.     private int maxIdle;
  10.     @Value(“${spring.redis.pool.max-wait}”)
  11.     private long maxWaitMillis;
  12.     @Value(“${spring.redis.commandTimeout}”)
  13.     private int commandTimeout;
  14.     @Bean
  15.     public JedisCluster getJedisCluster() {
  16.         String[] cNodes = clusterNodes.split(“,”);
  17.         Set<HostAndPort> nodes = new HashSet<HostAndPort>();
  18.         // 分割出集群节点
  19.         for (String node : cNodes) {
  20.             String[] hp = node.split(“:”);
  21.             nodes.add(new HostAndPort(hp[0],Integer.parseInt(hp[1])));
  22.         }
  23.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  24.         jedisPoolConfig.setMaxIdle(maxIdle);
  25.         jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
  26.         // 创建集群对象
  27. //      JedisCluster jedisCluster = new JedisCluster(nodes,commandTimeout);
  28.         JedisCluster jedisCluster = new JedisCluster(nodes,commandTimeout,jedisPoolConfig);
  29.         return jedisCluster;
  30.     }
  31. }

Service实现类中,自动注入jedisCluster

  1. @Autowired
  2. private JedisCluster jedisCluster;

并写入一个测试方法

  1. public String findRedis() {
  2.     jedisCluster.set(“userName”“hello wenqy”);
  3.     return jedisCluster.get(“userName”);
  4. }

在控制层加入调用刚才的service方法

  1. @RequestMapping(“/redis”)
  2. public String findRedis() {
  3.     return userService.findRedis();
  4. }

测试

在findRedis方法中打断点,第一次访问时建立缓存,第二次访问发现没有进入该函数。但返回的结果是一样的。

最后访问

redis_hello_wenqy

安装Redis集群

在前面写过Redis单机版集群http://wenqy.com/2017/02/08/redis%e5%8d%95%e6%9c%ba%e7%89%88%e9%9b%86%e7%be%a4.html,是主从模式的方案。现用redis-trib命令,一个Ruby程序来构建单机集群。

发现gem install redis没办法使用,原来ruby 的gem被和谐了,只能将Ruby的镜像转到国内淘宝镜像。

gem sources --remove https://rubygems.org/$ gem sources -a https://ruby.taobao.org/$ gem sources -l

然后更新源列表,安装Rails

gem update --system
Sudo gem install rake
Sudo gem install -v=1.0.1 rack
Sudo gem install rails --no-ri --no-rdoc -v=3.2.7
Sudo gem install rails

修改创建集群 create-cluster脚本

  1. if [ “$1” == “start” ]
  2. then
  3.     while [ $((PORT < ENDPORT)) != “0” ]; do
  4.         PORT=$((PORT+1))
  5.         echo “Starting $PORT
  6.         ../../src/redis-server /home/wenqy/redis-3.2.6/redis.conf –bind 192.168.56.101 –port $PORT –cluster-enabled yes –cluster-config-file nodes-${PORT}.conf —protected-mode no –cluster-node-timeout $TIMEOUT –appendonly yes –appendfilename appendonly-${PORT}.aof –dbfilename dump-${PORT}.rdb –logfile ${PORT}.log –daemonize yes
  7.     done
  8.     exit 0
  9. fi
  10. if [ “$1” == “create” ]
  11. then
  12.     HOSTS=””
  13.     while [ $((PORT < ENDPORT)) != “0” ]; do
  14.         PORT=$((PORT+1))
  15.         HOSTS=”$HOSTS 192.168.56.101:$PORT
  16.     done
  17.     ../../src/redis-trib.rb create –replicas $REPLICAS $HOSTS
  18.     exit 0
  19. fi

在连接Redis集群时,会出现Could not get a resource from the pool

ConnectException: Connection refused: connect等无法建立连接的错误。

就在加载Redis.conf 配置文件中,覆盖参数。启用集群模式,关闭保护模式,设置超时时间,注释掉IP绑定等等

创建集群

~/redis-3.2.6/utils/create-cluster$ ./create-cluster create

启动集群

~/redis-3.2.6/utils/create-cluster$ ./create-cluster start

关闭集群

~/redis-3.2.6/utils/create-cluster$ ./create-cluster stop

客户端访问,并查看集群信息和节点

~/redis-3.2.6/src$ ./redis-cli -c -h 192.168.56.101 -p 30001
cluster info

发现有6个节点,集群状态运行正常。
redis_cluster_info

Cluster nodes

发现Redis集群,3主3从
redis_cluster_nodes

对于开发使用而言,模式都是一样的。但关系型和非关系数据库有适应不同的结构化和非结构化数据。

参考

https://redis.io/topics/cluster-tutorial

发表评论

电子邮件地址不会被公开。 必填项已用*标注

0 + 2 = ?