物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控

0. 前言

  一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB UI。但是整体的注册中心还是基于Eureka,只是WebUI是用这个Spring Boot Admin 来显示而已。具体的结构如下所示

1. Eureka服务

  这个没什么好说的,按照创建一个微服务的流程,通过 Spring Starter 工具,自动生成一个Eureka微服务。主要就是配置Application.java application.yml pom.xml 这三个文件。
  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 
 6     <groupId>com.wunaozai.eureka</groupId>
 7     <artifactId>global-service-eureka</artifactId>
 8     <version>0.0.1</version>
 9     <packaging>jar</packaging>
10 
11     <name>global-service-eureka</name>
12     <description>服务注册中心</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.1.0.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25         <spring-cloud.version>Greenwich.M2</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.cloud</groupId>
31             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39     </dependencies>
40 
41     <dependencyManagement>
42         <dependencies>
43             <dependency>
44                 <groupId>org.springframework.cloud</groupId>
45                 <artifactId>spring-cloud-dependencies</artifactId>
46                 <version>${spring-cloud.version}</version>
47                 <type>pom</type>
48                 <scope>import</scope>
49             </dependency>
50         </dependencies>
51     </dependencyManagement>
52 
53     <build>
54         <plugins>
55             <plugin>
56                 <groupId>org.springframework.boot</groupId>
57                 <artifactId>spring-boot-maven-plugin</artifactId>
58             </plugin>
59         </plugins>
60     </build>
61 
62     <repositories>
63         <repository>
64             <id>spring-milestones</id>
65             <name>Spring Milestones</name>
66             <url>https://repo.spring.io/milestone</url>
67             <snapshots>
68                 <enabled>false</enabled>
69             </snapshots>
70         </repository>
71     </repositories>
72 
73 </project>

  application.yml 这里我配置两份,一份EUREKA1:8761 一份EUREKA:8762

 1 server:
 2   port: 8761
 3   
 4 spring:
 5   application:
 6     name: EUREKA服务注册中心
 7 
 8 management:
 9   endpoints:
10     web:
11       exposure:
12         include:
13         - "*"
14   endpoint:
15     health:
16       show-details: ALWAYS
17       
18 eureka:
19   client:
20 #    register-with-eureka: false
21 #    fetch-registry: false
22     service-url:
23       defaultZone: http://EUREKA1:8761/eureka/,http://EUREKA2:8762/eureka/
24   instance:
25     hostname: EUREKA1

  Application.java

 1 package com.wunaozai.eureka;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class GlobalServiceEurekaApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(GlobalServiceEurekaApplication.class, args);
13     }
14 }

 

2. Spring Boot Admin 服务

  默认是不需要帐号密码登录的,我这里配置一下spring-boot-starter-security 增加帐号密码登录
  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     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.2.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.wunaozai.admin.demo</groupId>
12     <artifactId>spring-cloud-admin-demo</artifactId>
13     <version>0.0.1</version>
14     <name>spring-cloud-admin-demo</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-boot-admin.version>2.1.1</spring-boot-admin.version>
20         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
21     </properties>
22 
23     <dependencies>
24         <dependency>
25             <groupId>de.codecentric</groupId>
26             <artifactId>spring-boot-admin-starter-server</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>de.codecentric</groupId>
30             <artifactId>spring-boot-admin-starter-client</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.cloud</groupId>
34             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
35         </dependency>
36         <dependency>
37             <groupId>org.springframework.cloud</groupId>
38             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
39         </dependency>
40         
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-starter-security</artifactId>
44         </dependency>
45 
46 
47         <dependency>
48             <groupId>org.springframework.boot</groupId>
49             <artifactId>spring-boot-devtools</artifactId>
50             <scope>runtime</scope>
51         </dependency>
52         <dependency>
53             <groupId>org.springframework.boot</groupId>
54             <artifactId>spring-boot-starter-test</artifactId>
55             <scope>test</scope>
56         </dependency>
57     </dependencies>
58 
59     <dependencyManagement>
60         <dependencies>
61             <dependency>
62                 <groupId>de.codecentric</groupId>
63                 <artifactId>spring-boot-admin-dependencies</artifactId>
64                 <version>${spring-boot-admin.version}</version>
65                 <type>pom</type>
66                 <scope>import</scope>
67             </dependency>
68             <dependency>
69                 <groupId>org.springframework.cloud</groupId>
70                 <artifactId>spring-cloud-dependencies</artifactId>
71                 <version>${spring-cloud.version}</version>
72                 <type>pom</type>
73                 <scope>import</scope>
74             </dependency>
75         </dependencies>
76     </dependencyManagement>
77 
78     <build>
79         <plugins>
80             <plugin>
81                 <groupId>org.springframework.boot</groupId>
82                 <artifactId>spring-boot-maven-plugin</artifactId>
83             </plugin>
84         </plugins>
85     </build>
86 
87 </project>

  applicatoin.yml

 1 server:
 2   port: 8788
 3 
 4 spring:
 5   boot:
 6     admin:
 7       client:
 8         url:
 9         - "http://ADMIN:8788" #这里配置Spring Boot Admin 服务地址,配置这里表示把自己注册到Admin上,如果使用Eureka服务发现的,这部分可以省略
