Spring boot整合es,redis。启动项目报错的解决方案

Spring boot整合es,redis。启动项目报错的解决方案


Spring boo 整合es,redis项目启动报以下错误:

    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
    Error creating bean with name 'elasticsearchTemplate' defined in class path resource 
[org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfigura
tion.class]: 
Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0;
 nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'elasticsearchClient' defined in class path resource 
[org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration
.class]: Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: 
Factory method 'elasticsearchClient' threw exception; nested exception is 
java.lang.IllegalStateException: availableProcessors is already set to [2], rejecting [2]

通过百度。网上给的解决方案都是在main方法中添加:
System.setProperty("es.set.netty.runtime.available.processors", "false")
我在main方法中加入以后,还是启动报错。
最后在github上面发现了解决方案:

/**
 * @ Redis 配置类
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        System.out.println("--进入redis--");
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<Object>(Object.class);

        // 设置值(value)的序列化采用FastJsonRedisSerializer。
        redisTemplate.setValueSerializer(fastJsonRedisSerializer);
        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
        // 设置键(key)的序列化采用StringRedisSerializer。
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        //设置redis服务器的host或者ip地址
        redisStandaloneConfiguration.setHostName("120.0.0.1");
        //设置redis的端口号
        redisStandaloneConfiguration.setPort(6379);
        // 设置redis的密码 没有设置可以不用
        redisStandaloneConfiguration.setPassword("xxxxx");
        // 获取第一个redis服务
        redisStandaloneConfiguration.setDatabase(0);

        JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return factory;
    }

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(2);
        return jedisPoolConfig;
    }

}

添加配置类之后,项目可以正常的启动了。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值