| | |
| | | package com.ltkj.framework.config; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.cache.annotation.CachingConfigurerSupport; |
| | | import org.springframework.cache.annotation.EnableCaching; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| | | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.script.DefaultRedisScript; |
| | | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| | | import redis.clients.jedis.JedisPoolConfig; |
| | | |
| | | import java.io.*; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * redis配置 |
| | |
| | | */ |
| | | @Configuration |
| | | @EnableCaching |
| | | @Slf4j |
| | | public class RedisConfig extends CachingConfigurerSupport { |
| | | |
| | | @Value ("${config.properties}") |
| | | private String url; |
| | | |
| | | @Value ("${config.path}") |
| | | private String path; |
| | | |
| | | @Bean |
| | | @SuppressWarnings(value = {"unchecked", "rawtypes"}) |
| | | public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { |
| | |
| | | } |
| | | |
| | | @Bean |
| | | public JedisPoolConfig jedisPoolConfig() { |
| | | JedisPoolConfig config = new JedisPoolConfig(); |
| | | // 设置JedisPoolConfig的相关参数,例如最大连接数、最大空闲时间等 |
| | | // config.setMinIdle(0); |
| | | // config.setMaxIdle(8); |
| | | // config.setMaxTotal(8); |
| | | // config.setMaxWaitMillis(-1); |
| | | // config.setTestOnBorrow(true); |
| | | // config.setTestOnReturn(true); |
| | | return config; |
| | | } |
| | | @Bean |
| | | public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) { |
| | | JedisConnectionFactory factory = new JedisConnectionFactory(); |
| | | // 从文件中读取配置信息 |
| | | try { |
| | | FileInputStream fis = null; |
| | | Properties props = new Properties(); |
| | | try { |
| | | fis = new FileInputStream(url); |
| | | } catch (FileNotFoundException e) { |
| | | log.info("配置文件找不到 系统正在创建!"); |
| | | File f = new File(path); |
| | | if(!f.exists()){ |
| | | f.mkdirs(); |
| | | } |
| | | File file = new File(url); |
| | | try { |
| | | FileWriter fileWriter = new FileWriter(file); |
| | | fileWriter.write("ip = 你的主数据库连接ip地址\n"); |
| | | fileWriter.write("prot = 你的主数据库连接端口\n"); |
| | | fileWriter.write("name = 你的主数据库连接名称\n"); |
| | | fileWriter.write("username = 你的主数据库连接用户名\n"); |
| | | fileWriter.write("password = 你的主数据库连接密码\n"); |
| | | fileWriter.write("redisIp = 你的redisIp地址"); |
| | | fileWriter.write("redisProt = 你的redis端口"); |
| | | fileWriter.write("redisIpDatabase = 你的redis链接库"); |
| | | fileWriter.write("redisPassword = 你的redis密码"); |
| | | fileWriter.write(""); |
| | | fileWriter.close(); |
| | | log.info("配置文件创建成功!"); |
| | | } catch (IOException ioException) { |
| | | log.info("配置文件创建失败 请联系管理员手动创建!"); |
| | | ioException.printStackTrace(); |
| | | } |
| | | e.printStackTrace(); |
| | | } |
| | | props.load(fis); |
| | | fis.close(); |
| | | // 获取属性值并赋值 |
| | | factory.setPoolConfig(jedisPoolConfig); |
| | | // 设置Redis服务器的地址和端口号 |
| | | factory.setHostName(props.getProperty("redisIp")); |
| | | factory.setPort(Integer.parseInt(props.getProperty("redisProt"))); |
| | | // 如果需要密码验证,设置密码 |
| | | factory.setPassword(props.getProperty("redisPassword")); |
| | | // 设置其他参数,如数据库索引等 |
| | | factory.setDatabase(Integer.parseInt(props.getProperty("redisIpDatabase"))); |
| | | // 最后,初始化连接 |
| | | factory.afterPropertiesSet(); |
| | | log.info("redis连接成功!!!"); |
| | | } catch (IOException e) { |
| | | log.info("redis连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return factory; |
| | | } |
| | | |
| | | |
| | | @Bean |
| | | public DefaultRedisScript<Long> limitScript() { |
| | | DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(); |
| | | redisScript.setScriptText(limitScriptText()); |