Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect to the Redis server

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to connect to the Redis server". In the daily operation, I believe many people have doubts about how to connect to the Redis server. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to connect to the Redis server". Next, please follow the editor to study!

1. Overview of development and running environment

First of all, the following environmental requirements must be met:

Docker: Docker has become a necessary tool for new application development, which makes it extremely easy to build, share and deploy applications.

Docker Compose: we use Docker Compose to manage all services so that they can be easily extended.

Other requirements are met by the Docker image, we don't need to install anything else, we just need to write a simple Docker Compos configuration document-- docker-compose.yml:

Version: '3'services: ganache: image: trufflesuite/ganache-cli command:-m redis: image: redis:alpine ports:-"6379image 6379" command: redis-server-- appendonly yes volumes:-redis:/data zookeeper: image: wurstmeister/zookeeper ports:-"2181 Vera 2181" kafka: image: wurstmeister/kafka ports:-"9092 image 9092" environment: KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1 KAFKA_CREATE_TOPICS: "command:1:1 Address.created:1:1,transaction:1:1,errors:1:1 "KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181volumes: redis:

You can easily start the service by running docker-compose up-d, which automatically downloads the necessary images from the Docker center and starts them. Let's take a look at what services are available.

1.1 Ganache-cli

If we don't have access to the nodes of the ethernet block chain, our wallet service will be useless. We do not need to download the entire ethernet block chain during the development phase, so we just need to use the Ganache simulator. The advantage of using Ganache is that the development efficiency is high, because the block is very fast. However, in the production environment, you need to use node software such as Geth to access the Ethernet Fong main network.

1.2 Redis

We need a database to hold the addresses we created and monitor transactions related to those addresses. Redis is an excellent memory key / value database, which is very suitable for our application scenario.

In this tutorial, we will use the Redis database to hold the private keys we generated for the address, but we should use a more secure hardware facility on the production server to protect these private keys.

1.3 Kafka/Zookeeper

Apache Kafka plays a central role in the exchange architecture, receiving messages from all services and distributing them to nodes that subscribe to these messages.

For ethernet Wallet service, we will use the following topics to communicate:

Command

Address.created

Transaction

Errors

The Apache Kafka server can be extended independently, providing a distributed message processing cluster for our service.

2. Development language selection

Personally, I like Elixir very much because it can be used to write extremely reliable distributed applications, and the code is easy to understand and maintain. But considering the ecology of Ethernet Square, Elixir has no advantage.

The best choice for ethernet development is to use Node.js/Javascript. Because there are a lot of components that you can use directly. So our ethersquare wallet service finally decided to use Node.js for development.

3. Build the initial development environment

First run the npm init command to create the default node package:

~ / exchange-hubwiz/eth-wallet$ npm init

Then we can add some node dependency packages to be used by the wallet service and execute the following command:

~ / exhcange-hubwiz/eth-wallet$ npm install-- save web3 redis kafka-node ethereumjs-tx bluebird

The role of the first three dependency packages is easy to understand:

Web3: connect to Ganache or other etheric Fong nodes via websocket

Redis: connect to a Redis server to save or extract data

Kafka-node: access to Zookeeper, obtain Kafka access nodes, produce or consume Kafka messages

The last two dependency packages help make our code easier to understand and can take advantage of async/await 's asynchronous programming mode.

Next, we will use these node packages to connect to Redis, Ethernet Square, and Kafka servers.

4. Connect the server 4.1 connect to the Redis server

It's easy to connect to Redis, create a redis.js file, and then write the following code:

/ / load configurationconst config = require ('.. /.. / config') const redis = require ('redis') const bluebird = require (' bluebird') / / promisify the redis client using bluebirdbluebird.promisifyAll (redis.RedisClient.prototype); bluebird.promisifyAll (redis.Multi.prototype); / / create a new redis client and connect to the redis instanceconst client = redis.createClient (config.redis_port, config.redis_host) / / if an error occurs, print it to the consoleclient.on ('error', function (err) {console.error ("[REDIS] Error encountered", err)}) module.exports = client;4.2 to connect Ethernet Fong nodes

