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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Amaze UI 开发思路通过拆分、封装一些常用的网页组件,以规范化采用云适配平台开发的移动网站,统一用户体验逐渐形成的。1、语义化 Amaze UI开发遵循语义化原则,意图通过类名(class)等信息直观传达元素的功能角色,同时关注结构、样式、行为分离,降低各部分的耦合程度,提高开发效率和可维护性。2、移动优先,跨屏适配 遵循 “移动优先(Mobile First)”、“渐进增强(Progressive enhancement)”的理念,可先从移动设备开始开发网站,逐步在扩展的更大屏幕的设备上,专注于最重要的内容和交互,适应移动互联潮流。轻松创建跨屏适配的网页。3、模块化,按需定制 AMUI使用LESS编写样式,结构良好,易扩展,易维护;使用Seajs模块化开发、组织 JavaScript,自然、优雅。4、专注于HTML5AMUI 基于轻量的Zepto.js开发,有效减少为兼容旧浏览器的臃肿代码;基于 CSS3 的交互效果,平滑、高效。AMUI专注于现代浏览器(支持HTML5),不再为过时的浏览器耗费资源,为更有价值的用户提高更好的体验。5、本地化支持相比国外的前端框架,Amaze UI专注解决中文排版优化问题,根据操作系统调整字体,实现最佳中文排版效果;针对国内主流浏览器及App内置浏览器提供更好的兼容性支持,为你节省大量兼容性调试时间。Amaze UI 的开发历程云适配创始人陈本峰:这个项目最开始是作为内部使用工具来开发的。我们云适配本身就是一个前端产品,Amaze UI能帮我们降低开发时间和成本,用标准化作业流程,能有更高的产出。从云适配创立之初,我们就开始积累自己的前端框架,同时也借鉴了Bootstrap等国外框架的优点。在内部使用过程中,大家一致反映不错,我们就希望把这个产品开源,希望分享给更多的人,也希望更多的人来贡献代码,来共建中国前端开源生态环境。在最近几个月,我们投入人力将这个项目整理成一个开源产品。目前有2个软件工程师全职在开发这个产品,还有一个设计,一个PM也在尽力配合。产品发布之后,我们会投入更多全职的工程师来专心打磨这个产品,同时也呼吁更多的前端开发爱好者共同来完善这个框架。Amaze UI的目标帮助开发者提高开发效率,提升网页效果,即用最短的时间做出最赞的网页,使更多的前端开发者不再受前端复杂代码困扰。下面是特性硬广Amaze UI 是中国首个开源 HTML5 跨屏前端框架,基于 React.js 开发的 Web 组件库。Amaze UI 是一个轻量级、Mobile first的前端框架, 基于开源社区流行前端框架编写。为移动而生Amaze UI 采用业内先进的 mobile first 理念,从小屏逐步扩展到大屏,最终实现所有屏幕适配,适应移动互联潮流。组件丰富,模块化Amaze UI 含近 20 个 CSS 组件、10 个 JS 组件,更有 17 款包含近 60 个主题的 Widgets,可快速构建界面出色、体验优秀的跨屏页面,大幅度提升你的开发效率。本地化支持相比国外的前端框架,Amaze UI 专注解决中文排版优化问题,根据操作系统调整字体,实现最佳中文排版效果;针对国内主流浏览器及 App 内置浏览器提供更好的兼容性支持,为你节省大量兼容性调试时间。轻量级,高性能Amaze UI 非常注重性能,基于轻量的 Zepto.js 开发,并使用 CSS3 来做动画交互,平滑、高效,更适合移动设备,让你的 Web 应用可以高速载入。 标签:Amaze
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值