物联网架构成长之路(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

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

时间: 2024-10-11 15:10:38

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

物联网架构成长之路(18)-接阿里云OSS服务

1.申请/购买OSS服务 在阿里云上申请/购买OSS服务, 然后在会得AccessKeyID,AccessKeySecret,bucketName 这三个东西 2.增删改查 在pom.xml文件上增加 1 <!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss --> 2 <dependency> 3 <groupId>com.aliyun.oss</groupId> 4 &

物联网架构成长之路(24)-Docker练习之Compose容器编排

0.前言 一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂.因此学习东西还是要循序渐进,慢慢来.先了解单机编排技术Docker Compose,了解一些技术细节及原理后,在入手K8s.还是不能一口吃成胖子,要多吃几口才可以.而且目前公司都是一些小项目,能用得上DockerCompose已经很不错了,还想要上K8s,估计是不现实的. 1. 安装 可以通过运行下面命令进行安装, 1 curl -L https://github.com/d

物联网架构成长之路(56)-SpringCloudGateway+JWT实现网关鉴权

0. 前言 结合前面两篇博客,前面博客实现了Gateway网关的路由功能.此时,如果每个微服务都需要一套帐号认证体系就没有必要了.可以在网关处进行权限认证.然后转发请求到后端服务.这样后面的微服务就可以直接调用,而不需要每个都单独一套鉴权体系.参考了Oauth2和JWT,发现基于微服务,使用JWT会更方便一些,所以准备集成JWT作为微服务架构的认证方式. [https://www.cnblogs.com/wunaozai/p/12512753.html] 物联网架构成长之路(54)-基于Naco

物联网架构成长之路(0)-目录

一.基础 [http://www.cnblogs.com/wunaozai/p/8067621.html] 物联网架构成长之路(1)-前言 [http://www.cnblogs.com/wunaozai/p/8075640.html] 物联网架构成长之路(2)-脚手架工具准备 [http://www.cnblogs.com/wunaozai/p/8082332.html] 物联网架构成长之路(3)-EMQ消息服务器了解

Spring Boot、微服务架构和大数据

一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况下,如何从系统架构的角度出发,构建灵活.易扩展的系统,快速应对需求的变化:同时,随着用户的增加,如何保证系统的可伸缩性.高可用性,

一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事

微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物. 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况下,如何从系统架构的角度出发,构建灵活.易扩展的系统,快速应对需求的变化:同时,随着用户的增加,如何保证系统的可伸缩性.高可用性,成为系统架构面临的挑战.如果你想了解大数据的学习路线,想学习

使用Spring Boot构建微服务(文末福利)

本文主要内容 学习微服务的关键特征 了解微服务是如何适应云架构的 将业务领域分解成一组微服务 使用Spring Boot实现简单的微服务 掌握基于微服务架构构建应用程序的视角 学习什么时候不应该使用微服务 软件开发的历史充斥着大型开发项目崩溃的故事,这些项目可能投资了数百万美元.集中了行业里众多的顶尖人才.消耗了开发人员成千上万的工时,但从未给客户交付任何有价值的东西,最终由于其复杂性和负担而轰然倒塌. 这些庞大的项目倾向于遵循大型传统的瀑布开发方法,坚持在项目开始时界定应用的所有需求和设计.这

代码大爆炸|用Spring Boot创建微服务的21种代码描述(上)

代码大爆炸|用Spring Boot创建微服务的21种代码描述(上)

使用Spring Boot创建微服务

过去几年以来,"微服务架构"的概念已经在软件开发领域获得了一个稳定的基础.作为"面向服务架构"(SOA)的一个继任者,微服务同样也可以被归类为"分布式系统"这一类,并且进一步发扬了SOA中的许多概念与实践.不过,它们在不同之处在于每个单一服务所应承担的责任范围.在SOA中,每个服务将负责处理广范围的功能与数据领域,而微服务的一种通用指南则认为,它所负责的部分是管理一个单独的数据领域,以及围绕着该领域的相关功能.使用分布式系统方式的目的是将整体性的