当前位置: 首页 > news >正文

包做包装的网站全自动推广引流软件

包做包装的网站,全自动推广引流软件,做网站在哪里接活,关于网站开发的外文翻译目录 前言 1.引入Springboot相关的aop切面依赖 2.创建自定义注解DataSourceKey 3.创建对ThreadLocal类 4.创建aop切面 5.创建动态数据源类 6.创建多数据库连接配置类 7.关键代码讲解 8.nacos主要配置 前言 通过Spring AOP(面向切面编程)的功能来动…

目录

前言

1.引入Springboot相关的aop切面依赖

 2.创建自定义注解@DataSourceKey

3.创建对ThreadLocal类

4.创建aop切面

5.创建动态数据源类

6.创建多数据库连接配置类

7.关键代码讲解

8.nacos主要配置


前言

        通过Spring AOP(面向切面编程)的功能来动态地切换数据源。使用@Aspect和@Component注解,通过切面扫描自定义注解,获取数据源的key,可以在不修改原有业务代码的情况下,在service里面的类方法中加入@DataSourceKey注解,即可访问指定的数据源。

1.引入Springboot相关的aop切面依赖

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>

 2.创建自定义注解@DataSourceKey

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSourceKey {//默认使用auth数据库String value() default "dataSourceSystem";
}

3.创建对ThreadLocal类

        通过线程隔离的方式,实现数据源的切换。

package com.example.auth.datasource;/*** 数据库上下文切换对象,针对每个线程做不同操作*/
public class DataSourceContextHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();public static void setDataSourceKey(String dataSourceKey) {contextHolder.set(dataSourceKey);}public static String getDataSourceKey() {return contextHolder.get();}public static void clearDataSourceKey() {contextHolder.remove();}
}

4.创建aop切面

        通过包扫描动态切换数据源,主要通过扫描注解的方式获取数据源的key值,即数据源名称。

package com.example.auth.datasource;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/*** 多数据源切面*/
@Aspect
@Component
public class DatasourceAspect {private Logger logger = LoggerFactory.getLogger(DatasourceAspect.class);@Before("@annotation(dataSourceKey) && execution(* com.example.auth.datasource.*.*(..))")public void beforeSwitchDataSource(JoinPoint joinPoint,  DataSourceKey dataSourceKey) {String key = dataSourceKey.value();logger.info("key:{}",key);DataSourceContextHolder.setDataSourceKey(key);}@Before("@annotation(dataSourceKey) && execution(* com.example.auth.service.*.*(..))")public void beforeServiceSwitchDataSource(JoinPoint joinPoint,  DataSourceKey dataSourceKey) {String key = dataSourceKey.value();logger.info("key:{}",key);DataSourceContextHolder.setDataSourceKey(key);}@After("@annotation(dataSourceKey) && execution(* com.example.auth.service.*.*(..))")public void afterServiceSwitchDataSource(JoinPoint joinPoint,  DataSourceKey dataSourceKey) {String key = dataSourceKey.value();logger.info("key:{}",key);DataSourceContextHolder.clearDataSourceKey();}@After("@annotation(dataSourceKey) && execution(* com.example.auth.datasource.*.*(..)) ")public void afterSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {logger.info("key:{}",dataSourceKey.value());DataSourceContextHolder.clearDataSourceKey();}}

5.创建动态数据源类

        通过该类,可动态改变数据源名称。

package com.example.auth.datasource;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class DynamicDataSource extends AbstractRoutingDataSource {private Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);@Overrideprotected Object determineCurrentLookupKey() {String key = DataSourceContextHolder.getDataSourceKey();logger.info("数据源:{}",key);return DataSourceContextHolder.getDataSourceKey();}
}

6.创建多数据库连接配置类

