【Sharding Sphere、Spring Boot】Spring Boot整合Sharding Sphere Proxy(5.0.0)

一、Sharding Sphere基本介绍

官网链接推荐:跳转官网 | 5.0.0中文文档 | 快速入门

二、Spring Boot整合Sharding Sphere Proxy

1、安装Sharding Sphere Proxy环境

参考地址:【Docker、Sharding Sphere】Docker安装Sharding Sphere Proxy-5.0.0

2、相关pom.xml依赖

		
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.1version>
        dependency>

3、创建Mybatis映射相关文件

【Sharding Sphere、Spring Boot】Spring Boot整合Sharding Sphere Proxy(5.0.0)_第1张图片
注意: @TableName注解中得表名需和代理数据库中得一致
【Sharding Sphere、Spring Boot】Spring Boot整合Sharding Sphere Proxy(5.0.0)_第2张图片

4、application.yml配置

server:
  port: 7208
  servlet:
    context-path: /sharding_sphere/proxy

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:7009/sharding_proxy_db?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root

5、测试接口

package com.cyun.demo.sharding.sphere.proxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cyun.core.result.ResultVO;
import com.cyun.demo.sharding.sphere.proxy.entity.SysLog;
import com.cyun.demo.sharding.sphere.proxy.service.impl.SysLogServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author He PanFu
 * @date 2022-04-20 11:39:11
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private final SysLogServiceImpl sysLogService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @GetMapping(value = "/by_date")
    public ResultVO byDate() throws ParseException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.between("create_time", sdf.parse("2022-04-01 00:59:59"), sdf.parse("2022-04-28 00:59:59"));
        final List list = sysLogService.list(queryWrapper);
        return ResultVO.ok(list.size());
    }

    @GetMapping(value = "/add/{logType}")
    public ResultVO add(@PathVariable("logType") Integer logType) {
        SysLog sysLog = SysLog.builder()
                .id(IdWorker.getIdStr())
                .applicationName("sharding-sphere-server")
                .logType(logType)
                .operateType(2)
                .logContent("测试")
                .ip("127.0.0.1")
                .method("测试")
                .requestUrl("测试")
                .requestParam("测试")
                .requestType("get")
                .costTime(5L)
                .createBy("123")
                .createTime(new Date())
                .updateBy("123")
                .updateTime(new Date())
                .build();

        sysLogService.save(sysLog);
        return ResultVO.ok();
    }
}

6、测试

测试新增

请求url
在这里插入图片描述

实际数据库表数据
【Sharding Sphere、Spring Boot】Spring Boot整合Sharding Sphere Proxy(5.0.0)_第3张图片
代理数据库表数据
在这里插入图片描述

测试查询

请求url
在这里插入图片描述
结果
【Sharding Sphere、Spring Boot】Spring Boot整合Sharding Sphere Proxy(5.0.0)_第4张图片

你可能感兴趣的:(Sharding,Sphere,sharding,sphere,微服务,分布式,spring,cloud,spring,boot)