redis-cli, kod veya Data Management (DMS) kullanarak bir Tair (Redis OSS-compatible) örneğine bağlanın.
Önkoşullar
Örnek için bir IP adresi izin listesi yapılandırılmış olmalıdır.
Örnek için bir parola belirtilmiş olmalıdır. Örnek için parola belirtilmemişse Parolayı değiştirme veya sıfırlama konusuna bakın.
Yordam
redis-cli
Bu örnekte, aynı sanal özel bulut (VPC) içindeki bir Tair (Redis OSS-compatible) örneğine bağlanmak için Linux Elastic Compute Service (ECS) örneği üzerindeki redis-cli kullanılır.
İnternet üzerinden bağlanmak için genel uç nokta başvurusu yapın.
-
ECS örneğinde oturum açın ve redis-cli'yi indirmek, derlemek ve yüklemek için aşağıdaki komutları çalıştırın:
sudo yum -y install gcc # Install GNU Compiler Collection (GCC). wget https://download.redis.io/releases/redis-7.2.0.tar.gz tar xzf redis-7.2.0.tar.gz cd redis-7.2.0&&makeBu örnekte redis-cli 7.2.0 kullanılmaktadır. Farklı bir sürüm yükleyebilirsiniz. Derleme işlemi 2 ila 3 dakika sürer.
-
Örneğe bağlanmak için aşağıdaki komutu çalıştırın:
src/redis-cli -h hostname -a password -p portParametreler:
hostname: örnek uç noktasıdır. Örnek ayrıntıları sayfasındaki Connection Information bölümünde VPC uç noktasını bulun; örneğin
r-8vbwds91ie1rdl****.redis.zhangbei.rds.aliyuncs.com. Daha fazla bilgi için Uç noktaları görüntüleme konusuna bakın.password: örnek parolasıdır.
port: bağlantı noktası numarasıdır. Varsayılan değer: 6379.
Örnek komut:
src/redis-cli -h r-8vbwds91ie1rdl****.redis.zhangbei.rds.aliyuncs.com -a TestPassword123 -p 6379 -
Veri yazın ve okuyun.
-
SET bar fookomutunu çalıştırın.Beklenen çıktı:
OK. -
GET barkomutunu çalıştırın.Beklenen çıktı:
"foo".
-
Kod
İnternet üzerinden bağlanmak için genel uç nokta başvurusu yapın.
Bu örnekte Jedis istemcisi dosyasına bakın. Diğer istemci türleri için Yaygın istemci türleriyle bağlantı kodu örnekleri konusuna bakın.
-
pom.xml dosyasına gerekli bağımlılıkları ekleyin.
<!-- Import spring-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!-- Since Spring Boot 2.0, Lettuce is the default client. Exclude lettuce when using Jedis. --> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!-- Import jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> -
Açıklamalara göre bağlantı parametrelerini değiştirin.
@Configuration public class RedisConfig { @Bean JedisConnectionFactory redisConnectionFactory() { // This example is used only to test connectivity. In production environments, we recommend that you place connection information in a configuration file and retrieve it using the @Value annotation. // The endpoint (hostName) and port (port) can be obtained from the Connection Information section of the instance details page. Select a VPC endpoint or public endpoint based on your client network environment. RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("r-8vbwds91ie1rdl****.redis.zhangbei.rds.aliyuncs.com", 6379); // Specify the password in the username:password format. For example, if the username is testaccount and the password is Rp829dlwa, enter testaccount:Rp829dlwa as the password. // If you forget your username or password, you can reset the password or create a new account by clicking Account Management in the left-side navigation pane of the instance details page. config.setPassword(RedisPassword.of("Username:Password")); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // Specify the maximum number of connections based on your business requirements. This value cannot exceed the maximum number of connections supported by the instance. jedisPoolConfig.setMaxTotal(30); // Specify the maximum number of idle connections based on your business requirements. This value cannot exceed the maximum number of connections supported by the instance. jedisPoolConfig.setMaxIdle(20); // Disable testOn[Borrow|Return] to prevent additional PING commands from being generated. jedisPoolConfig.setTestOnBorrow(false); jedisPoolConfig.setTestOnReturn(false); JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling().poolConfig( jedisPoolConfig).build(); return new JedisConnectionFactory(config, jedisClientConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } -
Bağlantıyı test edin.
@SpringBootTest public class RedisTest { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test void test() { try { redisTemplate.opsForValue().set("test_key", "hello world!"); System.out.println("Connection successful:"+redisTemplate.opsForValue().get("test_key")); } catch (Exception e) { e.printStackTrace(); System.out.println("An exception occurred during the connection attempt. Refer to the documentation " + "https://www.alibabacloud.com/help/en/redis/support/how-do-i-troubleshoot-connection-issues-in-apsaradb-for-redis?spm=a2c63.p38356.help-menu-26340.d_5_1_1_4.47ca2024nvxRlS" + "to troubleshoot network, whitelist, and account/password issues." + "You can also refer to the documentation based on the error message: https://www.alibabacloud.com/help/en/redis/support/common-errors-and-troubleshooting?spm=a2c63.p38356.help-menu-26340.d_5_0.77cc79fejApYJN"); } } }Bağlantı başarılı olursa çıktı şu şekildedir:
Connection successful: hello world!
DMS
Konsolda oturum açın ve Instances sayfasına gidin. Üst gezinme çubuğunda yönetmek istediğiniz örneğin bulunduğu bölgeyi seçin. Ardından örneği bulun ve örnek kimliğine tıklayın.
Örnek ayrıntıları sayfasının sağ üst köşesinde Log into Database seçeneğine tıklayın.
-
Log on to Database Instance iletişim kutusunda Access mode değerini password login olarak ayarlayın ve bir parola girin.
Bu mod varsayılan hesabı kullanır. Hesap ayrıntılarını konsoldaki Account Management sayfasında görüntüleyin.
Login seçeneğine tıklayın.
-
Veri yazın ve okuyun.
-
SQLConsole sayfasında
SET foo hellokomutunu girin ve Execute(F8) seçeneğine tıklayın.Beklenen çıktı:
OK. -
GET fookomutunu girin ve Execute(F8) seçeneğine tıklayın.Beklenen çıktı:
hello.
-
İlgili Konular
Daha fazla bilgi için aşağıdaki konulara bakın:
Özel bağlantı yöntemleri
Bir örneğe TLS (SSL) şifreli bağlantıları etkinleştirme: TLS şifreleme, aktarım bağlantısı güvenliğini artırır ve veri bütünlüğünü sağlar.
Doğrudan bağlantı modu kullanarak bir küme örneğine bağlanma: Örneğiniz doğrudan bağlantı modundaki bir küme örneği ise arka uç veri parçalarına doğrudan erişmek için proxy düğümlerini atlayarak özel uç noktayı kullanabilirsiniz. Bu küme örneklerine bağlantı, açık kaynaklı Redis kümelerine bağlantıya benzer. Proxy moduyla karşılaştırıldığında doğrudan bağlantı modu, isteklerin proxy düğümlerinden geçmesine gerek kalmadığından yanıt süresini azaltır.
Sentinel uyumlu modu kullanarak bağlanma: Tair (Redis OSS-compatible), Sentinel uyumlu modu sağlar. Bu modu etkinleştirdiğinizde örneklere yerel Redis Sentinel ile aynı şekilde istemciden bağlanılabilir.
Yaygın hatalar ve sorun giderme
|
Hata mesajı |
Neden ve çözüm |
|
|
İzin listesi yanlış yapılandırılmış.
Bağlantıyı doğrulamak için |
|
|
|
|
|
|
|
Geçersiz parola. Gerekli biçim hesap türüne göre değişir.
Not
|