{"id":1244,"date":"2020-09-17T15:50:22","date_gmt":"2020-09-17T07:50:22","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=1244"},"modified":"2020-09-17T15:51:26","modified_gmt":"2020-09-17T07:51:26","slug":"springboot%e7%9a%84%e5%a4%9a%e6%95%b0%e6%8d%ae%e6%ba%90","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=1244","title":{"rendered":"springboot\u7684\u591a\u6570\u636e\u6e90"},"content":{"rendered":"\n<p>public class DynamicDataSource extends AbstractRoutingDataSource<br>{<br>private Map&lt;Object, Object&gt; targetDataSources;\/\/\u5b9e\u73b0\u52a8\u6001\u6dfb\u52a0<\/p>\n\n\n\n<p>public DynamicDataSource(DataSource defaultTargetDataSource, Map&lt;Object, Object&gt; targetDataSources)<br>{<br>this.targetDataSources = targetDataSources;<br>super.setDefaultTargetDataSource(defaultTargetDataSource);<br>super.setTargetDataSources(targetDataSources);<br>super.afterPropertiesSet();<br>}<\/p>\n\n\n\n<p>@Override<br>protected Object determineCurrentLookupKey()<br>{<br>return DynamicDataSourceContextHolder.getDataSourceType();<br>}<\/p>\n\n\n\n<p>public Map&lt;Object, Object&gt; getTargetDataSources() {<br>return targetDataSources;<br>}<\/p>\n\n\n\n<p>}<br>1.\u641e\u4e00\u4e2a\u7c7b\u7ee7\u627fAbstractRoutingDataSource\uff0c\u628a\u8fd9\u4e2atargetDataSources\u66b4\u9732\u51fa\u6765\uff0c\u5728 filter\u91cc\u9762\u53ef\u4ee5\u5bf9\u5b83\u8fdb\u884c\u52a8\u6001\u6dfb\u52a0\u7684\u64cd\u4f5c<\/p>\n\n\n\n<p>@Configuration<br>public class DruidConfig {<br>@Bean<br>@ConfigurationProperties(&#8220;spring.datasource.druid.master&#8221;)<br>public DataSource masterDataSource(DruidProperty druidProperties)<br>{<br>DruidDataSource dataSource = DruidDataSourceBuilder.create().build();<br>return druidProperties.dataSource(dataSource);<br>}<\/p>\n\n\n\n<p>@Bean(name = &#8220;dynamicDataSource&#8221;)<br>@Primary<br>public DynamicDataSource dataSource(DataSource masterDataSource)<br>{<br>Map&lt;Object, Object&gt; targetDataSources = new HashMap&lt;&gt;();<br>targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);<br>return new DynamicDataSource(masterDataSource, targetDataSources);<br>}<br>}<br>2.\u641e\u4e00\u4e2aconfig,\u521d\u59cb\u5316\u4e00\u4e0b\u8fd9\u4e2adynamicDataSource<\/p>\n\n\n\n<p>\/\/\u5207\u6362\u5230\u65b0\u7684\u6570\u636e\u6e90<br>Map m = apps.get(0);<br>String driver = &#8220;com.mysql.jdbc.Driver&#8221;;<br>String host = (String) m.get(&#8220;f_dbhost&#8221;);<br>String user = (String) m.get(&#8220;f_dbusername&#8221;);<br>String dbname = (String) m.get(&#8220;f_dbname&#8221;);<br>String password = (String) m.get(&#8220;f_dbpassword&#8221;);<br>DruidDataSource newDataSource = druidProperty.dataSource(new DruidDataSource());<br>newDataSource.setDriverClassName(driver);<br>String url = &#8220;jdbc:mysql:\/\/&#8221;+host+&#8221;\/&#8221;+dbname+&#8221;?useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=true&amp;serverTimezone=GMT%2B8&#8243;;<br>newDataSource.setUrl(url);<br>newDataSource.setUsername(user);<br>newDataSource.setPassword(password);<\/p>\n\n\n\n<p>\/\/\u52a8\u6001\u6dfb\u52a0\u65b0\u7684\u79df\u6237<br>tgetTargetDataSources.put(domain, newDataSource);<br>dynamicDataSource.setTargetDataSources(tgetTargetDataSources);<br>dynamicDataSource.afterPropertiesSet();<\/p>\n\n\n\n<p>DynamicDataSourceContextHolder.setDataSourceType(domain);<\/p>\n\n\n\n<p>3.\u5199\u4e00\u4e2a\u62e6\u622a\u5668\uff0c\u6839\u636e\u60c5\u51b5\u5904\u7406\uff0c\u5b58\u5728\u7684\u79df\u6237\uff0c\u76f4\u63a5\u5207\u6362\uff0c\u65b0\u52a0\u7684\u79df\u6237\u52a8\u6001\u6dfb\u52a0\uff0c\u7136\u540e\u6267\u884cafterPropertiesSet\uff0c\u76f8\u5f53\u4e8e\u5237\u65b0\u6570\u636e\u6e90\u5217\u8868\u3002<\/p>\n\n\n\n<p>4.DynamicDataSourceContextHolder\u7684\u4ee3\u7801\u4e5f\u5b58\u4e00\u4e0b\u3002<br>public class DynamicDataSourceContextHolder<br>{<br>public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);<\/p>\n\n\n\n<p>\/**<br>* \u4f7f\u7528ThreadLocal\u7ef4\u62a4\u53d8\u91cf\uff0cThreadLocal\u4e3a\u6bcf\u4e2a\u4f7f\u7528\u8be5\u53d8\u91cf\u7684\u7ebf\u7a0b\u63d0\u4f9b\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c<br>* \u6240\u4ee5\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u6539\u53d8\u81ea\u5df1\u7684\u526f\u672c\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5176\u5b83\u7ebf\u7a0b\u6240\u5bf9\u5e94\u7684\u526f\u672c\u3002<br>*\/<br>private static final ThreadLocal&lt;String&gt; CONTEXT_HOLDER = new ThreadLocal&lt;&gt;();<\/p>\n\n\n\n<p>\/**<br>* \u8bbe\u7f6e\u6570\u636e\u6e90\u7684\u53d8\u91cf<br>*\/<br>public static void setDataSourceType(String dsType)<br>{<br>log.info(&#8220;\u5207\u6362\u5230{}\u6570\u636e\u6e90&#8221;, dsType);<br>CONTEXT_HOLDER.set(dsType);<br>}<\/p>\n\n\n\n<p>\/**<br>* \u83b7\u5f97\u6570\u636e\u6e90\u7684\u53d8\u91cf<br>*\/<br>public static String getDataSourceType()<br>{<br>return CONTEXT_HOLDER.get();<br>}<\/p>\n\n\n\n<p>\/**<br>* \u6e05\u7a7a\u6570\u636e\u6e90\u53d8\u91cf<br>*\/<br>public static void clearDataSourceType()<br>{<br>CONTEXT_HOLDER.remove();<br>}<br>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>public class DynamicDataSource extends AbstractRoutingD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[69,68],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1244"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1244"}],"version-history":[{"count":7,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1244\/revisions"}],"predecessor-version":[{"id":1251,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1244\/revisions\/1251"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}