数据源切换druid
1.加坐标
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.8version>
dependency>
2.做配置
2.1 配置类中注入datasource
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource bruidSource(){
return new DruidDataSource();
}
2.2 yml文件中设置druid数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
yaml文件加密
1.添加坐标
com.github.ulisesbocchio
jasypt-spring-boot-starter
2.1.0
2.启动类添加注解
@SpringBootApplication
@EnableEncryptableProperties #加密注解
public class SpringBootDatasourceDruidApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDatasourceDruidApplication.class, args);
}
}
3.通过测试类生成密码
@Test
void show2() {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
config.setPassword("apesource");
standardPBEStringEncryptor.setConfig(config);
String name = "root";
String passwd = "123456";
String encryptname = standardPBEStringEncryptor.encrypt(name);
String encryptpwd = standardPBEStringEncryptor.encrypt(passwd);
System.out.println(encryptname);
System.out.println(encryptpwd);
}
4.配置yaml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT
username: ENC(uw12GfYMqt4JN6tvxxV85A==)
password: ENC(zoYTVRd3WBzt+bx6TYEGbQ==)
type: com.alibaba.druid.pool.DruidDataSource
jasypt:
encryptor:
password: apesource
druid监控平台
1.添加坐标
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.8version>
dependency>
2.编写yaml配置
spring:
datasource:
filters: stat,wall
3.编写配置类
3.1注入数据源
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource bruidSource(){
return new DruidDataSource();
}
3.2配置servlet制作监控页面
@Bean
public ServletRegistrationBean myServletRegistrationBean(){
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<StatViewServlet>();
StatViewServlet statViewServlet = new StatViewServlet();
registrationBean.setServlet(statViewServlet);
registrationBean.setUrlMappings(Arrays.asList("/druid/*"));
Map<String,String> maps = new HashMap<String,String>();
maps.put(StatViewServlet.PARAM_NAME_USERNAME,"admin");
maps.put(StatViewServlet.PARAM_NAME_PASSWORD,"123");
maps.put(StatViewServlet.PARAM_NAME_ALLOW,"");
maps.put(StatViewServlet.PARAM_NAME_DENY,"192.168.0.12");
registrationBean.setInitParameters(maps);
return registrationBean;
}
3.3配置filter编写过滤规则
@Bean
public FilterRegistrationBean myFilterRegistrationBean(){
FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
WebStatFilter webStatFilter = new WebStatFilter();
filterRegistrationBean.setFilter(webStatFilter);
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
Map<String, String> initPrams = new HashMap<>();
initPrams.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*");
filterRegistrationBean.setInitParameters(initPrams);
return filterRegistrationBean;
}
3.4 输出结果
```