Analysis of the JavaWeb project architecture-Redis distributed log queue, javawebredis

Source: Internet
Author: User

Analysis of the JavaWeb project architecture-Redis distributed log queue, javawebredis

Abstract:

The architecture, distributed, and log queue titles all look at the publisher, which is actually a log collection function, but a Redis is added in the middle for message queue. Why message queue? When factors such as the speed or stability of "production" and "consumption" are inconsistent in the system, a message queue is required as an abstraction layer to bridge the differences between the two parties.

The architecture, distributed, and log queue titles all look at the publisher, which is actually a log collection function, but a Redis is added in the middle for message queue.

Why message queue?

When factors such as the speed or stability of "production" and "consumption" are inconsistent in the system, a message queue is required as an abstraction layer to bridge the differences between the two parties.

For example, common emails and text messages in our system write these functions that do not require timely response to the queue, process requests asynchronously, and reduce the response time.

How to implement it?

Mature JMS message queue middleware Products are available in the market. However, based on the current project architecture and deployment, we use Redis for message queue.

Why Redis?

The list Data Structure in Redis has the feature of "dual-end queue" and redis has the capability of persistent data. Therefore, it is very safe and reliable to implement distributed queues in redis.

It is similar to the "Queue" in JMS, but its functions and reliability (transactional) are not strict with JMS. Redis's high performance and "convenient" distributed design (sharding) provide a good foundation for implementing "distributed queues.

Provider

The project uses the third-party redis plug-in spring-data-redis. If you do not know how to use it, Google or Baidu.

Redis. properties:

# Redis configuration center redis. host = 192.168.1.180redis.port = 6379redis. password = 123456redis. maxIdle = 100 redis. maxActive = 300 redis. maxWait = 1000 redis. testOnBorrow = true redis. timeout = 100000

Redis Configuration:

<! -- Redis configuration --> <bean id = "jedisPoolConfig" class = "redis. clients. jedis. jedisPoolConfig "/> <bean id =" jedisConnectionFactory "class =" org. springframework. data. redis. connection. jedis. jedisConnectionFactory "> <property name =" hostName "value =" $ {redis. host} "/> <property name =" port "value =" $ {redis. port} "/> <property name =" password "value =" $ {redis. password} "/> <property name =" timeout "value =" $ {redis. timeout} "/> <property name =" poolConfig "ref =" jedisPoolConfig "/> <property name =" usePool "value =" true "/> </bean> <bean id = "redisTemplate" class = "org. springframework. data. redis. core. stringRedisTemplate "> <property name =" connectionFactory "ref =" jedisConnectionFactory "/> </bean>

Slice log configuration (pseudo code ):

/*** System Log, cut surface processing class * Creator Xiao Peng 2012 * Creation Time: July 15, January 15, 2018 */@ Component @ Scope @ Aspectpublic class SysLogAspect {@ Autowired private RedisTemplate <String, String> redisTemplate; // annotation is based on swagger APIs. You can also define @ Pointcut ("@ annotation (io. swagger. annotations. apiOperation) ") public void logPointCut () {}@ Around (" logPointCut () ") public Object around (ProceedingJoinPoint point) throws Throwable {Object result = point. proceed (); // write the log message to the itstyle_log channel redisTemplate. convertAndSend ("itstyle_log", "log data, self-processing"); return result ;}}

Consumer

Redis Configuration:

<! -- Redis configuration --> <bean id = "jedisPoolConfig" class = "redis. clients. jedis. jedisPoolConfig "/> <bean id =" jedisConnectionFactory "class =" org. springframework. data. redis. connection. jedis. jedisConnectionFactory "> <property name =" hostName "value =" $ {redis. host} "/> <property name =" port "value =" $ {redis. port} "/> <property name =" password "value =" $ {redis. password} "/> <property name =" timeout "value =" $ {redis. time Out} "/> <property name =" poolConfig "ref =" jedisPoolConfig "/> <property name =" usePool "value =" true "/> </bean> <bean id = "redisTemplate" class = "org. springframework. data. redis. core. redisTemplate "p: connection-factory-ref =" jedisConnectionFactory "> <property name =" keySerializer "> <bean class =" org. springframework. data. redis. serializer. stringRedisSerializer "/> </property> <property name =" hashKeySeriali Zer "> <bean class =" org. springframework. data. redis. serializer. StringRedisSerializer "/> </property> </bean> <! -- Listener implementation class --> <bean id = "listener" class = "com. itstyle. market. common. listener. messageDelegateListenerImpl "/> <bean id =" stringRedisSerializer "class =" org. springframework. data. redis. serializer. stringRedisSerializer "/> <redis: listener-container connection-factory =" jedisConnectionFactory "> <! -- Topic indicates the listening channel, which is a regular match and actually the channel you want to subscribe to --> <redis: listener ref = "listener" serializer = "stringRedisSerializer" method = "handleLog" topic = "itstyle_log"/> </redis: listener-container>

Listener interface:

public interface MessageDelegateListener {  public void handleLog(Serializable message);}

Listener implementation:

Public class MessageDelegateListenerImpl implements MessageDelegateListener {@ Override public void handleLog (Serializable message) {if (message = null) {System. out. println ("null");} else {// process log data }}}

Q &

[Question 1] Why Redis?

As mentioned above, although there are many stable products on the market, such as Kafka, RabbitMQ, and RocketMQ that may come to mind. However, because the project uses Redis for Distributed caching, Redis is selected based on the principle of easy-to-use.

[Question 2] how to store log data?

In principle, it is not recommended to store data in relational databases. For example, MySql generates a large number of logs. We recommend that you store data in non-relational databases such as Elasticsearch.

[Question 3] How does one collect slice logs?

The Jar package related to spring-aspects needs to be introduced to the slice log, and the configuration requires Spring to adopt CGLIB proxy.

Open Source project source code (reference): https://gitee.com/52itstyle/spring-boot-mail

Summary

The above is the Redis distributed log queue of the JavaWeb project architecture introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.