增加druid监控的配置

1、POM.xml增加引入:

1 <dependency>
2     <groupId>com.alibaba</groupId>
3     <artifactId>druid-spring-boot-starter</artifactId>
4     <version>1.1.10</version>
5 </dependency>

2、Application.properties增加配置:

1 spring.datasource.filters: stat

3、添加配置类DruidConfig

 1 @Configuration
 2 public class DruidConfig {
 3 
 4     //将所有前缀为spring.datasource下的配置项都加载到DataSource中
 5     @ConfigurationProperties(prefix = "spring.datasource")
 6     @Bean
 7     public DataSource dataSource() {
 8         return new DruidDataSource();
 9     }
10     @Bean
11     public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
12         return new PropertySourcesPlaceholderConfigurer();
13     }
14     @Bean
15     public PlatformTransactionManager transactionManager() throws Exception {
16         DataSourceTransactionManager txManager = new DataSourceTransactionManager();
17         txManager.setDataSource(dataSource());
18         return txManager;
19     }
20     @Bean
21     public ServletRegistrationBean druidStatViewServlet() {
22         ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
23         Map<String, String> initParams = new HashMap<>();
24         // 可配的属性都在 StatViewServlet 和其父类下
25         initParams.put("loginUsername", "admin-druid");
26         initParams.put("loginPassword", "111111");
27         servletRegistrationBean.setInitParameters(initParams);
28         return servletRegistrationBean;
29     }
30     @Bean
31     public FilterRegistrationBean druidWebStatFilter() {
32         FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
33         Map<String, String> initParams = new HashMap<>();
34         initParams.put("exclusions", "*.js,*.css,/druid/*");
35         filterRegistrationBean.setInitParameters(initParams);
36         filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
37         return filterRegistrationBean;
38     }
39 }

4、在浏览器中访问:http://localhost:8080/druid/login.html,即可打开下面的页面,登录账号:admin-druid111111(这个账号密码是可以在DruidConfig.druidStatViewServlet方法中修改的)即可打开监控页面;

 

 

 

 

posted on 2019-12-02 14:12  小夏coding  阅读(542)  评论(0编辑  收藏  举报

导航