後端接口響應慢,通常我們就需要優化代碼和sql,如果項目中使用druid連接配接池,那麼我們可以利用其提供的sql監控功能,來幫助我們快速定位慢sql已經sql執行次數等問題,springboot2之後,durid監控配置變的更簡單了,不需要額外的代碼,隻需要添加配置即可。整個項目配置如下:
依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml
spring:
datasource:
druid:
url: jdbc:postgresql://127.0.0.1:5432/test01?characterEncoding=utf-8
username: admin
password: 123456
driver-class-name: org.postgresql.Driver
initial-size: 1
max-active: 20
max-wait: 6000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=2000
min-idle: 1
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-max-count: 1000
session-stat-enable: true
profile-enable: true
stat-view-servlet:
enabled: true
url-pattern: "/druid/*"
login-username: root
login-password: root
allow: 127.0.0.1
reset-enable: true
type: com.alibaba.druid.pool.DruidDataSource
url:
username: admin
password: 123456
driver-class-name:
filters: stat,wall,slf4j
maxActive: 20
jpa:
database: postgresql
show-sql: true
properties:
hibernate:
temp:
use_jdbc_metadata_defaults: false
測試添加的相關類
package com.junlin.druid.Entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="newtable")
public class DemoEntity implements Serializable {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.junlin.druid.Dao;
import com.junlin.druid.Entity.DemoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface DemoJPA extends JpaRepository<DemoEntity,Integer> {
List<DemoEntity> findByName(String name);
}
package com.junlin.druid.Controller;
import com.junlin.druid.Dao.DemoJPA;
import com.junlin.druid.Entity.DemoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DemoController {
@Autowired
private DemoJPA demoJPA;
@GetMapping("/findName")
public Object findEntity(String name){
List<DemoEntity> list = demoJPA.findByName(name);
return list;
}
}
啟動項目,通路localhost:8080/druid/,進入登陸頁面輸入在application.yml配置好的使用者和密碼(demo中都是root),即可看到監控頁面
通路一下我們準備好的接口,執行幾次sql查詢,在監控頁面點看sql監控頁面,就能看到我們的sql執行情況
其它相關的配置以及頁面相關内容可以參考druid官網文檔,位址https://github.com/alibaba/druid/wiki/常見問題
轉自 :https://www.cnblogs.com/hhhshct/p/11378770.html
後端接口響應慢,通常我們就需要優化代碼和sql,如果項目中使用druid連接配接池,那麼我們可以利用其提供的sql監控功能,來幫助我們快速定位慢sql已經sql執行次數等問題,springboot2之後,durid監控配置變的更簡單了,不需要額外的代碼,隻需要添加配置即可。整個項目配置如下:
依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml
spring:
datasource:
druid:
url: jdbc:postgresql://127.0.0.1:5432/test01?characterEncoding=utf-8
username: admin
password: 123456
driver-class-name: org.postgresql.Driver
initial-size: 1
max-active: 20
max-wait: 6000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=2000
min-idle: 1
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-max-count: 1000
session-stat-enable: true
profile-enable: true
stat-view-servlet:
enabled: true
url-pattern: "/druid/*"
login-username: root
login-password: root
allow: 127.0.0.1
reset-enable: true
type: com.alibaba.druid.pool.DruidDataSource
url:
username: admin
password: 123456
driver-class-name:
filters: stat,wall,slf4j
maxActive: 20
jpa:
database: postgresql
show-sql: true
properties:
hibernate:
temp:
use_jdbc_metadata_defaults: false
測試添加的相關類
package com.junlin.druid.Entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="newtable")
public class DemoEntity implements Serializable {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.junlin.druid.Dao;
import com.junlin.druid.Entity.DemoEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface DemoJPA extends JpaRepository<DemoEntity,Integer> {
List<DemoEntity> findByName(String name);
}
package com.junlin.druid.Controller;
import com.junlin.druid.Dao.DemoJPA;
import com.junlin.druid.Entity.DemoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DemoController {
@Autowired
private DemoJPA demoJPA;
@GetMapping("/findName")
public Object findEntity(String name){
List<DemoEntity> list = demoJPA.findByName(name);
return list;
}
}
啟動項目,通路localhost:8080/druid/,進入登陸頁面輸入在application.yml配置好的使用者和密碼(demo中都是root),即可看到監控頁面
通路一下我們準備好的接口,執行幾次sql查詢,在監控頁面點看sql監控頁面,就能看到我們的sql執行情況
其它相關的配置以及頁面相關内容可以參考druid官網文檔,位址https://github.com/alibaba/druid/wiki/常見問題