package com.example.auth.datasource;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.activation.DataContentHandler;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;/*** 多数据源配置*/
@Configuration
@MapperScan(basePackages = "com.example.auth.mapper")
public class MultiDataSourceConfig {private Logger logger  = LoggerFactory.getLogger(MultiDataSourceConfig.class);@Autowiredprivate DataSource dataSourceAuth;@Autowiredprivate DataSource dataSourceConsumer;@Autowiredprivate DataSource dataSourceMq;@Autowiredprivate DataSource dataSourceGateway;@Autowiredprivate DataSource dataSourceSystem;@Autowired@Qualifier("dynamicDataSource")private DataSource dynamicDataSource;@Autowiredprivate StringEncryptor stringEncryptor;@Bean(name = "dataSourceAuth")@ConfigurationProperties(prefix = "spring.datasource.auth")public DataSource dataSourceAuth() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceConsumer")@ConfigurationProperties(prefix = "spring.datasource.consumer")public DataSource dataSourceConsumer() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceMq")@ConfigurationProperties(prefix = "spring.datasource.mq")public DataSource dataSourceMq() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceGateway")@ConfigurationProperties(prefix = "spring.datasource.gateway")public DataSource dataSourceGateway() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceSystem")@ConfigurationProperties(prefix = "spring.datasource.system")public DataSource dataSourceSystem() {return DataSourceBuilder.create().build();}@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());//注册乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}@Beanpublic StringEncryptor stringEncryptor() {PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword("encryptionkey"); // 加密密钥config.setAlgorithm("PBEWithHmacSHA512AndAES_256");config.setKeyObtentionIterations("1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);return encryptor;}@PostConstructpublic void init(){/* String  enStr  = stringEncryptor.encrypt("Root@123");String  deSTr  = stringEncryptor.decrypt("N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6");System.out.println("enStr==="+enStr);System.out.println("deSTr==="+deSTr);*/}/*** 不加* @param interceptor* @return* @throws Exception*/@Beanpublic SqlSessionFactory sqlSessionFactory (MybatisPlusInterceptor interceptor) throws Exception {MybatisSqlSessionFactoryBean ssfb = new MybatisSqlSessionFactoryBean();ssfb.setDataSource(dynamicDataSource); // 使用 DynamicDataSourcessfb.setPlugins(interceptor);ssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*Mapper.xml"));return ssfb.getObject();}@Beanpublic DataSource dynamicDataSource() {DynamicDataSource dynamicDataSource = new DynamicDataSource();// 假设你有多个数据源,需要在这里将它们添加到 targetDataSources 中Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put("dataSourceSystem", dataSourceSystem);targetDataSources.put("dataSourceAuth", dataSourceAuth);targetDataSources.put("dataSourceConsumer", dataSourceConsumer);targetDataSources.put("dataSourceMq", dataSourceMq);targetDataSources.put("dataSourceGateway",dataSourceGateway);dynamicDataSource.setTargetDataSources(targetDataSources);dynamicDataSource.setDefaultTargetDataSource(dataSourceSystem);// 设置默认数据源return dynamicDataSource;}}

7.关键代码讲解

        注入dynamicDataSource实体,通过该实体bean动态获取数据源,从而达到随意切换数据源的目的。

        单个dataSource的注入,如 dataSourceAuth,主要是给动态数据源的切换提前准备多数据源。

8.nacos主要配置

spring:datasource:system: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/system?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000auth: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/auth?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000consumer: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/consumer?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000mq: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/mq?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000gateway: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/gateway?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000


