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

The following examples show how to use org.springframework.data.redis.connection.RedisStandaloneConfiguration. These examples are extracted from open source projects. 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 Project: SpringMVC-Project   Source File: RedisConfig.java    License: MIT License 7 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 Project: distributed-lock   Source File: EmbeddedRedis.java    License: 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 3
Source Project: spring-cloud-aws   Source File: RedisCacheFactory.java    License: 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 4
Source Project: ultimate-redis-boot   Source File: CacheConfig.java    License: 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 5
Source Project: ad   Source File: RedisConfiguration.java    License: 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 6
Source Project: hot-crawler   Source File: RedisConfig.java    License: 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 7
Source Project: alcor   Source File: RedisConfiguration.java    License: 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 8
Source Project: alcor   Source File: RedisConfiguration.java    License: 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 9
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: 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 10
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: 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 11
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: MIT License 5 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 12
Source Project: charging_pile_cloud   Source File: RedisConfig.java    License: 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 13
Source Project: unimall   Source File: RedisAutoConfig.java    License: 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 14
Source Project: unimall   Source File: RedisAutoConfig.java    License: 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 15
Source Project: unimall   Source File: RedisAutoConfig.java    License: 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 16
Source Project: unimall   Source File: RedisAutoConfig.java    License: 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;
}
 
Example 17
Source Project: unimall   Source File: RedisAutoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisStandaloneConfiguration lockRedisConfig() {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    config.setHostName(host);
    config.setPassword(RedisPassword.of(password));
    config.setPort(port);
    config.setDatabase(database);
    return config;
}
 
Example 18
Source Project: unimall   Source File: RedisAutoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisStandaloneConfiguration defaultRedisConfig() {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    config.setHostName(host);
    config.setPassword(RedisPassword.of(password));
    config.setPort(port);
    config.setDatabase(database);
    return config;
}
 
Example 19
Source Project: spring-boot-vue-admin   Source File: RedisConfig.java    License: 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 Project: soul   Source File: RateLimiterPluginDataHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Redis standalone configuration redis standalone configuration.
 *
 * @param rateLimiterConfig the rate limiter config
 * @return the redis standalone configuration
 */
protected final RedisStandaloneConfiguration redisStandaloneConfiguration(final RateLimiterConfig rateLimiterConfig) {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    String[] parts = StringUtils.split(rateLimiterConfig.getUrl(), ":");
    assert parts != null;
    config.setHostName(parts[0]);
    config.setPort(Integer.parseInt(parts[1]));
    if (rateLimiterConfig.getPassword() != null) {
        config.setPassword(RedisPassword.of(rateLimiterConfig.getPassword()));
    }
    config.setDatabase(rateLimiterConfig.getDatabase());
    return config;
}
 
Example 21
Source Project: spring-redis-websocket   Source File: HerokuRedisConfig.java    License: 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 22
Source Project: ueboot   Source File: RedisConfig.java    License: 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 23
Source Project: SpringBootBucket   Source File: RedisCacheConfig.java    License: 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 24
Source Project: FEBS-Security   Source File: RedisConfig.java    License: 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 25
Source Project: x7   Source File: L3RedisCacheConfig.java    License: 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 26
Source Project: x7   Source File: RedisCacheConfig.java    License: 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 27
Source Project: syncer   Source File: Redis.java    License: 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 28
Source Project: spring-boot-start-current   Source File: RedisConfig.java    License: 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 29
Source Project: jetcache   Source File: RedisSpringDataCacheTest.java    License: 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 30
Source Project: jeesupport   Source File: InstallRemote.java    License: 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( "连接失败" );
    }
}