亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • Mybatis-plus多條件篩選分頁的實(shí)現(xiàn)

    Mybatis-plus多條件篩選分頁的實(shí)現(xiàn)

    本文主要介紹了Mybatis-plus多條件篩選分頁,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring切入點(diǎn)表達(dá)式配置過程圖解

    Spring切入點(diǎn)表達(dá)式配置過程圖解

    這篇文章主要介紹了Spring切入點(diǎn)表達(dá)式配置過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot FreeWorker模板技術(shù)解析

    SpringBoot FreeWorker模板技術(shù)解析

    這篇文章主要介紹了SpringBoot FreeWorker模板技術(shù)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 詳解@ConditionalOnMissingBean注解的作用

    詳解@ConditionalOnMissingBean注解的作用

    這篇文章主要介紹了詳解@ConditionalOnMissingBean注解的作用,@ConditionalOnMissingBean,它是修飾bean的一個(gè)注解,主要實(shí)現(xiàn)的是,當(dāng)你的bean被注冊(cè)之后,如果而注冊(cè)相同類型的bean,就不會(huì)成功,它會(huì)保證你的bean只有一個(gè),需要的朋友可以參考下
    2023-10-10
  • springSecurity之如何添加自定義過濾器

    springSecurity之如何添加自定義過濾器

    這篇文章主要介紹了springSecurity之如何添加自定義過濾器的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java判斷字符串是否含有亂碼實(shí)例代碼

    Java判斷字符串是否含有亂碼實(shí)例代碼

    本文通過實(shí)例代碼給大家介紹了Java判斷字符串是否含有亂碼的方法,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-11-11
  • 實(shí)例分析java中重載與重寫的區(qū)別

    實(shí)例分析java中重載與重寫的區(qū)別

    這篇文章主要介紹了實(shí)例分析java中重載與重寫的區(qū)別,需要的朋友可以參考下
    2014-07-07
  • Java如何求交集、并集、差集

    Java如何求交集、并集、差集

    這篇文章主要介紹了Java如何求交集、并集、差集問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java使用JNDI連接數(shù)據(jù)庫的實(shí)現(xiàn)方法

    Java使用JNDI連接數(shù)據(jù)庫的實(shí)現(xiàn)方法

    本文主要介紹了Java使用JNDI連接數(shù)據(jù)庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制(踩坑經(jīng)驗(yàn))

    springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制(踩坑經(jīng)驗(yàn))

    這篇文章主要介紹了springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制,本文給大家分享小編實(shí)際開發(fā)中的一點(diǎn)踩坑經(jīng)驗(yàn),內(nèi)容簡單易懂,需要的朋友可以參考下
    2020-07-07

最新評(píng)論