/ 6浏览

Spring Boot 集成 MyBatis、Redis、RabbitMQ 和 Elasticsearch 教程

1. 环境准备

确保你的开发环境已经安装了以下软件:

  • Java Development Kit (JDK) 1.8 或更高版本
  • Apache Maven 或 Gradle 作为构建工具
  • 一个文本编辑器或IDE(如IntelliJ IDEA或Eclipse)
  • Redis、RabbitMQ 和 Elasticsearch 服务器

2. 创建项目

使用 Spring Initializr 创建一个新项目,并添加以下依赖:

  • Spring Web
  • MyBatis-Spring-Boot-Starter
  • Spring Boot Redis Starter
  • Spring Boot RabbitMQ Starter
  • Spring Data Elasticsearch

3. 配置数据库

application.properties​ 文件中配置数据库连接信息:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
mybatis.configuration.default-statement-timeout=30

4. 配置 MyBatis

创建 mybatis-config.xml​ 文件在 src/main/resources​ 目录下,并配置 MyBatis 相关设置。

5. 配置 Redis

application.properties​ 文件中添加 Redis 配置:

spring.redis.host=localhost
spring.redis.port=6379

6. 配置 RabbitMQ

application.properties​ 文件中添加 RabbitMQ 配置:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

7. 配置 Elasticsearch

application.properties​ 文件中添加 Elasticsearch 配置:

spring.elasticsearch.rest.uris=http://localhost:9200

8. 实体类定义

定义一个简单的用户实体 User.java​:

package com.example.demo.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User {
    @Id
    private Long id;
    private String name;
    private String email;

    // Getters and Setters
}

9. MyBatis Mapper 接口

创建一个 MyBatis Mapper 接口 UserMapper.java​:

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> findAll();
}

10. Redis 配置

创建一个配置类 RedisConfig.java​ 来配置 Redis 序列化:

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

11. RabbitMQ 配置

创建一个配置类 RabbitMQConfig.java​ 来配置 RabbitMQ 交换机和队列:

package com.example.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue userQueue() {
        return new Queue("userQueue");
    }

    @Bean
    public DirectExchange userExchange() {
        return new DirectExchange("userExchange");
    }

    @Bean
    public Binding userBinding(Queue userQueue, DirectExchange userExchange) {
        return BindingBuilder.bind(userQueue).to(userExchange).with("userRoutingKey");
    }
}

12. Elasticsearch 配置

创建一个 Elasticsearch 配置类 ElasticsearchConfig.java​:

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.ElasticsearchRestClient;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public ClientConfiguration clientConfiguration() {
        return ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
    }

    @Override
    @Bean
    public ElasticsearchRestClient elasticsearchClient(ClientConfiguration clientConfiguration) {
        return new ElasticsearchRestClientBuilder(clientConfiguration).build();
    }

    @Override
    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient(clientConfiguration()));
    }
}

13. 业务逻辑实现

创建业务服务 UserService.java​ 来集成 MyBatis、Redis、RabbitMQ 和 Elasticsearch:

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    public List<User> findAll() {
        // 从 Redis 获取
        List<User> users = (List<User>) redisTemplate.opsForList().range("users", 0, -1);
        if (users == null || users.isEmpty()) {
            // 从数据库获取
            users = userMapper.findAll();
            // 存储到 Redis
            redisTemplate.opsForList().rightPushAll("users", users);
        }
        return users;
    }

    public void create(User user) {
        // 存储到数据库
        userMapper.insert(user);
        // 发送消息到 RabbitMQ
        rabbitTemplate.convertAndSend("userExchange", "userRoutingKey", user);
        // 索引到 Elasticsearch
        elasticsearchOperations.save(user);
    }
}

14. 控制器

创建一个控制器 UserController.java​ 来处理 HTTP 请求:

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> listAll() {
        return userService.findAll();
    }

    @PostMapping
    public void create(@RequestBody User user) {
        userService.create(user);
    }
}

15. 启动类

DemoApplication.java​ 是Spring Boot的启动类:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

16. 运行和测试

  • 运行 DemoApplication.java​ 类启动应用。
  • 使用Postman或任何HTTP客户端测试API。

17. 打包和部署

  • 使用Maven或Gradle命令打包应用。
  • 将打包好的JAR文件部署到服务器。

18. 进阶

  • 考虑使用Spring Security进行用户认证和授权。
  • 优化RabbitMQ的消息处理逻辑,确保消息的可靠传递。
  • 使用Elasticsearch进行全文搜索功能的实现。

这个教程提供了一个集成了MyBatis、Redis、RabbitMQ和Elasticsearch的Spring Boot应用的基础框架。你可以在此基础上扩展更多功能和特性。