概要
actuator是一個(gè)監(jiān)控和度量springboot工具,讓我們更好的了解springboot.
前言
初學(xué)者對(duì)springboot自動(dòng)配置感覺(jué)很強(qiáng)大,同樣也有些疑惑.比如我知道了spring+mybatis的配置,比如需要配置datasource和SqlSessionFactory等等,但是在springboot集成mybatis時(shí),只是簡(jiǎn)單的配置了屬性,就實(shí)現(xiàn)了集成,根本不需要配置datasource和SqlSessionFactory.那么它又是如何實(shí)現(xiàn)自動(dòng)配置的呢?這時(shí)候actuator就可以派上用場(chǎng)了.
使用
配置actuator很簡(jiǎn)單
<!-- Spring Boot Web 依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依賴(lài) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依賴(lài) -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 連接驅(qū)動(dòng)依賴(lài) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
這是springboot集成mybatis,最后的依賴(lài)就是actuator.
加入依賴(lài)之后其實(shí)就可以啟動(dòng)程序訪(fǎng)問(wèn)/info和/health接口了,這是actuator默認(rèn)開(kāi)放的,其他接口需要另外配置,我在application.properties中實(shí)現(xiàn)配置.
## 數(shù)據(jù)源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
#防止訪(fǎng)問(wèn)actuator接口401問(wèn)題(很關(guān)鍵)
management.security.enabled=false
#actuator配置
endpoints.enabled=true
endpoints.info.sensitive=false
endpoints.health.sensitive=false
management.context-path=/
management.port=8081
這是簡(jiǎn)單的配置,只為查看mybatis的問(wèn)題.管理接口設(shè)定為8081.
這樣就可以查看接口了,我的目標(biāo)是/autoconfig接口.
返回的json中可以看出springboot自動(dòng)配置了SqlSessionFactory和SqlSessionTemplate了并且用condition注解的方式判斷是否需要自動(dòng)配置.
actuator的功能還有很多,就不一一說(shuō)明了.