最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring boot實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的方法

 更新時(shí)間:2017年01月19日 10:46:22   作者:Srggggg  
本篇文章主要介紹了Spring boot實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

背景

數(shù)據(jù)庫配置主從之后,如何在代碼層面實(shí)現(xiàn)讀寫分離?

用戶自定義設(shè)置數(shù)據(jù)庫路由

Spring boot提供了AbstractRoutingDataSource根據(jù)用戶定義的規(guī)則選擇當(dāng)前的數(shù)據(jù)庫,這樣我們可以在執(zhí)行查詢之前,設(shè)置讀取從庫,在執(zhí)行完成后,恢復(fù)到主庫。

實(shí)現(xiàn)可動(dòng)態(tài)路由的數(shù)據(jù)源,在每次數(shù)據(jù)庫查詢操作前執(zhí)行

ReadWriteSplitRoutingDataSource.java

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @author songrgg
 * @since 1.0
 */
public class ReadWriteSplitRoutingDataSource extends AbstractRoutingDataSource {
  @Override
  protected Object determineCurrentLookupKey() {
    return DbContextHolder.getDbType();
  }
}

線程私有路由配置,用于ReadWriteSplitRoutingDataSource動(dòng)態(tài)讀取配置

DbContextHolder.java

/**
 * @author songrgg
 * @since 1.0
 */
public class DbContextHolder {
  public enum DbType {
    MASTER,
    SLAVE
  }

  private static final ThreadLocal<DbType> contextHolder = new ThreadLocal<>();

  public static void setDbType(DbType dbType) {
    if(dbType == null){
      throw new NullPointerException();
    }
    contextHolder.set(dbType);
  }

  public static DbType getDbType() {
    return contextHolder.get() == null ? DbType.MASTER : contextHolder.get();
  }

  public static void clearDbType() {
    contextHolder.remove();
  }
}

AOP優(yōu)化代碼

利用AOP將設(shè)置數(shù)據(jù)庫的操作從代碼中抽離,這里的粒度控制在方法級(jí)別,所以利用注解的形式標(biāo)注這個(gè)方法涉及的數(shù)據(jù)庫事務(wù)只讀,走從庫。

只讀注解,用于標(biāo)注方法的數(shù)據(jù)庫操作只走從庫。

ReadOnlyConnection.java

package com.wallstreetcn.hatano.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Indicates the database operations is bound to the slave database.
 * AOP interceptor will set the database to the slave with this interface.
 * @author songrgg
 * @since 1.0
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadOnlyConnection {
}

ReadOnlyConnectionInterceptor.java

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * Intercept the database operations, bind database to read-only database as this annotation
 * is applied.
 * @author songrgg
 * @since 1.0
 */
@Aspect
@Component
public class ReadOnlyConnectionInterceptor implements Ordered {

  private static final Logger logger = LoggerFactory.getLogger(ReadOnlyConnectionInterceptor.class);

  @Around("@annotation(readOnlyConnection)")
  public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection) throws Throwable {
    try {
      logger.info("set database connection to read only");
      DbContextHolder.setDbType(DbContextHolder.DbType.SLAVE);
      Object result = proceedingJoinPoint.proceed();
      return result;
    } finally {
      DbContextHolder.clearDbType();
      logger.info("restore database connection");
    }
  }

  @Override
  public int getOrder() {
    return 0;
  }
}

UserService.java

@ReadOnlyConnection
public List<User> getUsers(Integer page, Integer limit) {
  return repository.findAll(new PageRequest(page, limit));
}

配置Druid數(shù)據(jù)庫連接池

build.gradle

compile("com.alibaba:druid:1.0.18")

groovy依賴注入

配置dataSource為可路由數(shù)據(jù)源

context.groovy

import com.alibaba.druid.pool.DruidDataSource
import DbContextHolder
import ReadWriteSplitRoutingDataSource