10         username: 'user'
11         password: 'password'
12   application:
13     name: WEB服务监控中心
14   security:
15     user:
16       name: 'user'
17       password: 'password'
18 
19 
20 management:
21   endpoints:
22     web:
23       exposure:
24         include:
25         - "*"
26   endpoint:
27     health:
28       show-details: ALWAYS
29 
30 info:
31   version: ${spring.application.name}:${server.port}
32 
33 
34 eureka:
35   instance:
36     lease-renewal-interval-in-seconds: 10
37     health-check-url-path: /actuator/health
38     # 注册给eureka的时候告诉eureka自己的密码
39     metadata-map:
40       "user.name": ${spring.security.user.name}
41       "user.password": ${spring.security.user.password}
42     prefer-ip-address: true
43   client:
44     registry-fetch-interval-seconds: 5
45 #    fetch-registry: false
46 #    register-with-eureka: false
47     service-url:
48       defaultZone: ${EUREKA_SERVICE_URL:http://EUREKA1:8761}/eureka/,${EUREKA_SERVICE_URL:http://EUREKA2:8762}/eureka/

  Application.java

 1 package com.wunaozai.admin.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 8 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 9 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
10 
11 import de.codecentric.boot.admin.server.config.AdminServerProperties;
12 import de.codecentric.boot.admin.server.config.EnableAdminServer;
13 
14 @Configuration
15 @EnableAdminServer
16 @EnableEurekaClient
17 @SpringBootApplication
18 public class SpringCloudAdminDemoApplication {
19 
20     public static void main(String[] args) {
21         SpringApplication.run(SpringCloudAdminDemoApplication.class, args);
22     }
23 
24     @Configuration
25     public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
26         private final String adminContextPath;
27         
28         public SecuritySecureConfig(AdminServerProperties prop) {
29             this.adminContextPath = prop.getContextPath();
30         }
31         
32         @Override
33         protected void configure(HttpSecurity http) throws Exception {
34             SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
35             handler.setTargetUrlParameter("redirectTo");
36             
37             http.authorizeRequests()
38                 .antMatchers(adminContextPath + "/assets/**").permitAll()
39                 .antMatchers(adminContextPath + "/login").permitAll()
40                 .antMatchers(adminContextPath + "/actuator/**").permitAll()
41                 .anyRequest().authenticated()
42                 .and()
43                 .formLogin().loginPage(adminContextPath + "/login").successHandler(handler)
44                 .and()
45                 .logout().logoutUrl(adminContextPath + "/logout").and()
46                 .httpBasic().and().csrf().disable();
47         }
48     }
49 }

 

3. Spring Boot Client 服务(配置到Eureka服务)
  这里就配置Eureka Client 即可
  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     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.2.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.wunaozai.client.demo</groupId>
