栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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依赖
		
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1
        
3、创建Mybatis映射相关文件


注意: @TableName注解中得表名需和代理数据库中得一致

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;


@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

实际数据库表数据

代理数据库表数据

测试查询

请求url

结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/840608.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号