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一起學習

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM