|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// A basic client for native Redis Cluster using brpc::RedisClusterChannel. |
| 19 | + |
| 20 | +#include <gflags/gflags.h> |
| 21 | +#include <bthread/countdown_event.h> |
| 22 | +#include <butil/logging.h> |
| 23 | +#include <brpc/controller.h> |
| 24 | +#include <brpc/redis.h> |
| 25 | +#include <brpc/redis_cluster.h> |
| 26 | + |
| 27 | +DEFINE_string(seeds, "127.0.0.1:7000,127.0.0.1:7001", |
| 28 | + "Comma-separated redis cluster seed endpoints"); |
| 29 | +DEFINE_string(key_prefix, "brpc_cluster_demo", "Prefix for demo keys"); |
| 30 | +DEFINE_int32(timeout_ms, 1000, "RPC timeout in milliseconds"); |
| 31 | +DEFINE_int32(rpc_max_retry, 1, "Max retries for a single sub RPC"); |
| 32 | +DEFINE_int32(max_redirect, 5, "Max MOVED/ASK redirect retries"); |
| 33 | +DEFINE_int32(refresh_interval_s, 30, "Periodic topology refresh interval"); |
| 34 | +DEFINE_int32(topology_refresh_timeout_ms, 1000, |
| 35 | + "Timeout of CLUSTER SLOTS/NODES request"); |
| 36 | +DEFINE_bool(disable_periodic_refresh, false, "Disable periodic topology refresh"); |
| 37 | + |
| 38 | +namespace { |
| 39 | + |
| 40 | +class Done : public google::protobuf::Closure { |
| 41 | +public: |
| 42 | + explicit Done(bthread::CountdownEvent* event) : _event(event) {} |
| 43 | + void Run() override { _event->signal(); } |
| 44 | + |
| 45 | +private: |
| 46 | + bthread::CountdownEvent* _event; |
| 47 | +}; |
| 48 | + |
| 49 | +int PrintResponse(const brpc::RedisResponse& response) { |
| 50 | + for (int i = 0; i < response.reply_size(); ++i) { |
| 51 | + const brpc::RedisReply& reply = response.reply(i); |
| 52 | + if (reply.is_error()) { |
| 53 | + LOG(ERROR) << "reply[" << i << "] error=" << reply.error_message(); |
| 54 | + return -1; |
| 55 | + } |
| 56 | + LOG(INFO) << "reply[" << i << "] " << reply; |
| 57 | + } |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace |
| 62 | + |
| 63 | +int main(int argc, char* argv[]) { |
| 64 | + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 65 | + |
| 66 | + brpc::RedisClusterChannelOptions options; |
| 67 | + options.max_redirect = FLAGS_max_redirect; |
| 68 | + options.refresh_interval_s = FLAGS_refresh_interval_s; |
| 69 | + options.enable_periodic_refresh = !FLAGS_disable_periodic_refresh; |
| 70 | + options.topology_refresh_timeout_ms = FLAGS_topology_refresh_timeout_ms; |
| 71 | + options.channel_options.timeout_ms = FLAGS_timeout_ms; |
| 72 | + options.channel_options.max_retry = FLAGS_rpc_max_retry; |
| 73 | + |
| 74 | + brpc::RedisClusterChannel channel; |
| 75 | + if (channel.Init(FLAGS_seeds, &options) != 0) { |
| 76 | + LOG(ERROR) << "Fail to init redis cluster channel, seeds=" << FLAGS_seeds; |
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + const std::string key1 = FLAGS_key_prefix + "_1"; |
| 81 | + const std::string key2 = FLAGS_key_prefix + "_2"; |
| 82 | + |
| 83 | + // Sync pipeline. |
| 84 | + brpc::RedisRequest request; |
| 85 | + brpc::RedisResponse response; |
| 86 | + brpc::Controller cntl; |
| 87 | + CHECK(request.AddCommand("set %s v1", key1.c_str())); |
| 88 | + CHECK(request.AddCommand("set %s v2", key2.c_str())); |
| 89 | + CHECK(request.AddCommand("mget %s %s", key1.c_str(), key2.c_str())); |
| 90 | + channel.CallMethod(NULL, &cntl, &request, &response, NULL); |
| 91 | + if (cntl.Failed()) { |
| 92 | + LOG(ERROR) << "Sync call failed: " << cntl.ErrorText(); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + if (PrintResponse(response) != 0) { |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + // Async single request. |
| 100 | + brpc::RedisRequest async_request; |
| 101 | + brpc::RedisResponse async_response; |
| 102 | + brpc::Controller async_cntl; |
| 103 | + CHECK(async_request.AddCommand("get %s", key1.c_str())); |
| 104 | + |
| 105 | + bthread::CountdownEvent event(1); |
| 106 | + Done done(&event); |
| 107 | + channel.CallMethod(NULL, &async_cntl, &async_request, &async_response, &done); |
| 108 | + event.wait(); |
| 109 | + if (async_cntl.Failed()) { |
| 110 | + LOG(ERROR) << "Async call failed: " << async_cntl.ErrorText(); |
| 111 | + return -1; |
| 112 | + } |
| 113 | + if (PrintResponse(async_response) != 0) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + LOG(INFO) << "Redis cluster demo finished"; |
| 118 | + return 0; |
| 119 | +} |
0 commit comments