郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.gkhy.safePlatform.config.database;
 
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.jta.JtaTransactionManager;
 
import javax.sql.DataSource;
import javax.sql.XADataSource;
import javax.transaction.UserTransaction;
import java.util.Properties;
 
/**
 * Druid配置
 *
 *
 */
@Component
@Configuration
public class DruidConfig {
 
 
    @Autowired
    private DataCourceProperties dataCourceProperties;
 
    @Bean(name = "dsAccount")
    @Primary
    @Autowired
    public DataSource dsAccount(Environment env) {
        final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        Properties prop = build(env, "spring.datasource.account.");
        mysqlXADataSource.setUrl(dataCourceProperties.getAccount().getUrl());
        mysqlXADataSource.setUser(dataCourceProperties.getAccount().getUsername());
        mysqlXADataSource.setPassword(dataCourceProperties.getAccount().getPassword());
        final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setXaDataSource(mysqlXADataSource);
        ds.setUniqueResourceName("account");
        ds.setPoolSize(dataCourceProperties.getAccount().getInitialSize());
        ds.setMinPoolSize(dataCourceProperties.getAccount().getMinIdle());
        ds.setMaxPoolSize(dataCourceProperties.getAccount().getMaxActive());
        ds.setXaProperties(prop);
        ds.setMaxIdleTime(3600);
        ds.setMaxLifetime(3000);
        ds.setTestQuery(dataCourceProperties.getAccount().getValidationQuery());
        return ds;
 
    }
 
 
    @Autowired
    @Primary
    @Bean(name = "dsDoublePrevent")
    public AtomikosDataSourceBean dsDoublePrevent(Environment env) {
        final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(dataCourceProperties.getDoubleprevent().getUrl());
        mysqlXADataSource.setUser(dataCourceProperties.getDoubleprevent().getUsername());
        mysqlXADataSource.setPassword(dataCourceProperties.getDoubleprevent().getPassword());
        final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        Properties prop = build(env, "spring.datasource.doubleprevent.");
        ds.setXaDataSource(mysqlXADataSource);
        ds.setUniqueResourceName("doubleprevent");
        ds.setPoolSize(dataCourceProperties.getDoubleprevent().getInitialSize());
        ds.setMinPoolSize(dataCourceProperties.getDoubleprevent().getMinIdle());
        ds.setMaxPoolSize(dataCourceProperties.getDoubleprevent().getMaxActive());
        ds.setXaProperties(prop);
        ds.setMaxIdleTime(3600);
        ds.setMaxLifetime(3000);
        ds.setTestQuery(dataCourceProperties.getDoubleprevent().getValidationQuery());
        return ds;
    }
 
    @Autowired
    @Primary
    @Bean(name = "dsSafeCheck")
    public AtomikosDataSourceBean dsSafeCheck(Environment env) {
        final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(dataCourceProperties.getSafecheck().getUrl());
        mysqlXADataSource.setUser(dataCourceProperties.getSafecheck().getUsername());
        mysqlXADataSource.setPassword(dataCourceProperties.getSafecheck().getPassword());
        final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        Properties prop = build(env, "spring.datasource.safecheck.");
        ds.setXaDataSource(mysqlXADataSource);
        ds.setUniqueResourceName("safecheck");
        ds.setPoolSize(dataCourceProperties.getSafecheck().getInitialSize());
        ds.setMinPoolSize(dataCourceProperties.getSafecheck().getMinIdle());
        ds.setMaxPoolSize(dataCourceProperties.getSafecheck().getMaxActive());
        ds.setXaProperties(prop);
        ds.setMaxIdleTime(3600);
        ds.setMaxLifetime(3000);
        ds.setTestQuery(dataCourceProperties.getSafecheck().getValidationQuery());
        return ds;
    }
 
    @Autowired
    @Primary
    @Bean(name = "dsSpecialWork")
    public AtomikosDataSourceBean dsSpecialWork(Environment env) {
        final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(dataCourceProperties.getSpecialwork().getUrl());
        mysqlXADataSource.setUser(dataCourceProperties.getSpecialwork().getUsername());
        mysqlXADataSource.setPassword(dataCourceProperties.getSpecialwork().getPassword());
        final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        Properties prop = build(env, "spring.datasource.specialwork.");
        ds.setXaDataSource(mysqlXADataSource);
        ds.setUniqueResourceName("specialwork");
        ds.setPoolSize(dataCourceProperties.getSpecialwork().getInitialSize());
        ds.setMinPoolSize(dataCourceProperties.getSpecialwork().getMinIdle());
        ds.setMaxPoolSize(dataCourceProperties.getSpecialwork().getMaxActive());
        ds.setXaProperties(prop);
        ds.setMaxIdleTime(3600);
        ds.setMaxLifetime(3000);
        ds.setTestQuery(dataCourceProperties.getSpecialwork().getValidationQuery());
        return ds;
    }
 
