Synchronous call for data synchronization between MySQL and ES

This is the first method and the simplest method. When performing additions, deletions, modifications and queries on mysql, directly call the ES method after the operation to implement additions, deletions, modifications and queries.
It can be seen that this method has simple business logic and high real-time performance, but it will have strong business coupling, there is the risk of data loss due to double-write failure, poor performance, and strong code intrusion. It is not recommended, but it can be simply used as a common solution. take a look.
Next, let’s do it through the SpringBoot project

SpringBoot project

Introduce dependencies

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

Configuration file

Pay attention to modifying the mysql address

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://mysql地址/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
  elasticsearch:
    rest:
      uris: 101.200.128.156:9200

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml

Project structure

Insert image description here

Entity class

/**
 * mysql(user)与ES(user-demo)实体类
 */
@Data
@TableName(value = "user_t")
@Document(indexName = "user-demo")
public class User {
    
    
    @Id
    private String id;
    private String userName;
    private String address;
}

Mapper class

UserMapper

/**
 * mysql user实体Mapper类
 */
public interface UserMapper extends BaseMapper<User> {
    
    
}

UserEsMapper


/**
 * ES user-demo实体Mapper类
 */
@Repository
public interface UserEsMapper extends ElasticsearchRepository<User,String> {
    
    

}

Controller

Here you can see that after calling the mysql method, the method that does the same operation on ES is called synchronously.

/**
 * 同步调用方式实现mysql与ES数据同步Controller
 */
@RestController
@RequestMapping("/sycn")
public class DataController {
    
    

    @Resource
    private IDataService dataService;

    /**
     * 同步更新mysql和ES的user信息
     * @param user user实体
     */
    @GetMapping("/update")
    public void updateData(User user){
    
    
        dataService.updateMysqlData(user);
        dataService.esAddData(user);
    }

    /**
     * 查询user表信息
     * @return user信息集合
     */
    @GetMapping("/findData")
    public List<User> findAllData(){
    
    
        return dataService.findAllData();
    }

    /**
     * 同步根据id删除mysql和ES中user对应的数据信息
     * @param id 需要删除的信息id
     */
    @GetMapping("/delete")
    public void deleteDataById(String id){
    
    
        dataService.deleteDataById(id);
        dataService.esDeleteDataById(id);
    }

    /**
     * 同步新增mysql和ES的user数据
     * @param user user实体
     */
    @GetMapping("addData")
    public void addData(User user){
    
    
        dataService.addData(user);
        dataService.esAddData(user);
    }

    /**
     * 同步删除mysql和ES中所有user信息
     */
    @GetMapping("deleteAll")
    public void deleteAllData(){
    
    
        dataService.deleteAllData();
        dataService.esDeleteAllData();
    }
}

Service interface

/**
 * 同步调用方式实现mysql与ES数据同步Service
 */
public interface IDataService extends IService<User> {
    
    

    /**
     * 根据id更新mysql数据
     * @param user 需要更新数据的user对象
     */
    void updateMysqlData(User user);

    /**
     * 查询所有数据
     * @return user对象集合
     */
    List<User> findAllData();

    /**
     * mysql根据id删除信息
     * @param id 需要删除信息的id
     */
    void deleteDataById(String id);

    /**
     * mysql新增数据
     * @param user 需要新增数据的对象
     */
    void addData(User user);

    /**
     * ES根据ID删除数据
     * @param id 需要删除信息的id
     */
    void esDeleteDataById(String id);

    /**
     * ES新增/根据ID修改数据
     * @param user 需要新增/根据ID修改数据的对象
     */
    void esAddData (User user);

    /**
     * mysql删除user表所有数据
     */
    void deleteAllData();

    /**
     * es删除index=user-demoa中所有数据
     */
    void esDeleteAllData();
}

Service implementation class

/**
 * 同步调用方式实现mysql与ES数据同步Service实现类
 */
@Service
public class DataServiceImpl extends ServiceImpl<UserMapper, User> implements IDataService {
    
    

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserEsMapper userEsMapper;

    /**
     * 根据id更新mysql数据
     * @param user 需要更新数据的user对象
     */
    @Override
    public void updateMysqlData(User user) {
    
    
        userMapper.updateById(user);
    }

    /**
     * 查询所有数据
     * @return user对象集合
     */
    @Override
    public List<User> findAllData() {
    
    
        return userMapper.selectList(null);
    }

    /**
     * mysql根据id删除信息
     * @param id 需要删除信息的id
     */
    @Override
    public void deleteDataById(String id) {
    
    
        userMapper.deleteById(id);
    }

    /**
     * mysql新增数据
     * @param user 需要新增数据的对象
     */
    @Override
    public void addData(User user) {
    
    
        userMapper.insert(user);
    }

    /**
     * ES根据ID删除数据
     * @param id 需要删除信息的id
     */
    @Override
    public void esDeleteDataById(String id) {
    
    
        userEsMapper.deleteById(id);
    }

    /**
     * ES新增/根据ID修改数据
     * @param user 需要新增/根据ID修改数据的对象
     */
    @Override
    public void esAddData(User user) {
    
    
        userEsMapper.save(user);
    }

    /**
     * mysql删除user表所有数据
     */
    @Override
    public void deleteAllData() {
    
    
        userMapper.delete(null);
    }

    /**
     * es删除index=user-demoa中所有数据
     */
    @Override
    public void esDeleteAllData() {
    
    
        userEsMapper.deleteAll();
    }

}

It’s over here. Let’s take a look at how asynchronous calls are implemented in the next article.

Guess you like

Origin blog.csdn.net/m0_68681879/article/details/132836252