springboot+dubbo+druid(监控)

一.部署dubbo-admin后台(便于观察)
1.安装/启动zookeeper(略),注意规避8080端口(一些版本的zookeeper启动后会占用8080,网上有解决办法)
2.下载dubbo-admin(地址https://github.com/apache/incubator-dubbo-ops)
3.进入dubbo-admin下运行maven指令打包:mvn package -Dmaven.skip.test=true

dubbo-admin-war.PNG

4.将dubbo-admin-2.0.0.war丢入tomcat(D:\dev\apache-tomcat-8.0.46\webapps),启动后,在..\apache-tomcat-8.0.46\webapps中会生产dubbo-admin-2.0.0的文件夹
image.png

5.访问http://127.0.0.1:8082/dubbo-admin-2.0.0/, 账号/密码: root/root
image.png

二. 项目创建和环境搭建
1.https://www.jianshu.com/p/6361a670ebf7
2.https://www.jianshu.com/p/0837b48d1691

三.整合druid数据源(->mysql)
参考:https://www.cnblogs.com/hlhdidi/p/6350306.html
https://blog.csdn.net/qq_34531925/article/details/78904897 (Druid数据库配置详细介绍)
https://www.cnblogs.com/softidea/p/6071630.html
1.在boot-dubbo-provider模块下的pom.xml中添加依赖


org.springframework.boot
spring-boot-starter-jdbc


org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1


mysql
mysql-connector-java
runtime


com.alibaba
druid
1.1.10

2.Druid数据源配置

server:
  port: 8071
  context-path: /
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver
    platform: mysql
    initialSize: 5
    minIdle: 5
    maxActive: 20
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,log4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true
mybatis:
  mapper-locations: ['mapper/*.xml']

3.建立DruidConfiguration配置类,配置过滤信息(配置完,数据库连接池便实现)

@Configuration
@ConditionalOnClass(value = {MapperScannerConfigurer.class,     
DataSourceTransactionManager.class})
@EnableTransactionManagement(proxyTargetClass = true)
public class MysqlDBConfig {
  @Autowired
  @Qualifier("mysqlDB")
  private DataSource ds;
  @Bean(name = "mysqlDB")
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource dataSource() {
      return       
 DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
   }
 }

4.配置数据库监控
4.1 依赖

     
        javax.servlet
        javax.servlet-api
        4.0.1
    
    
        org.springframework.boot
        spring-boot-starter-tomcat
        2.0.1.RELEASE
    
    
        org.apache.tomcat.embed
        tomcat-embed-jasper
        9.0.6
    

4.2 在MysqlDBConfig中新增方法

import javax.sql.DataSource;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 
* @ClassName: OracleDBConfig  
* @Description: oracle数据源配置  
* @author liyang  
* @date 2018年5月4日  
*
 */
@Configuration
@ConditionalOnClass(value = {MapperScannerConfigurer.class, DataSourceTransactionManager.class})
@EnableTransactionManagement(proxyTargetClass = true)
public class MysqlDBConfig {

    @Autowired
    @Qualifier("mysqlDB")
    private DataSource ds;

    @Bean(name = "mysqlDB")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
    }

    /**
     * druid 监控
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServle(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //白名单:
//        servletRegistrationBean.addInitParameter("allow","192.168.1.218,127.0.0.1");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的即提示:Sorry, you are not permitted to view this page.
//        servletRegistrationBean.addInitParameter("deny","192.168.1.100");
        //登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername","druid");
        servletRegistrationBean.addInitParameter("loginPassword","12345678");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    /**
     * druid 监控
     * @return
     */
    @Bean
    public FilterRegistrationBean statFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

4.3 启动后访问:

  • http://127.0.0.1:8071/druid/index.html
  • 账号/密码,为4.2中配置: druid/12345678

码云地址: https://gitee.com/googelchrome/boot-dubbo

你可能感兴趣的:(springboot+dubbo+druid(监控))