Redisson学习———学习redisson加载yaml配置信息

基于spring反射工具类获取yaml配置中指定配置信息

  • yaml
spring:
  redis:
    client-name: redisson_demo
  • properties
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

	private String clientName;

	public String getClientName() {
		return this.clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
}
  • test

通过ReflectionUtils工具类反射获取clientName的值

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RedissonDemoApplication.class)
@Slf4j
public class RedissonDemoApplicationTests {
	@Autowired
    private RedisProperties redisProperties;
	@Test
    public void getSimpleYamlConfig(){
        Method clientNameMethod = ReflectionUtils.findMethod(RedisProperties.class, "getClientName");

        String clientName = null;
        if (clientNameMethod != null) {
            clientName = (String) ReflectionUtils.invokeMethod(clientNameMethod, redisProperties);
        }

        System.out.println(clientName);
	}
}

Redisson配置加载—配置在application.yaml中

yaml

以下配置二选一即可,他们的不同点点在于redisson . config 后面的纯文本信息表达方式不同,分为yaml和json俩种表达方式
(其中 spring.redos.redisson.config 后面的 | 的含义表示config配置对应的value为纯文本信息)

  • yaml文本
spring:
  redis:
    host: xxx.host
    port: 6379
    database: 0
    password: xxxpassword
    timeout: 3600
    lettuce:
      pool:
        max-active: 3
        max-wait: 5
        max-idle: 3
        min-idle: 0
    client-name: redisson_demo
# 其中 redisson . config 后面的 | 的含义表示config配置对应的value为纯文本信息
    redisson:
      config: |
         singleServerConfig:
           address: redis://xxx.host:6379
           connectionMinimumIdleSize: 11
           password: xxxpassword
           database: 0
  • json文本
spring:
  redis:
    host: xxx.host
    port: 6379
    database: 0
    password: xxxpassword
    timeout: 3600
    lettuce:
      pool:
        max-active: 3
        max-wait: 5
        max-idle: 3
        min-idle: 0
    client-name: redisson_demo
# 其中 redisson . config 后面的 | 的含义表示config配置对应的value为纯文本信息
    redisson:
      config: |
        {
          "singleServerConfig": {
            "address": "redis://xxx.host:6379",
            "connectionMinimumIdleSize": 11,
            "password": "2023!@#raven",
            "database": 0
          }
        }

RedissonProperties

config 设置为String 类型 用于加载spring.redis.redisson下的纯文本配置

@ConfigurationProperties(prefix = "spring.redis.redisson")
    public class RedissonProperties {

        private String config;

        private String file;

        public String getConfig() {
            return config;
        }

        public void setConfig(String config) {
            this.config = config;
        }

        public String getFile() {
            return file;
        }

        public void setFile(String file) {
            this.file = file;
        }
    }

Redisson config解析类

该类提供默认的config配置,并提供将各种类型的数据解析为config配置类的能力

public class Config {
    @Deprecated
    public static Config fromJSON(String content) throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        ConfigSupport support = new ConfigSupport();
        return support.fromJSON(content, Config.class);
    }

    @Deprecated
    public static Config fromJSON(InputStream inputStream) throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        ConfigSupport support = new ConfigSupport();
        return support.fromJSON(inputStream, Config.class);
    }

    @Deprecated
    public static Config fromJSON(File file, ClassLoader classLoader) throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        ConfigSupport support = new ConfigSupport();
        return support.fromJSON(file, Config.class, classLoader);
    }

    @Deprecated
    public static Config fromJSON(File file) throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        return fromJSON(file, null);
    }

    @Deprecated
    public static Config fromJSON(URL url) throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        ConfigSupport support = new ConfigSupport();
        return support.fromJSON(url, Config.class);
    }

    @Deprecated
    public static Config fromJSON(Reader reader) throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        ConfigSupport support = new ConfigSupport();
        return support.fromJSON(reader, Config.class);
    }

    @Deprecated
    public String toJSON() throws IOException {
        log.error("JSON configuration is deprecated and will be removed in future!");
        ConfigSupport support = new ConfigSupport();
        return support.toJSON(this);
    }

    /**
     * Read config object stored in YAML format from String
     *
     * @param content of config
     * @return config
     * @throws IOException error
     */
    public static Config fromYAML(String content) throws IOException {
        ConfigSupport support = new ConfigSupport();
        return support.fromYAML(content, Config.class);
    }

    /**
     * Read config object stored in YAML format from InputStream
     *
     * @param inputStream object
     * @return config
     * @throws IOException error
     */
    public static Config fromYAML(InputStream inputStream) throws IOException {
        ConfigSupport support = new ConfigSupport();
        return support.fromYAML(inputStream, Config.class);
    }

    /**
     * Read config object stored in YAML format from File
     *
     * @param file object
     * @return config
     * @throws IOException error
     */
    public static Config fromYAML(File file) throws IOException {
        return fromYAML(file, null);
    }

    public static Config fromYAML(File file, ClassLoader classLoader) throws IOException {
        ConfigSupport support = new ConfigSupport();
        return support.fromYAML(file, Config.class, classLoader);
    }

    /**
     * Read config object stored in YAML format from URL
     *
     * @param url object
     * @return config
     * @throws IOException error
     */
    public static Config fromYAML(URL url) throws IOException {
        ConfigSupport support = new ConfigSupport();
        return support.fromYAML(url, Config.class);
    }

    /**
     * Read config object stored in YAML format from Reader
     *
     * @param reader object
     * @return config
     * @throws IOException error
     */
    public static Config fromYAML(Reader reader) throws IOException {
        ConfigSupport support = new ConfigSupport();
        return support.fromYAML(reader, Config.class);
    }

}

