欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼

 更新時(shí)間:2018年04月12日 15:45:46   作者:dalaoyang  
這篇文章主要介紹了SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

druid是阿里巴巴開源的數(shù)據(jù)庫(kù)連接池,提供了優(yōu)秀的對(duì)數(shù)據(jù)庫(kù)操作的監(jiān)控功能,本文要講解一下springboot項(xiàng)目怎么集成druid。

本文在基于jpa的項(xiàng)目下開發(fā),首先在pom文件中額外加入druid依賴,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.dalaoyang</groupId>
 <artifactId>springboot_druid</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>springboot_druid</name>
 <description>springboot_druid</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.12.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.28</version>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

application.properties上半段和整合jpa一點(diǎn)沒變,下面加入了一些druid的配置,如果對(duì)druid的配置有什么不理解的,可以去網(wǎng)上查一下。(這篇文章我覺得寫的很好,傳送門)

#端口號(hào)
server.port=8888

##validate 加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)
##create 每次加載hibernate,重新創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的原因。
##create-drop  加載hibernate時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)
##update     加載hibernate自動(dòng)更新數(shù)據(jù)庫(kù)結(jié)構(gòu)
##validate 啟動(dòng)時(shí)驗(yàn)證表的結(jié)構(gòu),不會(huì)創(chuàng)建表
##none 啟動(dòng)時(shí)不做任何操作
spring.jpa.hibernate.ddl-auto=create

##控制臺(tái)打印sql
spring.jpa.show-sql=true

##數(shù)據(jù)庫(kù)配置
##數(shù)據(jù)庫(kù)地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##數(shù)據(jù)庫(kù)用戶名
spring.datasource.username=root
##數(shù)據(jù)庫(kù)密碼
spring.datasource.password=root
##數(shù)據(jù)庫(kù)驅(qū)動(dòng)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


#這里是不同的
#使用druid的話 需要多配置一個(gè)屬性spring.datasource.type
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 


# 連接池的配置信息
# 初始化大小,最小,最大
spring.datasource.initialSize=5 
spring.datasource.minIdle=5 
spring.datasource.maxActive=20 
# 配置獲取連接等待超時(shí)的時(shí)間
spring.datasource.maxWait=60000 
# 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000 
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000 
spring.datasource.validationQuery=SELECT 1 FROM DUAL 
spring.datasource.testWhileIdle=true 
spring.datasource.testOnBorrow=false 
spring.datasource.testOnReturn=false 
# 打開PSCache,并且指定每個(gè)連接上PSCache的大小
spring.datasource.poolPreparedStatements=true 
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
spring.datasource.filters=stat,wall,log4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄

然后在項(xiàng)目中加入DruidConfig,簡(jiǎn)單講解一下,這個(gè)配置文件主要是加載application.properties的配置,代碼如下:

package com.dalaoyang.config;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.config
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/12
 */
@Configuration
public class DruidConfig {
 private Logger logger = Logger.getLogger(this.getClass());

 @Value("${spring.datasource.url}")
 private String dbUrl;

 @Value("${spring.datasource.username}")
 private String username;

 @Value("${spring.datasource.password}")
 private String password;

 @Value("${spring.datasource.driver-class-name}")
 private String driverClassName;

 @Value("${spring.datasource.initialSize}")
 private int initialSize;

 @Value("${spring.datasource.minIdle}")
 private int minIdle;

 @Value("${spring.datasource.maxActive}")
 private int maxActive;

 @Value("${spring.datasource.maxWait}")
 private int maxWait;

 @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
 private int timeBetweenEvictionRunsMillis;

 @Value("${spring.datasource.minEvictableIdleTimeMillis}")
 private int minEvictableIdleTimeMillis;

 @Value("${spring.datasource.validationQuery}")
 private String validationQuery;

 @Value("${spring.datasource.testWhileIdle}")
 private boolean testWhileIdle;

 @Value("${spring.datasource.testOnBorrow}")
 private boolean testOnBorrow;

 @Value("${spring.datasource.testOnReturn}")
 private boolean testOnReturn;

 @Value("${spring.datasource.poolPreparedStatements}")
 private boolean poolPreparedStatements;

 @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
 private int maxPoolPreparedStatementPerConnectionSize;

 @Value("${spring.datasource.filters}")
 private String filters;

 @Value("{spring.datasource.connectionProperties}")
 private String connectionProperties;

