org.springframework.data.redis.connection.RedisStandaloneConfiguration Java Examples

The following examples show how to use org.springframework.data.redis.connection.RedisStandaloneConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisConfig.java    From SpringMVC-Project with MIT License 8 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(hostName);
    redisStandaloneConfiguration.setPort(port);

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxWaitMillis(maxWaitMillis);
    poolConfig.setMaxTotal(maxTotal);

    LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
            .commandTimeout(Duration.ofSeconds(10))
            .shutdownTimeout(Duration.ZERO)
            .poolConfig(poolConfig)
            .build();

    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, lettucePoolingClientConfiguration);
    lettuceConnectionFactory.setShareNativeConnection(false);

    return lettuceConnectionFactory;
}
 
Example #2
Source File: RedisCacheFactory.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Override
protected RedisConnectionFactory createConnectionClient(String hostName, int port) {
	RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
	configuration.setHostName(hostName);
	configuration.setPort(port);
	if (JEDIS_AVAILABLE) {
		return new JedisConnectionFactory(configuration);
	}
	else if (LETTUCE_AVAILABLE) {
		return new LettuceConnectionFactory(configuration);
	}
	else {
		throw new IllegalArgumentException("No Jedis or lettuce client on classpath. "
				+ "Please add one of the implementation to your classpath");
	}
}
 
Example #3
Source File: EmbeddedRedis.java    From distributed-lock with MIT License 6 votes vote down vote up
@PostConstruct
public void start() throws IOException {
  // find free port
  final var serverSocket = new ServerSocket(0);
  final var port = serverSocket.getLocalPort();
  serverSocket.close();

  server = RedisServer.builder().setting("bind 127.0.0.1").port(port).build(); // bind to ignore windows firewall popup each time the server starts
  server.start();

  final var connectionFactory = new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", port));
  connectionFactory.setDatabase(0);
  connectionFactory.afterPropertiesSet();

  // set the property so that RedisAutoConfiguration picks up the right port
  System.setProperty("spring.redis.port", String.valueOf(port));
}
 
Example #4
Source File: RedisConfig.java    From charging_pile_cloud with MIT License 6 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory2() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_2);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example #5
Source File: Redis.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LettuceConnectionFactory getConnectionFactory() {
  if (clusterConnection.valid()) {
    return new LettuceConnectionFactory(clusterConnection.getConfig());
  }
  if (connection.valid()) {
    RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration(
        connection.getAddress(),
        connection.getPort());
    if (!connection.noPassword()) {
      standaloneConfig.setPassword(RedisPassword.of(connection.getPassword()));
    }
    return new LettuceConnectionFactory(standaloneConfig);
  }
  throw new InvalidConfigException("No valid connection configured");
}
 
Example #6
Source File: HerokuRedisConfig.java    From spring-redis-websocket with Apache License 2.0 5 votes vote down vote up
@SneakyThrows
private LettuceConnectionFactory lettuceConnectionFactory() {
	String redisUrl = System.getenv("REDIS_URL");
	URI redistogoUri = new URI(redisUrl);
	RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redistogoUri.getHost(), redistogoUri.getPort());
	redisStandaloneConfiguration.setPassword(redistogoUri.getUserInfo().split(":", 2)[1]);
	return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
 
Example #7
Source File: RedisConfig.java    From ueboot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * spring boot 2.0后采用Lettuce作为redis调用客户端
 * 默认配置只支持单机模式,如需要其他模式,需要另外定义
 *
 * @return LettuceConnectionFactory
 */
@Bean
@ConditionalOnMissingBean(type = {"org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"})
public LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisProperties.getHost(),
            redisProperties.getPort());
    configuration.setDatabase(redisProperties.getDatabase());
    configuration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    return new LettuceConnectionFactory(configuration);
}
 
Example #8
Source File: RedisCacheConfig.java    From SpringBootBucket with MIT License 5 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisConf = new RedisStandaloneConfiguration();
    redisConf.setHostName(env.getProperty("spring.redis.host"));
    redisConf.setPort(Integer.parseInt(env.getProperty("spring.redis.port")));
    redisConf.setPassword(RedisPassword.of(env.getProperty("spring.redis.password")));
    return new LettuceConnectionFactory(redisConf);
}
 
Example #9
Source File: RedisConfig.java    From FEBS-Security with Apache License 2.0 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(host);
    redisStandaloneConfiguration.setPort(port);
    redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
    redisStandaloneConfiguration.setDatabase(database);

    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
    jedisClientConfiguration.connectTimeout(Duration.ofMillis(timeout));
    jedisClientConfiguration.usePooling();
    return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
}
 