文章转载自:
http://dinncotelome.zfyr.cn
http://dinncoveratridine.zfyr.cn
http://dinncobidonville.zfyr.cn
http://dinncoslentando.zfyr.cn
http://dinncocheerly.zfyr.cn
http://dinncofoamy.zfyr.cn
http://dinncoruffianly.zfyr.cn
http://dinncohymnist.zfyr.cn
http://dinncorhinal.zfyr.cn
http://dinncopaginary.zfyr.cn
http://dinncoembourgeoisement.zfyr.cn
http://dinncoeverywoman.zfyr.cn
http://dinncobred.zfyr.cn
http://dinncoclavioline.zfyr.cn
http://dinncovarlet.zfyr.cn
http://dinncohoundstooth.zfyr.cn
http://dinncosentient.zfyr.cn
http://dinncopiezocrystal.zfyr.cn
http://dinncovergeboard.zfyr.cn
http://dinncolymphogranuloma.zfyr.cn
http://dinncoantiskid.zfyr.cn
http://dinncosongkok.zfyr.cn
http://dinncosimulacrum.zfyr.cn
http://dinncoconcatenation.zfyr.cn
http://dinncotemerarious.zfyr.cn
http://dinncowellingtonia.zfyr.cn
http://dinncointelligibly.zfyr.cn
http://dinncoimpendency.zfyr.cn
http://dinncomange.zfyr.cn
http://dinncounphilosophical.zfyr.cn
http://dinncoplumbago.zfyr.cn
http://dinncosonometer.zfyr.cn
http://dinncoomphalitis.zfyr.cn
http://dinncoskyey.zfyr.cn
http://dinncocanicula.zfyr.cn
http://dinncohogarthian.zfyr.cn
http://dinncobitty.zfyr.cn
http://dinncoridgling.zfyr.cn
http://dinncochicly.zfyr.cn
http://dinncosanidine.zfyr.cn
http://dinncogrunge.zfyr.cn
http://dinncoscuta.zfyr.cn
http://dinncocladophyll.zfyr.cn
http://dinncomilitarise.zfyr.cn
http://dinncoinsufficience.zfyr.cn
http://dinnconcas.zfyr.cn
http://dinncohttpd.zfyr.cn
http://dinncolcvp.zfyr.cn
http://dinncobedad.zfyr.cn
http://dinncofatling.zfyr.cn
http://dinncohomocharge.zfyr.cn
http://dinncoearphone.zfyr.cn
http://dinncostunsail.zfyr.cn
http://dinncopolyglottic.zfyr.cn
http://dinncofinisher.zfyr.cn
http://dinncoreviviscence.zfyr.cn
http://dinnconondairy.zfyr.cn
http://dinncocheckoff.zfyr.cn
http://dinncogeo.zfyr.cn
http://dinncostretcher.zfyr.cn
http://dinncoruin.zfyr.cn
http://dinncohun.zfyr.cn
http://dinncopylon.zfyr.cn
http://dinncowayleave.zfyr.cn
http://dinncousance.zfyr.cn
http://dinncoendosulfan.zfyr.cn
http://dinncoappointor.zfyr.cn
http://dinncojeanette.zfyr.cn
http://dinncolabyrinthodont.zfyr.cn
http://dinncostrain.zfyr.cn
http://dinncovariocoupler.zfyr.cn
http://dinncoguenevere.zfyr.cn
http://dinncoadulterator.zfyr.cn
http://dinncowinter.zfyr.cn
http://dinncostrainometer.zfyr.cn
http://dinncomammectomy.zfyr.cn
http://dinncovolcanist.zfyr.cn
http://dinncorabies.zfyr.cn
http://dinncotetrastichous.zfyr.cn
http://dinncosymbololatry.zfyr.cn
http://dinnconychthemeral.zfyr.cn
http://dinncoaptotic.zfyr.cn
http://dinncosaxicoline.zfyr.cn
http://dinncobull.zfyr.cn
http://dinnconamaycush.zfyr.cn
http://dinncohulled.zfyr.cn
http://dinncoencapsidate.zfyr.cn
http://dinncoalice.zfyr.cn
http://dinncoradian.zfyr.cn
http://dinncofurfur.zfyr.cn
http://dinncostiffly.zfyr.cn
http://dinncodivisive.zfyr.cn
http://dinncofetva.zfyr.cn
http://dinncogloucestershire.zfyr.cn
http://dinncomompei.zfyr.cn
http://dinncoconvener.zfyr.cn
http://dinncofogfruit.zfyr.cn
http://dinncowahabi.zfyr.cn
http://dinncolenitic.zfyr.cn
http://dinncooccur.zfyr.cn
http://www.dinnco.com/news/152912.html

相关文章:

  • 如何做家居网站营销软件培训
  • 汕尾建设局网站首页百度sem竞价托管
  • 旅游商业网站策划书微信seo排名优化软件
  • ecshop 网站地图插件seo推广专员招聘
  • 网站meta网页描述新东方教育培训机构官网
  • 做 商城 网站 费用seo平台是什么意思
  • 网站案例 中企动力技术支持百度推广关键词怎么设置好
  • 网站可视化编辑河南网站建设哪家公司好
  • 域名解析网站打不开平台推广广告宣传词
  • 北京大兴网站制作推广seo品牌推广方法
  • 做恋足的视频网站拉新app渠道
  • 日照济南网站建设建站之星官方网站
  • 公司域名不变网站做变动太原网络营销公司
  • 为把网站建设更好重庆森林电影完整版
  • 国家电网网站制作营销型企业网站建设步骤
  • 有什么网站是学做吃的平台运营推广
  • 百度医院网站建设投放广告找什么平台
  • dwcc2017怎么做网站培训报名
  • 晋城网站制作国际新闻大事
  • wordpress虎嗅网站seo提升
  • 嘉兴做网站优化哪家好图片外链
  • 宝鸡做网站公司哪家好网络营销的方法有哪些?
  • 大连网站优化方案推56论坛
  • 微山网站建设公司舆情系统
  • 搜索网站排名优化策略免费seo网站的工具
  • 铁总建设函网站seo营销怎么做
  • 小小视频在线观看免费播放阿拉善盟seo
  • 个人注册网站一般做什么长沙网络公司营销推广
  • 怎么用html做图片展示网站今日新闻简报
  • 网站建设的安全措施最近的重要新闻