    @Autowired
    @Primary
    @Bean(name = "dsRiskControl")
    public AtomikosDataSourceBean dsRiskControl(Environment env) {
        final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(dataCourceProperties.getRiskcontrol().getUrl());
        mysqlXADataSource.setUser(dataCourceProperties.getRiskcontrol().getUsername());
        mysqlXADataSource.setPassword(dataCourceProperties.getRiskcontrol().getPassword());
        final AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        Properties prop = build(env, "spring.datasource.riskcontrol.");
        ds.setXaDataSource(mysqlXADataSource);
        ds.setUniqueResourceName("riskcontrol");
        ds.setPoolSize(dataCourceProperties.getRiskcontrol().getInitialSize());
        ds.setMinPoolSize(dataCourceProperties.getRiskcontrol().getMinIdle());
        ds.setMaxPoolSize(dataCourceProperties.getRiskcontrol().getMaxActive());
        ds.setXaProperties(prop);
        ds.setMaxIdleTime(3600);
        ds.setMaxLifetime(3000);
        ds.setTestQuery(dataCourceProperties.getRiskcontrol().getValidationQuery());
        return ds;
    }
 
 
    /**
     * 注入事物管理器
     * @return
     */
    @Bean(name = "xatx")
    public JtaTransactionManager regTransactionManager () {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        UserTransaction userTransaction = new UserTransactionImp();
        return new JtaTransactionManager(userTransaction, userTransactionManager);
    }
 
 
    private Properties build(Environment env, String prefix) {
 
        Properties prop = new Properties();
        prop.put("url", env.getProperty(prefix + "url"));
        prop.put("username", env.getProperty(prefix + "username"));
        prop.put("password", env.getProperty(prefix + "password"));
        prop.put("driverClassName", env.getProperty(prefix + "driver-class-name", ""));
//        prop.put("initialSize", env.getProperty(prefix + "initialSize", Integer.class));
//        prop.put("maxActive", env.getProperty(prefix + "maxActive", Integer.class));
//        prop.put("minIdle", env.getProperty(prefix + "minIdle", Integer.class));
//        prop.put("maxWait", env.getProperty(prefix + "maxWait", Integer.class));
//        prop.put("poolPreparedStatements", env.getProperty(prefix + "poolPreparedStatements", Boolean.class));
//
//        prop.put("maxPoolPreparedStatementPerConnectionSize",
//                env.getProperty(prefix + "maxPoolPreparedStatementPerConnectionSize", Integer.class));
//
//        prop.put("maxPoolPreparedStatementPerConnectionSize",
//                env.getProperty(prefix + "maxPoolPreparedStatementPerConnectionSize", Integer.class));
//        prop.put("validationQuery", env.getProperty(prefix + "validationQuery"));
//        prop.put("validationQueryTimeout", env.getProperty(prefix + "validationQueryTimeout", Integer.class));
//        prop.put("testOnBorrow", env.getProperty(prefix + "testOnBorrow", Boolean.class));
//        prop.put("testOnReturn", env.getProperty(prefix + "testOnReturn", Boolean.class));
//        prop.put("testWhileIdle", env.getProperty(prefix + "testWhileIdle", Boolean.class));
//        prop.put("timeBetweenEvictionRunsMillis",
//                env.getProperty(prefix + "timeBetweenEvictionRunsMillis", Integer.class));
//        prop.put("minEvictableIdleTimeMillis", env.getProperty(prefix + "minEvictableIdleTimeMillis", Integer.class));
//        prop.put("filters", env.getProperty(prefix + "filters"));
 
        return prop;
    }
 
//    @Bean
//    public ServletRegistrationBean druidServlet() {
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//
//        //控制台管理用户,加入下面2行 进入druid后台就需要登录
//        //servletRegistrationBean.addInitParameter("loginUsername", "admin");
//        //servletRegistrationBean.addInitParameter("loginPassword", "admin");
//        return servletRegistrationBean;
//    }
 
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        filterRegistrationBean.setFilter(new WebStatFilter());
//        filterRegistrationBean.addUrlPatterns("/*");
//        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//        filterRegistrationBean.addInitParameter("profileEnable", "true");
//        return filterRegistrationBean;
//    }
 
//    @Bean
//    public StatFilter statFilter(){
//        StatFilter statFilter = new StatFilter();
//        statFilter.setLogSlowSql(true); //slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。
//        statFilter.setMergeSql(true); //SQL合并配置
//        statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000,也就是3秒。
//        return statFilter;
//    }
 
//    @Bean
//    public WallFilter wallFilter(){
//        WallFilter wallFilter = new WallFilter();
//        //允许执行多条SQL
//        WallConfig config = new WallConfig();
//        config.setMultiStatementAllow(true);
//        wallFilter.setConfig(config);
//        return wallFilter;
//    }
 
}