Example #10
Source File: L3RedisCacheConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public L3CacheStorageCustomizer l3CacheStorageCustomizer(){

    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(hostName, port);
    config.setPassword(password);
    config.setDatabase(database);

    LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config);
    connectionFactory.afterPropertiesSet();


    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
            Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);

    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(connectionFactory);
    stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
    stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());

    stringRedisTemplate.afterPropertiesSet();

    return () -> {

        L3CacheStorage l3CacheStorage = new DefaultL3CacheStorage();
        ((DefaultL3CacheStorage) l3CacheStorage).setStringRedisTemplate(stringRedisTemplate);

        return l3CacheStorage;
    };
}
 
Example #11
Source File: RedisCacheConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(hostName, port);
    LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config);
    connectionFactory.afterPropertiesSet();
    return connectionFactory;
}
 
Example #12
Source File: CacheConfig.java    From ultimate-redis-boot with MIT License 5 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory(CacheConfigurationProperties properties) {
    log.info("Redis (/Lettuce) configuration enabled. With cache timeout " + properties.getTimeoutSeconds() + " seconds.");

    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(properties.getRedisHost());
    redisStandaloneConfiguration.setPort(properties.getRedisPort());
    return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
 
Example #13
Source File: RedisConfig.java    From spring-boot-start-current with Apache License 2.0 5 votes vote down vote up
@Bean
  public RedisConnectionFactory redisConnectionFactory () {
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration(  );
standaloneConfig.setHostName( host );
standaloneConfig.setPort( port );
standaloneConfig.setPassword( RedisPassword.of( password ) );
standaloneConfig.setDatabase( database );
      return new JedisConnectionFactory(standaloneConfig);
  }
 
Example #14
Source File: RedisSpringDataCacheTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void lettuceTest() throws Exception {
    LettuceConnectionFactory connectionFactory =  new LettuceConnectionFactory(
            new RedisStandaloneConfiguration("127.0.0.1", 6379));
    connectionFactory.afterPropertiesSet();
    doTest(connectionFactory);
}
 
Example #15
Source File: InstallRemote.java    From jeesupport with MIT License 5 votes vote down vote up
@RemoteMethod
public void redis( String _host, int _port, String _password, int _db ) throws Exception{
    try{
        RedisConfig rc = new RedisConfig();

        RedisStandaloneConfiguration rsc = rc.redisStandaloneConfiguration();
        rsc.setHostName( _host );
        rsc.setPort( _port );
        rsc.setPassword( _password );
        rsc.setDatabase( _db );

        LettuceConnectionFactory lcf = ( LettuceConnectionFactory )
                rc.connectionFactory( rsc, rc.lettucePoolConfig( rc.clientOptions(), rc.clientResources() ) );
        lcf.setDatabase( _db );
        lcf.afterPropertiesSet();

        sDB.changeDatabase( lcf );

        Test t = new Test( RandomUtil.s_random_integer( 100 ) );
        sDB.insert( t );
        sDB.delete( t );

        installConfig.installRedis( _host, _port,_password, _db );
    }catch ( Exception e ){
        installConfig.installFailure( InstallConfig.InstallRedisSucc );
        throw new Exception( "连接失败" );
    }
}
 
Example #16
Source File: RedisConfig.java    From spring-boot-redis-guava-caffeine-cache with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public LettuceConnectionFactory factoryA(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfigA) {
    LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
            .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    return new LettuceConnectionFactory(redisConfigA, clientConfiguration);
}
 
Example #17
Source File: RedisConfig.java    From jeesupport with MIT License 5 votes vote down vote up
@Bean
public RedisConnectionFactory connectionFactory( RedisStandaloneConfiguration _rsc,
                                                 LettucePoolingClientConfiguration _lpcc ){
    int database = CommonConfig.getInteger( "spring.redis.database", 0 );

    LettuceConnectionFactory lcf = new LettuceConnectionFactory( _rsc, _lpcc );
    lcf.setShareNativeConnection( false );
    lcf.setDatabase( database );
    return lcf;
}
 
Example #18
Source File: RedisConfiguration.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public LettuceConnectionFactory lettuceConnectionFactory()
{
    LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder();
    if(Boolean.parseBoolean( (String) redisSslEnabled().getObject()))
    {
        builder.useSsl();
    }
    LettuceClientConfiguration clientConfiguration = builder.build();
    RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
    standaloneConfig.setHostName( (String) redisHost().getObject() );
    standaloneConfig.setPassword( (String) redisPassword().getObject() );
    standaloneConfig.setPort( Integer.parseInt( (String) redisPort().getObject() ) );
    return new LettuceConnectionFactory( standaloneConfig, clientConfiguration );
}
 
Example #19
Source File: RedisConfig.java    From spring-boot-vue-admin with Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public RedisConnectionFactory redisConnectionFactory(
    @Qualifier(value = "jedisPoolConfig") final JedisPoolConfig jedisPoolConfig) {
  // 方法上的 @ConfigurationProperties 不生效
  // 未知 bug,暂时这样手动设置
  // fixme
  // 单机版 jedis
  final RedisStandaloneConfiguration redisStandaloneConfiguration =
      new RedisStandaloneConfiguration(
          this.redisProperties.getHost(), this.redisProperties.getPort());
  redisStandaloneConfiguration.setDatabase(this.redisProperties.getDatabase());
  redisStandaloneConfiguration.setPassword(this.redisProperties.getPassword());

  // 获得默认的连接池构造器
  final JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
      (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)
          JedisClientConfiguration.builder();
  // 指定 jedisPoolConifig 来修改默认的连接池构造器
  jpcb.poolConfig(jedisPoolConfig);
  // 连接超时
  jpcb.and().connectTimeout(Duration.ofSeconds(10));
  // 通过构造器来构造 jedis 客户端配置
  final JedisClientConfiguration jedisClientConfiguration = jpcb.build();
  // 单机配置 + 客户端配置 = jedis 连接工厂
  return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
 
Example #20
Source File: RedisConfiguration.java    From ad with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisStandaloneConfiguration standaloneConfiguration() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(host);
    configuration.setPort(port);
    configuration.setDatabase(database);
    return configuration;
}
 
Example #21
Source File: RedisConfig.java    From hot-crawler with MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory()
{
    RedisProperties properties = redisProperties();
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(properties.getHost());
    configuration.setPort(properties.getPort());
    configuration.setPassword(properties.getPassword());
    configuration.setDatabase(properties.getDatabase());
    return new JedisConnectionFactory(configuration);
}
 
Example #22
Source File: RedisConfiguration.java    From alcor with Apache License 2.0 5 votes vote down vote up
@Bean
LettuceConnectionFactory lettuceConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(redisHostName);
    configuration.setPort(redisHostPort);
    return new LettuceConnectionFactory(configuration);
}
 