 @Bean
 @Primary //主數(shù)據(jù)源
 public DataSource dataSource(){
  DruidDataSource datasource = new DruidDataSource();
  datasource.setUrl(this.dbUrl);
  datasource.setUsername(username);
  datasource.setPassword(password);
  datasource.setDriverClassName(driverClassName);
  //configuration
  datasource.setInitialSize(initialSize);
  datasource.setMinIdle(minIdle);
  datasource.setMaxActive(maxActive);
  datasource.setMaxWait(maxWait);
  datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  datasource.setValidationQuery(validationQuery);
  datasource.setTestWhileIdle(testWhileIdle);
  datasource.setTestOnBorrow(testOnBorrow);
  datasource.setTestOnReturn(testOnReturn);
  datasource.setPoolPreparedStatements(poolPreparedStatements);
  datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
  try {
   datasource.setFilters(filters);
  } catch (SQLException e) {
   logger.error("druid configuration Exception", e);
  }
  datasource.setConnectionProperties(connectionProperties);
  return datasource;
 }
}

然后創(chuàng)建DruidFilter,代碼如下:

package com.dalaoyang.filter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.filter
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/12
 */
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
  initParams={
    @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略資源
  }
)
public class DruidFilter extends WebStatFilter {
}

新建DruidServlet,在類上面加注解@WebServlet,其中配置了登錄druid監(jiān)控頁(yè)面的賬號(hào)密碼,白名單黑名單之類的配置,代碼如下:

package com.dalaoyang.servlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;
/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.servlet
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/12
 */
@WebServlet(urlPatterns="/druid/*",
  initParams={
    @WebInitParam(name="allow",value=""),// IP白名單(沒有配置或者為空,則允許所有訪問)
    @WebInitParam(name="deny",value=""),// IP黑名單 (deny優(yōu)先于allow)
    @WebInitParam(name="loginUsername",value="admin"),// 登錄druid管理頁(yè)面用戶名
    @WebInitParam(name="loginPassword",value="admin")// 登錄druid管理頁(yè)面密碼
  })
public class DruidServlet extends StatViewServlet {

}

然后在啟動(dòng)類加入注解@ServletComponentScan,讓項(xiàng)目掃描到servlet,代碼如下:

package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
// 啟動(dòng)類必須加入@ServletComponentScan注解,否則無法掃描到servlet
@ServletComponentScan
public class SpringbootDruidApplication {
 public static void main(String[] args) {
  SpringApplication.run(SpringbootDruidApplication.class, args);
 }
}

剩余的就是和整合jpa一樣的entity(實(shí)體類),repository(數(shù)據(jù)操作層),controller(測(cè)試使用的controller),直接展示代碼。

City

package com.dalaoyang.entity;
import javax.persistence.*;
/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.Entity
 * @email 397600342@qq.com
 * @date 2018/4/7
 */
@Entity
@Table(name="city")
public class City {
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 private int cityId;
 private String cityName;
 private String cityIntroduce;

 public City(int cityId, String cityName, String cityIntroduce) {
  this.cityId = cityId;
  this.cityName = cityName;
  this.cityIntroduce = cityIntroduce;
 }

 public City(String cityName, String cityIntroduce) {
  this.cityName = cityName;
  this.cityIntroduce = cityIntroduce;
 }

 public City() {
 }

 public int getCityId() {
  return cityId;
 }

 public void setCityId(int cityId) {
  this.cityId = cityId;
 }

 public String getCityName() {
  return cityName;
 }

 public void setCityName(String cityName) {
  this.cityName = cityName;
 }

 public String getCityIntroduce() {
  return cityIntroduce;
 }

 public void setCityIntroduce(String cityIntroduce) {
  this.cityIntroduce = cityIntroduce;
 }
}

CityRepository

package com.dalaoyang.repository;
import com.dalaoyang.entity.City;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.Repository
 * @email 397600342@qq.com
 * @date 2018/4/7
 */
public interface CityRepository extends JpaRepository<City,Integer> {
}

CityController

package com.dalaoyang.controller;
import com.dalaoyang.entity.City;
import com.dalaoyang.repository.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email 397600342@qq.com
 * @date 2018/4/7
 */
@RestController
public class CityController {
 @Autowired
 private CityRepository cityRepository;

