背景
公司产品微服务架构下有十几个模块,几乎大部分模块都要连接redis。每次在客户那里部署应用,都要改十几遍配置,太痛苦了。当然可以用nacos配置中心的功能,配置公共参数。不过我是喜欢在应用级别上解决问题,因为并不是每个项目都会使用nacos,做个知识储备还是不错的。
公共配置文件位置
启动本地redis(windows版)
当前redis 没有数据
初始化redis
这里的初始化和正常把redis配置信息放到application.yml里的初始化是一样的。
package cn.com.soulfox.common.config; import lombok.extern.slf4j.slf4j; import org.springframework.boot.autoconfigure.condition.conditionalonclass; import org.springframework.boot.autoconfigure.condition.conditionalonmissingbean; import org.springframework.boot.autoconfigure.data.redis.redisproperties; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.core.redisoperations; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.serializer.jdkserializationredisserializer; import org.springframework.data.redis.serializer.stringredisserializer; /** * * @create 2024/4/11 10:48 */ @configuration @conditionalonclass(redisoperations.class) @enableconfigurationproperties(redisproperties.class) @slf4j public class redistemplateconfig { @bean @conditionalonmissingbean(name = "redistemplate") public redistemplate<string, object> getredistemplate(redisconnectionfactory factory){ log.info("开始初始化 redistemplate ------------------"); redistemplate<string, object> redistemplate = new redistemplate<>(); // key的序列化类型 redistemplate.setkeyserializer(new stringredisserializer()); redistemplate.sethashkeyserializer(new stringredisserializer()); redistemplate.sethashvalueserializer(new jdkserializationredisserializer()); redistemplate.setvalueserializer(new jdkserializationredisserializer()); redistemplate.setconnectionfactory(factory); log.info("初始化 redistemplate 结束------------------"); return redistemplate; } }
解析自定义sf-redis.yml
package cn.com.soulfox.business.config; import org.springframework.beans.factory.config.yamlpropertiesfactorybean; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.support.propertysourcesplaceholderconfigurer; import org.springframework.core.io.filesystemresource; import org.springframework.stereotype.component; /** * * @create 2024/6/26 16:41 */ @configuration public class commonconfig { @bean("common-config") public static propertysourcesplaceholderconfigurer properties() { propertysourcesplaceholderconfigurer configurer = new propertysourcesplaceholderconfigurer(); yamlpropertiesfactorybean redis = new yamlpropertiesfactorybean(); //文件路径写死的,真正做项目时,文件路径可以配置到application.yml文件 filesystemresource redisresource = new filesystemresource("../common-config/sf-redis.yml"); redis.setresources(redisresource); configurer.setpropertiesarray(redis.getobject()); //如果有多个配置文件,也是可以处理的。setpropertiesarray(properties... propertiesarray)方法的参数是个数组, //如下还可以同时处理文件sf-ports.yml,此时configurer.setpropertiesarray(redis.getobject());代码要注释掉 //yamlpropertiesfactorybean ports = new yamlpropertiesfactorybean(); // filesystemresource portsresource = new filesystemresource("../common-config/sf-ports.yml"); // ports.setresources(portsresource); //同时添加sf-redis.yml和sf-ports.yml的配置信息 // configurer.setpropertiesarray(redis.getobject(), ports.getobject()); return configurer; } }
应用启动类
注意一下,因为我已经搭建了完整的微服务,包括nacos,mybatis,feign等,所有启动类上注解比较多。如果只是单纯测试一下,引入springboot基础框架和redis依赖,写一个基础启动类就可以了。
package cn.com.soulfox.business; import org.springframework.beans.factory.config.yamlpropertiesfactorybean; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; import org.springframework.cloud.openfeign.enablefeignclients; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.support.propertysourcesplaceholderconfigurer; import org.springframework.core.io.filesystemresource; import org.springframework.core.io.pathresource; import org.springframework.core.io.resource; import org.springframework.transaction.annotation.enabletransactionmanagement; import tk.mybatis.spring.annotation.mapperscan; import java.io.file; @springbootapplication @enablediscoveryclient//nacos注册中心 @enablefeignclients(basepackages = {"cn.com.soulfox.common.feign.client"})//feign扫描 @mapperscan(basepackages={"cn.com.soulfox.*.mvc.mapper"})//mybatis mapper扫描 @enabletransactionmanagement//开启数据库事务 @componentscan("cn.com.soulfox") public class businessapplicationrun { public static void main(string[] args) { springapplication.run(businessapplicationrun.class, args); } }
启动一下应用看看redis是否初始化成功
测试一下是否可以正常使用
单元测试类
package cn.com.soulfox.common.config; import cn.com.soulfox.business.businessapplicationrun; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.boot.test.context.springboottest; import org.springframework.data.redis.core.redistemplate; import org.springframework.test.context.junit4.springrunner; /** * * @create 2024/6/26 16:52 */ @runwith(springrunner.class) @springboottest(classes = businessapplicationrun.class, webenvironment = springboottest.webenvironment.random_port) public class commonconfigtest { @autowired private redistemplate<string, object> redistemplate; //文件sf-redis.yml里属性的使用和applications.yml一样 @value("${spring.redis.host}") private string redishost; @test public void test(){ system.out.println("从文件取参数测试+++++++++++"); system.out.println("redishost: " + redishost); } }
测试sf-redis.yml属性使用
使用方法和配置在application.yml文件是一样,都是通过@value注解获取
测试结果
测试redis是否可以正常使用
测试写入数据,增加以下测试方法
@test public void testredissetvalue(){ this.redistemplate.opsforvalue().set("test", "test123"); }
测试结果
测试读取数据,增加以下测试方法
@test public void testredisgetvalue(){ object testvalue = this.redistemplate.opsforvalue().get("test"); system.out.println(testvalue); }
测试结果
总结一下
现在的微服务,大多使用nacos作为注册中心,同事nacos也能作为配置中心使用。公共配置一般放在nacos中,以上方法没有什么用处。但总有项目可能不会使用nacos,比如使用eureka,这时候以上方法就有用武之地。这个方法可以作为知识储备,了解一下总是有好处的 :–)
还有一点需要注意的,就是yml文件是在程序启动后解析的,所以文件里的配置信息,在application.yml里是不能通过${xxx.xxx}使用的。
以上就是springboot解析自定义yml文件的流程步骤的详细内容,更多关于springboot解析yml文件的资料请关注代码网其它相关文章!
发表评论