12     <artifactId>spring-cloud-client-demo</artifactId>
13     <version>0.0.1</version>
14     <name>spring-cloud-client-demo</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-boot-admin.version>2.1.1</spring-boot-admin.version>
20         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
21     </properties>
22 
23     <dependencies>
24         <dependency>
25             <groupId>de.codecentric</groupId>
26             <artifactId>spring-boot-admin-starter-client</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>     
32         <dependency>
33             <groupId>org.springframework.cloud</groupId>
34             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
35         </dependency>
36         <dependency>
37             <groupId>org.springframework.cloud</groupId>
38             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
39         </dependency>
40 
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-devtools</artifactId>
44             <scope>runtime</scope>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-test</artifactId>
49             <scope>test</scope>
50         </dependency>
51     </dependencies>
52 
53     <dependencyManagement>
54         <dependencies>
55             <dependency>
56                 <groupId>de.codecentric</groupId>
57                 <artifactId>spring-boot-admin-dependencies</artifactId>
58                 <version>${spring-boot-admin.version}</version>
59                 <type>pom</type>
60                 <scope>import</scope>
61             </dependency>
62             <dependency>
63                 <groupId>org.springframework.cloud</groupId>
64                 <artifactId>spring-cloud-dependencies</artifactId>
65                 <version>${spring-cloud.version}</version>
66                 <type>pom</type>
67                 <scope>import</scope>
68             </dependency>
69         </dependencies>
70     </dependencyManagement>
71 
72     <build>
73         <plugins>
74             <plugin>
75                 <groupId>org.springframework.boot</groupId>
76                 <artifactId>spring-boot-maven-plugin</artifactId>
77             </plugin>
78         </plugins>
79     </build>
80 
81 </project>

  applicatoin.yml

 1 server:
 2   port: 18889
 3 spring:
 4 #  boot:
 5 #    admin:
 6 #      client:
 7 #        url:
 8 #        - "http://127.0.0.1:8788"
 9 #        username: 'user'
10 #        password: 'password'
11   application:
12     name: 子业务服务器
13     
14 management:
15   endpoints:
16     web:
17       exposure:
18         include:
19         - "*"
20   endpoint:
21     health:
22       show-details: ALWAYS
23       
24 #eureka:
25 #  client:
26 #    fetch-registry: false
27 #    register-with-eureka: false
28 eureka:
29   instance:
30     lease-renewal-interval-in-seconds: 10
31     health-check-url-path: /actuator/health
32     prefer-ip-address: true
33   client:
34     registry-fetch-interval-seconds: 5
35     service-url:
36       defaultZone: ${EUREKA_SERVICE_URL:http://172.16.23.241:8761}/eureka/
37 
38       

  Application.java

 1 package com.wunaozai.client.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 
 7 @EnableEurekaClient
 8 @SpringBootApplication
 9 public class SpringCloudClientDemoApplication {
10 
11     public static void main(String[] args) {
12         System.out.println("ok");
13         SpringApplication.run(SpringCloudClientDemoApplication.class, args);
14     }
15 
16 }

 

4. Spring Boot Client 服务(直接配置Admin地址)
  如果不想通过Eureka,只是用Spring Boot Admin,可以只配置Admin的地址来实现。通过在application.yml 配置

1 spring:
2   boot:
3     admin:
4       client:
5         url:
6         - "http://127.0.0.1:8788"
7         username: 'user'
8         password: 'password'

 

5. 举个例子(基于docker-compose)

  使用mvn package 把以上3个微服务打包成Docker镜像
  Dockerfile 文件

1 FROM java:8
2 VOLUME /tmp
3 
4 ADD spring-cloud-admin-demo-0.0.1.jar app.jar
5 RUN bash -c 'touch /app.jar'
6 
7 EXPOSE 8788
8 
9 ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]

  Build 构建镜像

1 docker build -t wunaozai/eureka:0.0.1 -f Dockerfile .

  docker-compose.yml 文件

 1 version: '3'
 2 
 3 services:
 4     EUREKA1:
 5         image: eureka:1
 6         ports:
 7             - 8761:8761
 8     EUREKA2:
 9         image: eureka:2
10         ports:
11             - 8762:8762
12     ADMIN:
13         image: admin:1
14         ports:
15             - 8788:8788
16     client-1:
17         image: client:1
18         ports:
19             - 18881:18888
20     client-2:
21         image: client:1
22         ports:
23             - 18882:18888
24     client-3:
25         image: client:1
26         ports:
27             - 18883:18888
28     client-4:
29         image: client:1
30         ports:
31             - 18884:18888
32     client-5:
33         image: client:1
34         ports:
35             - 18885:18888
36     client-6:
37         image: client:1
38         ports:
39             - 18886:18888
40     client-7:
41         image: client:1
42         ports:
43             - 18887:18888
44     client-8:
45         image: client:1
46         ports:
47             - 18888:18888

 

  日志界面

  WeaveScope 界面

  Eureka 界面

  Spring Boot Admin 界面

   如果出现这个问题,请升级到最新的浏览器(Chrome) 一开始用Chrome 59,不行,升级到Chrome 71 才可以。

 

参考资料
  https://blog.csdn.net/kinginblue/article/details/52132113
  https://blog.csdn.net/hubo_88/article/details/80671192

本文地址: https://www.cnblogs.com/wunaozai/p/10313190.html

 

posted @ 2019-01-25 17:46  无脑仔的小明  阅读(...)  评论(...编辑  收藏