If you think it's easy to connect to Redis, you'll be surprised how easy it is to connect Ethernet Fong nodes with web3. Create an ethereum.js and write the following code:

Const config = require ('.. /.. / config') const Web3 = require ('web3') module.exports = new Web3 (config.uri) 4.3 Connect to the Kafka server

Kafka, you need to extract messages from the queue for consumption, or store production messages in the queue. So we also need to continue with the relevant configuration.

Create a new file, query.js, and write the following code:

Const kafka = require ('kafka-node') const config = require ('.. /.. / config') / / configure how the consumers should connect to the broker/servers// each consumer creates his own connecto to a brokerconst default_options = {host: config.kafka_zookeeper_uri, autoCommit: true, fromOffset: 'earliest',} module.exports.consumer = (group_id = "ethereum_wallet_manager_consumer", topics = [], opts = {}) = > {const options = Object.assign ({groupId: group_id}) Default_options, opts) const consumer = new kafka.ConsumerGroup (options, topics) return consumer} / / configure how the producer connects to the Apache Kafka broker// initiate the connection to the kafka clientconst client = new kafka.Client (config.kafka_zookeeper_uri, config.kafka_client_id) module.exports.client = clientconst producer = new kafka.Producer (client) / / add a listener to the ready eventasync function on_ready (cb) {producer.on ('ready') Cb)} / / define a method to send multiple messages to the given topic// this will return a promise that will resolve with the response from Kafka// messages are converted to JSON strings before they are added in the queueasync function send (topic, messages) {return new Promise ((resolve, reject) = > {/ / convert objects to JSON strings messages = messages.map (JSON.stringify) / / add the messages to the given topic producer.send ([{topic, messages}], function (err) Data) {if (err) return reject (err) resolve (data)})} / / expose only these methods to the rest of the application and abstract away// the implementation of the producer to easily change it latermodule.exports.on_ready = on_readymodule.exports.send = send5, create the wallet service of Ethernet Square

Now we are entering the development phase of the core features of the Ethernet Square wallet service.

5.1 create a new ethernet square account

Exchanges and payment gateways need to generate new addresses for customers so that users can recharge services or pay for products. Generating an unused ethernet address is a basic requirement for any virtual currency service, so let's see how to do it.

First, create a commands.js in which we subscribe to messages in the queue. It mainly includes the following steps:

Connect to the command topic and listen for new create_account commands

When a new create_account command is received, create a new key pair and store it in the password store

Account_created topics that generate account_created messages and send them to the queue

The code is as follows:

Const web3 = require (". / ethereum") const redis = require ('. / redis') const queue = require ('. / queue') / * * Listen to new commands from the queue * / async function listen_to_commands () {const queue_consumer = queue.consumer ('eth.wallet.manager.commands', [' command']) / / process messages queue_consumer.on ('message') Async function (topic_message) {try {const message = JSON.parse (topic_message.value) / / create the new address with some reply metadata to match the response to the request const resp = await create_address (message.meta) / / if successful then post the response to the queue if (resp) {await queue_producer.send ('address.created') [resp])} catch (err) {/ / in case something goes wrong catch the error and send it back in the 'errors' topic console.error (topic_message, err) queue_producer.send (' errors', [{type: 'command', request: topic_message, error_code: err.code, error_message: err.message Error_stack: err.stack}])}) return queue_consumer} / * Create a new ethereum address and return the address * / async function create_account (meta = {}) {/ / generate the address const account = await web3.eth.accounts.create () / / disable checksum when storing the address const address = account.address.toLowerCase () / / save the public address in Redis without any transactions received yet await redis.setAsync (`eth:address:public:$ {address} ` JSON.stringify ({}) / / Store the private key in a vault. / / For demo purposes we use the same Redis instance, but this should be changed in production await redis.setAsync (`eth:address:private:$ {address} `, account.privateKey) return Object.assign ({}, meta, {address: account.address})} module.exports.listen_to_commands = listen_to_commands5.2 to process new transactions

We haven't finished writing our wallets, so we should be notified when the address we create is recharged by the user. For this reason, the web3 client of Etay Fang provides a newBlockHeaders subscription mechanism. In addition, if our service goes down occasionally, the service will miss the blocks produced during the downtime, so we also need to check that the wallet has been synchronized to the latest blocks of the network.

Create the sync_blocks.js file and write the following code:

Const web3 = require ('. / ethereum') / * Sync blocks and start listening for new blocks * @ param {Number} current_block_number-The last block processed * @ param {Object} opts-A list of options with callbacks for events * / async function sync_blocks (current_block_number, opts) {/ / first sync the wallet to the latest block let latest_block_number = await web3.eth.getBlockNumber () let synced_block_number = await sync_to_block (current_block_number, latest_block_number) Opts) / / subscribe to new blocks web3.eth.subscribe ('newBlockHeaders', (error, result) = > error & & console.log (error)) .on ("data", async function (blockHeader) {return await process_block (blockHeader.number, opts)}) return synced_block_number} / / Load all data about the given block and call the callbacks if definedasync function process_block (block_hash_or_id Opts) {/ / load block information by id or hash const block = await web3.eth.getBlock (block_hash_or_id, true) / / call the onTransactions callback if defined opts.onTransactions? Opts.onTransactions (block.transactions): null; / / call the onBlock callback if defined opts.onBlock? Opts.onBlock (block_hash_or_id): null; return block} / / Traverse all unprocessed blocks between the current index and the lastest block numberasync function sync_to_block (index, latest, opts) {if (index > = latest) {return index;} await process_block (index + 1, opts) return await sync_to_block (index + 1, latest, opts)} module.exports = sync_blocks

In the above code, we start with the latest block processed before the wallet service and synchronize to the current latest block of the block chain. Once we synchronize to the latest block, we start subscribing to the new block event. For each chunk, we execute the following callback function to process the chunk header and the transaction list in the chunk:

OnTransactions

OnBlock

It usually includes the following processing steps:

Monitor the new block to get all transactions in the block

Filter out transactions that are not related to the wallet address

Send each related transaction to the queue

Pooling the funds on the address into secure storage

Update processed block number

The final code is as follows:

Const web3 = require ("web3") const redis = require ('. / redis') const queue = require ('. / queue') const sync_blocks = require ('. / sync_blocks') / * * Start syncing blocks and listen for new transactions on the blockchain * / async function start_syncing_blocks () {/ / start from the last block number processed or 0 (you can use the current block before deploying for the first time) let last_block_number = await redis.getAsync ('eth:last-block) ') last_block_number = last_block_number | | 0 / / start syncing blocks sync_blocks (last_block_number {/ / for every new block update the latest block value in redis onBlock: update_block_head, / / for new transactions check each transaction and see if it's new onTransactions: async (transactions) = > {for (let i in transactions) {await process_transaction (transaction [I])}})} / / save the lastest block on redisasync function update_block_head (head) {return await redis.setAsync ('eth:last-block') Head)} / / process a new transactionasync function process_transaction (transaction) {const address = transaction.to.toLowerCase () const amount_in_ether = web3.utils.fromWei (transaction.value) / / check if the receiving address has been generated by our wallet const watched_address = await redis.existsAsync (`eth:address:public:$ {address} `) if (watched_address! = = 1) {return false} / / then check if it's a new transaction that should be taken into account const Transaction_exists = await redis.existsAsync (`eth:address:public:$ {address} `) if (transaction_exists = 1) {return false} / / update the list of transactions for that address const data = await redis.getAsync (`eth:address:public:$ {address}`) let addr_data = JSON.parse (data) addr_ data [transaction.hash] = {value: amount_in_ether} await redis.setAsync (`eth:address:public:$ {address} `) JSON.stringify (addr_data)) await redis.setAsync (`eth:transaction:$ {transaction.hash} `, transaction) / / move funds to the cold wallet address / / const cold_txid = await move_to_cold_storage (address, amount_in_ether) / / send notification to the kafka server await queue_producer.send ('transaction', [{txid: transaction.hash, value: amount_in_ether, to: transaction.to, from: transaction.from / / cold_txid: cold_txid,}]) return true} module.exports = start_syncing_blocks so far The study on "how to connect to the Redis server" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report