Test

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RedissonDemoApplication.class)
@Slf4j
public class RedissonDemoApplicationTests {


    @Autowired
    private RedissonProperties redissonProperties;


    @Test
    public void getYmlConfig() throws IOException {
        Config config = null;

        // 获取spring.redis.redisson下的纯文本配置
        if (redissonProperties.getConfig() != null) {
            try {
            	// 先尝试用yaml的格式解析配置
                config = Config.fromYAML(redissonProperties.getConfig());
            } catch (IOException e) {
                try {
                	// 解析报错后再尝试安装json
                    config = Config.fromJSON(redissonProperties.getConfig());
                } catch (IOException e1) {
                    e1.addSuppressed(e);
                    throw new IllegalArgumentException("Can't parse config", e1);
                }
            }
        }else {
            // TODO...
        }

        System.out.println(config.toJSON());
    }
}

Redisson配置加载 配置在application.yaml外(加载其他配置文件)
Redisson学习———学习redisson加载yaml配置信息_第1张图片

yaml

  • application
spring:
  redis:
    host: xxx.host
    port: 6379
    database: 0
    password: xxx.password
    timeout: 3600
    lettuce:
      pool:
        max-active: 3
        max-wait: 5
        max-idle: 3
        min-idle: 0
    client-name: redisson_demo
    redisson:
       file: classpath:redisson.yaml
  • redisson

以下redisson配置二选一即可,配置内容相同,展示格式不同而已

  • yaml文本
singleServerConfig:
  address: redis://xxx.host:6379
  connectionMinimumIdleSize: 11
  password: xxxpassword
  database: 0
threads: 0
nettyThreads: 0
codec: ! { }
transportMode: "NIO"
  • json文本
{
  "singleServerConfig": {
    "address": "redis://xxx.host:6379",
    "connectionMinimumIdleSize": 11,
    "password": "xxxpassword",
    "database": 0
  }
}

RedissonProperties

同上

Redisson config解析类

同上

Test

@RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest(classes = RedissonDemoApplication.class)
 @Slf4j
 public class RedissonDemoApplicationTests {

    @Autowired
    private RedissonProperties redissonProperties;
    @Autowired
    private ApplicationContext ctx;

    @Test
	public void getYmlJsonConfig() throws IOException {
        Config config = null;
    	// 根据配置属性的config字段的值来进行解析
        if (redissonProperties.getConfig() != null) {
            try {
            	// 以yaml格式进行解析
                config = Config.fromYAML(redissonProperties.getConfig());
            } catch (IOException e) {
                try {
				// 以json格式进行解析
                    config = Config.fromJSON(redissonProperties.getConfig());
                } catch (IOException e1) {
                    e1.addSuppressed(e);
                    throw new IllegalArgumentException("Can't parse config", e1);
                }
            }
    	// 根据配置属性的file字段判断是否为外置的配置文件
        } else if (redissonProperties.getFile() != null) {
            try {
            	// 以yaml格式进行解析
                InputStream is = getConfigStream();
                config = Config.fromYAML(is);
            } catch (IOException e) {
                // trying next format
                try {
				// 以json格式进行解析
                    InputStream is = getConfigStream();
                    config = Config.fromJSON(is);
                } catch (IOException e1) {
                    e1.addSuppressed(e);
                    throw new IllegalArgumentException("Can't parse config", e1);
                }
            }
        }
        System.out.println(config.toJSON());
    }
    private InputStream getConfigStream() throws IOException {
        Resource resource = ctx.getResource(redissonProperties.getFile());
        return resource.getInputStream();
    }
}

你可能感兴趣的:(redisson,技术使用总结,知识总结,学习,数据库)