Example #23
Source File: RedisConfiguration.java    From alcor with Apache License 2.0 5 votes vote down vote up
@Bean
LettuceConnectionFactory lettuceConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(redisHostName);
    configuration.setPort(redisHostPort);
    return new LettuceConnectionFactory(configuration);
}
 
Example #24
Source File: RedisConfig.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
@Bean
@Primary
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_DEFAULT);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example #25
Source File: RedisConfig.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory1() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_1);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example #26
Source File: RedisConfig.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
@Bean
JedisConnectionFactory jedisConnectionFactory3() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
    redisStandaloneConfiguration.setHostName(redisProperties.getHost());
    redisStandaloneConfiguration.setPort(redisProperties.getPort());
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
    redisStandaloneConfiguration.setDatabase(RedisDbIndexConst.DB_INDEX_3);
    JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();

    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
            jedisClientConfiguration.usePooling().poolConfig(jedisPoolConfig()).build());
    return factory;
}
 
Example #27
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
/**** 缓存专用数据源 ****/
@Bean
public LettuceConnectionFactory defaultLettuceConnectionFactory(
        RedisStandaloneConfiguration defaultRedisConfig,GenericObjectPoolConfig defaultPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(defaultPoolConfig).build();
    return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
}
 
Example #28
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
/**** 用户SESSION专用数据源 ****/
@Bean
public LettuceConnectionFactory userLettuceConnectionFactory(
        RedisStandaloneConfiguration userRedisConfig,GenericObjectPoolConfig userPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(userPoolConfig).build();
    return new LettuceConnectionFactory(userRedisConfig, clientConfig);
}
 
Example #29
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
/**** 锁专用数据源 ****/
@Bean
public LettuceConnectionFactory lockLettuceConnectionFactory(
        RedisStandaloneConfiguration lockRedisConfig,GenericObjectPoolConfig lockPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(lockPoolConfig).build();
    return new LettuceConnectionFactory(lockRedisConfig, clientConfig);
}
 
Example #30
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisStandaloneConfiguration userRedisConfig() {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    config.setHostName(host);
    config.setPassword(RedisPassword.of(password));
    config.setPort(port);
    config.setDatabase(database);
    return config;
}