seninel部署
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Sentinel DataSource Redis
  2. Sentinel DataSource Redis provides integration with Redis. The data source leverages Redis pub-sub feature to implement push model (listener).
  3. The data source uses [Lettuce](https://lettuce.io/) as the Redis client, which requires JDK 1.8 or later.
  4. > **NOTE**: Currently we do not support Redis Cluster now.
  5. ## Usage
  6. To use Sentinel DataSource Redis, you should add the following dependency:
  7. ```xml
  8. <dependency>
  9. <groupId>com.alibaba.csp</groupId>
  10. <artifactId>sentinel-datasource-redis</artifactId>
  11. <version>x.y.z</version>
  12. </dependency>
  13. ```
  14. Then you can create an `RedisDataSource` and register to rule managers.
  15. For instance:
  16. ```java
  17. ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<List<FlowRule>>(redisConnectionConfig, ruleKey, channel, flowConfigParser);
  18. FlowRuleManager.register2Property(redisDataSource.getProperty());
  19. ```
  20. - `redisConnectionConfig`: use `RedisConnectionConfig` class to build your Redis connection config
  21. - `ruleKey`: the rule persistence key of a Redis String
  22. - `channel`: the channel to subscribe
  23. You can also create multi data sources to subscribe for different rule type.
  24. Note that the data source first loads initial rules from a Redis String (provided `ruleKey`) during initialization.
  25. So for consistency, users should publish the value and save the value to the `ruleKey` simultaneously like this (using Redis transaction):
  26. ```
  27. MULTI
  28. SET ruleKey value
  29. PUBLISH channel value
  30. EXEC
  31. ```
  32. An example using Lettuce Redis client:
  33. ```java
  34. public <T> void pushRules(List<T> rules, Converter<List<T>, String> encoder) {
  35. StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
  36. RedisPubSubCommands<String, String> subCommands = connection.sync();
  37. String value = encoder.convert(rules);
  38. subCommands.multi();
  39. subCommands.set(ruleKey, value);
  40. subCommands.publish(ruleChannel, value);
  41. subCommands.exec();
  42. }
  43. ```
  44. Transaction can be handled in Redis Cluster when just using the same key.
  45. An example using Lettuce Redis Cluster client:
  46. ```java
  47. public <T> void pushRules(List<T> rules, Converter<List<T>, String> encoder) {
  48. RedisAdvancedClusterCommands<String, String> subCommands = client.connect().sync();
  49. int slot = SlotHash.getSlot(ruleKey);
  50. NodeSelection<String, String> nodes = subCommands.nodes((n)->n.hasSlot(slot));
  51. RedisCommands<String, String> commands = nodes.commands(0);
  52. String value = encoder.convert(rules);
  53. commands.multi();
  54. commands.set(ruleKey, value);
  55. commands.publish(channel, value);
  56. commands.exec();
  57. }
  58. ```
  59. ## How to build RedisConnectionConfig
  60. ### Build with Redis standalone mode
  61. ```java
  62. RedisConnectionConfig config = RedisConnectionConfig.builder()
  63. .withHost("localhost")
  64. .withPort(6379)
  65. .withPassword("pwd")
  66. .withDataBase(2)
  67. .build();
  68. ```
  69. ### Build with Redis Sentinel mode
  70. ```java
  71. RedisConnectionConfig config = RedisConnectionConfig.builder()
  72. .withRedisSentinel("redisSentinelServer1",5000)
  73. .withRedisSentinel("redisSentinelServer2",5001)
  74. .withRedisSentinelMasterId("redisSentinelMasterId").build();
  75. ```
  76. ### Build with Redis Cluster mode
  77. ```java
  78. RedisConnectionConfig config = RedisConnectionConfig.builder()
  79. .withRedisCluster("redisSentinelServer1",5000)
  80. .withRedisCluster("redisSentinelServer2",5001).build();
  81. ```