This article describes the configuration process based on the springboot project to integrate spring Security, MySQL, Redis, RabbitMQ.
Introduce project dependencies
First, create a new gradle project using the IntelliJ editor, then change the build.gradle file to the following content and re-import the dependencies
So far, I have completed the basic configuration of a most basic Springboot + mysql project, which uses Restful style requests to handle User CRUD.
Configure
In some of our projects, there will be a lot of data. When the amount of data becomes large, the efficiency of our database will become a bottleneck of our project. This is unavoidable, so we can only change one way. To avoid the slow query of a large amount of data in the database.
Although there may be a large amount of data in the project, some data are used frequently or have high speed requirements, such as the data of a hot video, which will be requested by a large number of requests and require fast enough speed, while some data On the contrary, the former is called hot data, and the latter is called cold data. For hot data, we can put them in the cache to improve efficiency.
So we need a way to help us manage cached data, redis as a nosql database stored in memory can help us solve these problems well. Redis is single-threaded, but because it is an in-memory database, the read and write speed is very fast, and because it is single-threaded, it avoids the consumption caused by mechanisms such as process switching and locking caused by multi-threading; redis also has a snapshot mechanism to help us protect our data in the event of a sudden power outage.
Although redis is very powerful, it is very simple to integrate into our spring project.
Install redis
Like other databases, first you need to install redis on your system and run it
The value of the key needs to correspond to the incoming parameters. For example, the key of the first request is the id attribute in the user, which needs to be written as #user.id, which cannot be written as #myUser.id (cacheNames.id) or #my_user.id (tableName.id). Once you fill in the wrong, you will find that your request reported 500 but the database was written successfully.
The annotations used by CRUD are different. In an environment that supports Spring Cache, for methods that use the @Cacheable annotation, Spring will check whether there is a cache element with the same key in the Cache before each execution. If there is, the method will not be executed. Instead, get the result directly from the cache and return it, otherwise it will be executed and the return result will be stored in the specified cache. @CachePut can also declare a method that supports caching. Unlike @Cacheable, the method using @CachePut annotation does not check whether there are previously executed results in the cache before execution, but executes the method every time and stores the execution results in the form of Attribute - Value Pair in the specified cache. @CacheEvict is used to annotate methods or classes that need to clear cache elements. When the tag is on a class, it means that the execution of all methods in it will trigger the cache clearing operation.
There are two ways to get the configuration here. The first is to add the @ConfigurationProperties (prefix = “spring.rabbitmq”) annotation directly above the class, so the properties in the class with the same name as those in the configuration file will be automatically matched; The second way is to add the @Value annotation directly to the property, but this way needs to be fully specified.
@Autowired private RabbitConstants rabbitConstants; @Bean public CachingConnectionFactory connectionFactory() { CachingConnectionFactoryconnectionFactory=newCachingConnectionFactory(); connectionFactory.setAddresses(rabbitConstants.getHost()); connectionFactory.setUsername(rabbitConstants.getUsername()); connectionFactory.setPassword(rabbitConstants.getPassword()); connectionFactory.setVirtualHost("/"); //The call needs to be displayed here before the message can be called back, which must be set. connectionFactory.setPublisherConfirms(true); return connectionFactory; }
@Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public RabbitTemplate rabbitTemplate() { RabbitTemplatetemplate=newRabbitTemplate(connectionFactory()); return template; }
@Bean public DirectExchange defaultExchange() { returnnewDirectExchange(EXCHANGE); }
@Bean public org.springframework.amqp.core.Queue queue() { returnnewQueue("spring-boot-queue", true); }
@Bean public Binding binding() { return BindingBuilder.bind(queue()).to(defaultExchange()).with(RabbitmqConfig.ROUTINGKEY); }
@Bean public SimpleMessageListenerContainer messageContainer() { SimpleMessageListenerContainercontainer=newSimpleMessageListenerContainer(connectionFactory()); container.setQueues(queue()); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(1); container.setConcurrentConsumers(1); //Set confirmation mode manual confirmation container.setAcknowledgeMode(AcknowledgeMode.MANUAL); container.setMessageListener(newChannelAwareMessageListener() { @Override publicvoidonMessage(Message message, Channel channel)throws Exception { byte[] body = message.getBody(); System.out.println("receive msg : " + newString(body)); Confirm that the message was successfully consumed channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } }); return container; }
/** * Constructor method injection */ @Autowired publicRabbitmqSendMessage(RabbitTemplate rabbitTemplate) { this.rabbitTemplate = rabbitTemplate; //This is to set the callback to be received and sent to the response, confirm () is explained below rabbitTemplate.setConfirmCallback(this); }