SpringBoot构建Bean(RedisConfig + RestTemplateConfig)

摘要

本文章用于SpringBoot项目构建Bean对象
构建两种对象(RedisConfig + RestTemplateConfig)
使用 @Autowired 方式注入直接使用即可

1. 构建RedisConfig Bean对象

①依赖

<!--redis -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>
<!--redis锁-->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.13.6</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.10.1</version>
</dependency>        

②配置文件

#redis
spring.redis.database=15
spring.redis.host=xxx
spring.redis.port=xxx
spring.redis.password=xxx
spring.redis.pool.max-active=200
spring.redis.jedis.pool.max-wait= -1
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout = 10000

③代码

@Configuration
public class RedisConfig  {

    /**
     *
     * @param factory factory
     * @author liudz
     * @date 2021/1/6
     * @return 执行结果
     **/
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

2. 构建RestTemplateConfig Bean对象

代码

@Configuration
public class RestTemplateConfig {

    /**
     *
     * @param factory factory
     * @author liudz
     * @date 2021/1/6
     * @return 执行结果
     **/
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    /**
     *
     * @author liudz
     * @date 2021/1/6
     * @return 执行结果
     **/
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(Integer.parseInt("5000"));
        factory.setConnectTimeout(Integer.parseInt("15000"));
        return factory;
    }
}
Springboot可以通过Druid实现redis和mysql的读写分离,下面是一个示例: 1. 在pom.xml中添加依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> 2. 在application.yml中配置redis和mysql的读写分离: spring: datasource: druid: # Redis数据源 redis: url: jdbc:redis://127.0.0.1:6379/0 driver-class-name: com.alibaba.druid.pool.DruidDataSource username: root password: root # MySQL数据源 mysql: url: jdbc:mysql://127.0.0.1:3306/test driver-class-name: com.alibaba.druid.pool.DruidDataSource username: root password: root 3. 在Springboot启动类中添加@EnableDruid注解: @SpringBootApplication @EnableDruid public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 4. 配置Druid数据源: @Configuration public class DruidConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.druid.redis") public DataSource redisDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.druid.mysql") public DataSource mysqlDataSource() { return DruidDataSourceBuilder.create().build(); } } 5. 在Service层中实现读写分离: @Service public class UserService { @Autowired private DataSource redisDataSource; @Autowired private DataSource mysqlDataSource; public void saveUser() { // 使用MySQL数据源写入数据 DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MYSQL); // ... // 使用Redis数据源读取数据 DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.REDIS); // ... } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘大猫.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值