springcloud(九) springboot Actuator + admin 监控
前一章讲的都是Feign项目(调用方)的监控。接下来讲的是服务提供方的监控
一、springboot actuator + springboot admin
Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件,它针对springboot的actuator接口进行UI美化封装
springboot admin 独立工程:
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.tuling.cloud</groupId> 6 <artifactId>spring-boot-admin-server</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 <name>08-ms-spring-boot-admin</name> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <java.version>1.8</java.version> 14 </properties> 15 16 <dependencies> 17 <!-- spring-boot-admin server端, 有server端必有客户端, 服务提供者就是客户端--> 18 <dependency> 19 <groupId>de.codecentric</groupId> 20 <artifactId>spring-boot-admin-server</artifactId> 21 <version>1.5.6</version> 22 </dependency> 23 <dependency> 24 <groupId>de.codecentric</groupId> 25 <artifactId>spring-boot-admin-server-ui</artifactId> 26 <version>1.5.6</version> 27 </dependency> 28 </dependencies> 29 30 <!-- 添加spring-boot的maven插件 --> 31 <build> 32 <plugins> 33 <plugin> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-maven-plugin</artifactId> 36 </plugin> 37 </plugins> 38 </build> 39 </project>
SpringBootAdminApplication 启动类:
1 package com.jiagoushi.cloud.study; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.context.annotation.Configuration; 6 7 import de.codecentric.boot.admin.config.EnableAdminServer; 8 9 @Configuration 10 @EnableAutoConfiguration 11 @EnableAdminServer //支持admin 12 public class SpringBootAdminApplication { 13 14 public static void main(String[] args) { 15 SpringApplication.run(SpringBootAdminApplication.class, args); 16 } 17 }
启动之后是这样的:
什么都没有? 我们在启动一个服务提供者就有 了
user服务提供者:
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.tuling.cloud</groupId> 6 <artifactId>microservice-provider-user</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 <name>08-ms-provider-user</name> 10 11 <!-- 引入spring boot的依赖 --> 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>1.5.9.RELEASE</version> 16 </parent> 17 18 <properties> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <java.version>1.8</java.version> 21 </properties> 22 23 <dependencies>
24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency>
28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-data-jpa</artifactId> 31 </dependency>
32 <dependency> 33 <groupId>com.h2database</groupId> 34 <artifactId>h2</artifactId> 35 </dependency>
<!-- 配合springboot damin 监控 必须要依赖此包--> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-actuator</artifactId> 39 </dependency> 40 41 <dependency> 42 <groupId>org.springframework.cloud</groupId> 43 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 44 </dependency> 45 46 <!-- 作为spring boot admin客户端 ,想被监控就要添加此包 --> 47 <dependency> 48 <groupId>de.codecentric</groupId> 49 <artifactId>spring-boot-admin-starter-client</artifactId> 50 <version>1.5.6</version> 51 </dependency> 52 53 </dependencies> 54 55 <!-- 引入spring cloud的依赖 --> 56 <dependencyManagement> 57 <dependencies> 58 <dependency> 59 <groupId>org.springframework.cloud</groupId> 60 <artifactId>spring-cloud-dependencies</artifactId> 61 <version>Edgware.RELEASE</version> 62 <type>pom</type> 63 <scope>import</scope> 64 </dependency> 65 </dependencies> 66 </dependencyManagement> 67 68 <!-- 添加spring-boot的maven插件 --> 69 <build> 70 <plugins> 71 <plugin> 72 <groupId>org.springframework.boot</groupId> 73 <artifactId>spring-boot-maven-plugin</artifactId> 74 </plugin> 75 </plugins> 76 </build> 77 </project>
和之前用的user服务最大区别就是application.yml:
1 server: 2 port: 8001 3 spring: 4 application: 5 name: microservice-provider-user 6 boot: 7 admin: 8 url: http://localhost:9999 #注意这里:spring boot admin服务端地址,搜集客户端监控数据 9 jpa: 10 generate-ddl: false 11 show-sql: true 12 hibernate: 13 ddl-auto: none 14 datasource: # 指定数据源 15 platform: h2 # 指定数据源类型 16 schema: classpath:schema.sql # 指定h2数据库的建表脚本 17 data: classpath:data.sql # 指定h2数据库的数据脚本 18 logging: # 配置日志级别,让hibernate打印出执行的SQL 19 level: 20 root: INFO 21 org.hibernate: INFO 22 org.hibernate.type.descriptor.sql.BasicBinder: TRACE 23 org.hibernate.type.descriptor.sql.BasicExtractor: TRACE 24 25 eureka: 26 client: 27 serviceUrl: 28 defaultZone: http://localhost:8761/eureka/ 29 instance: 30 prefer-ip-address: true 31
#这里配置actuaotr不能丢,不然springboot admin会没数据 32 management: 33 security: 34 enabled: false #关掉安全认证 35 port: 8899 #管理端口调整成8888,独立的端口可以做安全控制 actuator的端口 36 context-path: /monitor #actuator的访问路径 37 health: 38 mail: 39 enabled: false
ProviderUserApplication_08 启动类:
1 package com.jiagoushi.cloud.study; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 @EnableDiscoveryClient 8 @SpringBootApplication 9 public class ProviderUserApplication_08 { 10 public static void main(String[] args) { 11 SpringApplication.run(ProviderUserApplication_08.class, args); 12 } 13 }
启动之后再次访问spring boot admin , 发现有一个user服务
点击详情:
欢迎来群592495675一起学习