** SOME INITIALIZED CODE LOAD PROPERTIES **
def dataSourceMaster = new DruidDataSource()
dataSourceMaster.url = properties.get('datasource.master.url')
println("master set to " + dataSourceMaster.url)
dataSourceMaster.username = properties.get('datasource.master.username')
dataSourceMaster.password = properties.get('datasource.master.password')

def dataSourceSlave = new DruidDataSource()
dataSourceSlave.url = properties.get('datasource.slave.url')
println("slave set to " + dataSourceSlave.url)
dataSourceSlave.username = properties.get('datasource.slave.username')
dataSourceSlave.password = properties.get('datasource.slave.password') 
beans {
  dataSource(ReadWriteSplitRoutingDataSource) { bean ->
    targetDataSources = [
        (DbContextHolder.DbType.MASTER): dataSourceMaster,
        (DbContextHolder.DbType.SLAVE): dataSourceSlave
    ]
  }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java集合ArrayDeque類實(shí)例分析

    Java集合ArrayDeque類實(shí)例分析

    這篇文章主要介紹了Java集合ArrayDeque類實(shí)例分析的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 淺析SpringBoot中常見的底層注解

    淺析SpringBoot中常見的底層注解

    Spring?Boot?是一個(gè)用于創(chuàng)建獨(dú)立的、基于Spring框架的Java應(yīng)用程序的框架,它提供了許多注解,下面小編就來和大家介紹一些常見的底層注解吧
    2023-08-08
  • springboot+vue?若依項(xiàng)目在windows2008R2企業(yè)版部署流程分析

    springboot+vue?若依項(xiàng)目在windows2008R2企業(yè)版部署流程分析

    這篇文章主要介紹了springboot+vue?若依項(xiàng)目在windows2008R2企業(yè)版部署流程,本次使用jar包啟動(dòng)后端,故而準(zhǔn)備打包后的jar文件,需要的朋友可以參考下
    2022-12-12
  • 深入淺出MyBatis映射器

    深入淺出MyBatis映射器

    映射器是MyBatis最復(fù)雜也最重要的組件,也是基于MyBatis應(yīng)用程序開發(fā)中,本文主要介紹了深入淺出MyBatis映射器,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • java 實(shí)現(xiàn)文件夾的拷貝實(shí)例代碼

    java 實(shí)現(xiàn)文件夾的拷貝實(shí)例代碼

    這篇文章主要介紹了java 實(shí)現(xiàn)文件夾的拷貝實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Mybatis的Mapper代理對(duì)象生成及調(diào)用過程示例詳解

    Mybatis的Mapper代理對(duì)象生成及調(diào)用過程示例詳解

    這篇文章主要為大家介紹了Mybatis的Mapper代理對(duì)象生成及調(diào)用過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringBoot中condition注解的使用方式

    SpringBoot中condition注解的使用方式

    這篇文章主要介紹了SpringBoot中condition注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • JSON中fastjson、jackson、gson如何選擇

    JSON中fastjson、jackson、gson如何選擇

    在Java中,JSON的解析方式很多,例如fastjson(阿里)、Gson(谷歌)、jackjson等,本文主要介紹了JSON中fastjson、jackson、gson如何選擇,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-12-12
  • Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例

    Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例

    這篇文章主要介紹了Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • @Scheduled 如何讀取動(dòng)態(tài)配置文件

    @Scheduled 如何讀取動(dòng)態(tài)配置文件

    這篇文章主要介紹了@Scheduled 如何讀取動(dòng)態(tài)配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論

关岭| 嘉义县| 大邑县| 浏阳市| 梅河口市| 沭阳县| 类乌齐县| 南通市| 浦城县| 扎囊县| 大洼县| 满洲里市| 江陵县| 扶绥县| 邹城市| 馆陶县| 外汇| 泽州县| 满城县| 门源| 喀喇沁旗| 繁峙县| 平凉市| 龙陵县| 柘荣县| 南开区| 蒙自县| 三门峡市| 珲春市| 时尚| 民勤县| 全州县| 玛纳斯县| 南木林县| 将乐县| 苗栗市| 昌宁县| 吉木乃县| 常山县| 湟中县| 阜新市|