 //http://localhost:8888/saveCity?cityName=北京&cityIntroduce=中國(guó)首都
 @GetMapping(value = "saveCity")
 public String saveCity(String cityName,String cityIntroduce){
  City city = new City(cityName,cityIntroduce);
  cityRepository.save(city);
  return "success";
 }

 //http://localhost:8888/deleteCity?cityId=2
 @GetMapping(value = "deleteCity")
 public String deleteCity(int cityId){
  cityRepository.delete(cityId);
  return "success";
 }

 //http://localhost:8888/updateCity?cityId=3&cityName=沈陽&cityIntroduce=遼寧省省會(huì)
 @GetMapping(value = "updateCity")
 public String updateCity(int cityId,String cityName,String cityIntroduce){
  City city = new City(cityId,cityName,cityIntroduce);
  cityRepository.save(city);
  return "success";
 }
 //http://localhost:8888/getCityById?cityId=3
 @GetMapping(value = "getCityById")
 public City getCityById(int cityId){
  City city = cityRepository.findOne(cityId);
  return city;
 }
}

然后啟動(dòng)項(xiàng)目,可以看到控制臺(tái)已經(jīng)創(chuàng)建了city表。

然后訪問http://localhost:8888/druid,可以看到如下圖:

輸入賬號(hào)密碼admin,admin,如下圖

然后這時(shí)我們可以訪問http://localhost:8888/saveCity?cityName=北京&cityIntroduce=中國(guó)首都

然后點(diǎn)擊導(dǎo)航上面的SQL監(jiān)控,如下圖,

從上圖可以看到啟動(dòng)項(xiàng)目創(chuàng)建表的sql已經(jīng)剛剛執(zhí)行的sql。到這里整合已經(jīng)完成了。

源碼下載 :https://gitee.com/dalaoyang/springboot_learn

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA項(xiàng)目如何取消git版本管控并添加svn版本控制

    IDEA項(xiàng)目如何取消git版本管控并添加svn版本控制

    在公司內(nèi)部服務(wù)器環(huán)境下,將代碼倉(cāng)庫(kù)從Gitee的Git遷移到SVN可以避免外部版本控制的風(fēng)險(xiǎn),遷移過程中,先刪除項(xiàng)目的.git文件夾,再通過Eclipse的設(shè)置界面刪除原Git配置并添加SVN配置,之后,將項(xiàng)目提交到SVN倉(cāng)庫(kù),確保使用ignore列表過濾不必要的文件
    2024-10-10
  • Spring boot多線程配置方法

    Spring boot多線程配置方法

    這篇文章主要為大家詳細(xì)介紹了Spring boot多線程配置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 聊一聊concurrenthashmap的size方法原理

    聊一聊concurrenthashmap的size方法原理

    這篇文章主要介紹了concurrenthashmap的size方法原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 使用idea2017搭建SSM框架(圖文步驟)

    使用idea2017搭建SSM框架(圖文步驟)

    這篇文章主要介紹了使用idea2017搭建SSM框架(圖文步驟),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler

    MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler

    這篇文章主要為大家介紹了MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • SparkSQL使用快速入門

    SparkSQL使用快速入門

    spark SQL是spark的一個(gè)模塊,主要用于進(jìn)行結(jié)構(gòu)化數(shù)據(jù)的處理。它提供的最核心的編程抽象就是DataFrame。這篇文章主要介紹了SparkSQL使用快速入門,需要的朋友可以參考下
    2021-08-08
  • JPA之多對(duì)多查詢死循環(huán)嵌套問題及解決方案

    JPA之多對(duì)多查詢死循環(huán)嵌套問題及解決方案

    這篇文章主要介紹了JPA之多對(duì)多查詢死循環(huán)嵌套問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 使用.NET Core3.0創(chuàng)建一個(gè)Windows服務(wù)的方法

    使用.NET Core3.0創(chuàng)建一個(gè)Windows服務(wù)的方法

    這篇文章主要介紹了使用.NET Core3.0創(chuàng)建一個(gè)Windows服務(wù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-04-04
  • Java?Spring的兩種事務(wù)你知道嗎

    Java?Spring的兩種事務(wù)你知道嗎

    這篇文章主要為大家詳細(xì)介紹了Java?Spring的兩種事務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • list集合去除重復(fù)對(duì)象的實(shí)現(xiàn)

    list集合去除重復(fù)對(duì)象的實(shí)現(xiàn)

    下面小編就為大家?guī)硪黄猯ist集合去除重復(fù)對(duì)象的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01

最新評(píng)論