master
parent
467425f01a
commit
b0cd824d30
@ -0,0 +1,47 @@
|
|||||||
|
package com.msb.mall.coupon.datasource;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
public class DataSourceConfiguration {
|
||||||
|
|
||||||
|
@Bean(name = "masterDataSource")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.master")
|
||||||
|
public DataSource masterDataSource() {
|
||||||
|
return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "slaveDataSource")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.slave")
|
||||||
|
public DataSource slaveDataSource() {
|
||||||
|
return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean("routingDataSource")
|
||||||
|
@Primary
|
||||||
|
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
|
||||||
|
ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource();
|
||||||
|
Map<Object, Object> targetDataResources = new HashMap<>();
|
||||||
|
DataSource masterDataSource = masterDataSource();
|
||||||
|
targetDataResources.put(DbContextHolder.DbType.MASTER, masterDataSource);
|
||||||
|
DataSource slaveDataSource = slaveDataSource();
|
||||||
|
targetDataResources.put(DbContextHolder.DbType.SLAVE, slaveDataSource);
|
||||||
|
proxy.setDefaultTargetDataSource(masterDataSource);//默认源
|
||||||
|
proxy.setTargetDataSources(targetDataResources);
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.msb.mall.coupon.datasource;
|
||||||
|
|
||||||
|
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(){
|
||||||
|
DbType dbType = contextHolder.get();
|
||||||
|
return dbType ==null? DbType.MASTER: dbType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearDbType(){
|
||||||
|
contextHolder.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.msb.mall.coupon.datasource;
|
||||||
|
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class ReadOnlyConnectionInterceptor implements Ordered {
|
||||||
|
|
||||||
|
@Around("@annotation(readOnlyConnection)")
|
||||||
|
public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection) throws Throwable {
|
||||||
|
try {
|
||||||
|
DbContextHolder.setDbType(DbContextHolder.DbType.SLAVE);
|
||||||
|
return proceedingJoinPoint.proceed();
|
||||||
|
} finally {
|
||||||
|
DbContextHolder.clearDbType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.msb.mall.coupon.datasource;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public class ReadWriteSplitRoutingDataSource extends AbstractRoutingDataSource {
|
||||||
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object determineCurrentLookupKey() {
|
||||||
|
return DbContextHolder.getDbType();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.msb.mall.product.datasource;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
public class DataSourceConfiguration {
|
||||||
|
|
||||||
|
@Bean(name = "masterDataSource")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.master")
|
||||||
|
public DataSource masterDataSource() {
|
||||||
|
return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "slaveDataSource")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.slave")
|
||||||
|
public DataSource slaveDataSource() {
|
||||||
|
return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean("routingDataSource")
|
||||||
|
@Primary
|
||||||
|
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
|
||||||
|
ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource();
|
||||||
|
Map<Object, Object> targetDataResources = new HashMap<>();
|
||||||
|
DataSource masterDataSource = masterDataSource();
|
||||||
|
targetDataResources.put(DbContextHolder.DbType.MASTER, masterDataSource);
|
||||||
|
DataSource slaveDataSource = slaveDataSource();
|
||||||
|
targetDataResources.put(DbContextHolder.DbType.SLAVE, slaveDataSource);
|
||||||
|
proxy.setDefaultTargetDataSource(masterDataSource);//默认源
|
||||||
|
proxy.setTargetDataSources(targetDataResources);
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.msb.mall.product.datasource;
|
||||||
|
|
||||||
|
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(){
|
||||||
|
DbType dbType = contextHolder.get();
|
||||||
|
return dbType ==null? DbType.MASTER: dbType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearDbType(){
|
||||||
|
contextHolder.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.msb.mall.product.datasource;
|
||||||
|
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class ReadOnlyConnectionInterceptor implements Ordered {
|
||||||
|
|
||||||
|
@Around("@annotation(readOnlyConnection)")
|
||||||
|
public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection) throws Throwable {
|
||||||
|
try {
|
||||||
|
DbContextHolder.setDbType(DbContextHolder.DbType.SLAVE);
|
||||||
|
return proceedingJoinPoint.proceed();
|
||||||
|
} finally {
|
||||||
|
DbContextHolder.clearDbType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.msb.mall.product.datasource;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Dulingling on 2019/8/7
|
||||||
|
*/
|
||||||
|
public class ReadWriteSplitRoutingDataSource extends AbstractRoutingDataSource {
|
||||||
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object determineCurrentLookupKey() {
|
||||||
|
return DbContextHolder.getDbType();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.msb.mall.mallsearch.controller;
|
||||||
|
|
||||||
|
import com.msb.mall.commons.util.R;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("test")
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@GetMapping("all")
|
||||||
|
public R testApi(){
|
||||||
|
System.out.println("=====>testApi");
|
||||||
|
return R.ok().put("test","testApi");
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,19 @@
|
|||||||
spring:
|
spring:
|
||||||
cloud:
|
cloud:
|
||||||
|
sentinel:
|
||||||
|
transport:
|
||||||
|
dashboard: localhost:8080
|
||||||
|
port: 8719
|
||||||
|
eager: true
|
||||||
nacos:
|
nacos:
|
||||||
discovery:
|
discovery:
|
||||||
server-addr: 192.168.37.133:8848
|
server-addr: 192.168.37.133:8848
|
||||||
application:
|
application:
|
||||||
name: mall-search
|
name: mall-search
|
||||||
server:
|
server:
|
||||||
port: 8090
|
port: 8090
|
||||||
|
management:
|
||||||
|
endpoints:
|
||||||
|
web:
|
||||||
|
exposure:
|
||||||
|
include: '*'
|
Loading…
Reference in new issue