New file |
| | |
| | | package com.ltkj.framework.config; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.redisson.Redisson; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.redisson.config.Config; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import java.io.*; |
| | | import java.util.Properties; |
| | | |
| | | @Slf4j |
| | | @Configuration |
| | | public class RedissionConfig { |
| | | |
| | | @Value("${config.properties}") |
| | | private String url; |
| | | |
| | | @Value ("${config.path}") |
| | | private String path; |
| | | |
| | | @Bean |
| | | public RedissonClient redissonClient(){ |
| | | Config config = new Config(); |
| | | 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(); |
| | | String redisPassword = props.getProperty("redisPassword"); |
| | | int database = Integer.parseInt(props.getProperty("redisIpDatabase")); |
| | | String address = "redis://"+props.getProperty("redisIp")+":"+props.getProperty("redisProt"); |
| | | config.useSingleServer() |
| | | .setAddress(address) |
| | | .setPassword(redisPassword) |
| | | .setDatabase(database); |
| | | log.info("redisson连接成功!!!"); |
| | | } catch (IOException e) { |
| | | log.info("redisson连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return Redisson.create(config); |
| | | } |
| | | } |