需求:用SpringBoot+Redis+RabbitMQ技术,PostMan发送一个请求,存储到Redis中,并且在控制台获取打印该信息。用PostMan发送一个请求将信息存储到RabbitMQ,并且在监听该队列打印出信息。
PostMan发送一个请求,存储到Redis中,并且在控制台获取打印该信息。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@PostMapping("/storeData")
public String storeData(@RequestBody String data){
// 将请求数据存储到Redis中,可以使用任意自定义的键名
redisTemplate.opsForValue().set("requestData",data);
return "Data stored successfully in Redis";
}
package com.example.redisdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@GetMapping("/getData")
public String getData() {
// 从Redis中获取数据
String data = redisTemplate.opsForValue().get("requestData");
System.out.println("Data from Redis: " + data);
return "Data from Redis: " + data;
}
}
server.port=8888
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.database=0
以上示例中,配置了Redis的主机地址、端口和数据库信息。如果 Redis 没有设置密码,spring.redis.password 可以为空。
PostMan发送一个请求,存储到RabbitMQ中,并且在监听该队列打印出信息。
server.port=8888
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MessageSender {
@Autowired
private RabbitTemplate rabbitTemplate;
@Bean
public Queue queue() {
return new Queue("ccc", false);
}
public void sendMessage(String message) {
rabbitTemplate.convertAndSend("ccc", message);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MessageListener {
@RabbitListener(queues = "ccc")
public void receiveMessage(String message) {
System.out.println("Message received: " + message);
}
}
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableRabbit
public class RabbitmqdemoApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqdemoApplication.class, args);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired
private MessageSender messageSender;
@PostMapping("/send")
@ResponseBody
public String sendMessage(@RequestParam("message") String message) {
messageSender.sendMessage(message);
return "Message sent successfully.";
}
}