物联网架构成长之路(30)-springbootadmin微服务webui监控

无脑仔的小明      2022-04-26     288

关键词:

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();
查看详情  

物联网架构成长之路-emq插件配置

1.前言  上一小结说了插件的创建,这一节主要怎么编写代码,以及具体流程之类的。2.增加一句HelloWorld  修改./deps/emq_plugin_wunaozai/src/emq_plugin_wunaozai.erl增加一行HelloWorld  增加后,保存1makeclean2make3cp-rebin../../_rel/emqttd/lib/em... 查看详情

物联网架构成长之路-emq插件创建

1.说明  以下用到的知识,是建立在我目前所知道的知识领域,以后如果随着知识的拓展,不一定会更新内容。由于不是EMQ公司的人,EMQ的文档又很少,很多知识点都是靠猜的。2.一些资料  架构设计http://emqtt.com/docs/v2/design.... 查看详情

物联网架构成长之路-emq权限控制

1.前言  EMQTT属于一个比较小众的开源软件,很多资料不全,很麻烦,很多功能都是靠猜测,还有就是看官方提供的那几个插件,了解。2.说明  上一小节的插件emq_plugin_wunaozai  文件 emq_plugin_wunaozai.erl  这个文件就是... 查看详情

物联网架构成长之路(40)-bladex开发框架入门

0.前言  前一小节,讲了如何入门,这里就简单讲一下如何自定义查询和权限控制配置。1.配置多租户  如果要启用该表的多租户功能,需要在application.yml这里配置。2.配置模糊匹配1@GetMapping(value="/list2")2publicR<IPage<Wunaozai... 查看详情

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

物联网架构成长之路(39)-bladex开发框架环境搭建

... 上一篇博客已经介绍了,阶段性小结。目前第一版的物联网平台已经趋于完成。框架基本不变了,剩下就是调整一些UI,还有配合硬件和市场那边,看看怎么推广这个平台。能不能挣点外快。第一版系统虽然简陋,但是对于整... 查看详情

物联网架构成长之路-emq权限验证小结

1.前言  经过前面几小节,讲了一下插件开发,这一小节主要对一些代码和目录结构进行讲解,这些都是测试过程中一些个人经验,不一定是官方做法。而且也有可能会因为版本不一致导致差异。2.目录结构   这个目录... 查看详情

物联网架构成长之路(19)-域名申请注册(代码片段)

0.前言  没有什么,就是把注册域名的过程记录下来而已。服务器公司近期不做,我自己做了。没有资源,我自己弄。自己玩。1.注册域名  现在注册个域名,还是比较简单的,我就直接在阿里云上注册了。首先,你可能要... 查看详情

物联网架构成长之路(59)-springboot项目作为系统应用,自启动

一、前言  开发完项目,一般调试都是在eclipse或者idea上运行服务的。但是要部署到服务器上,就需要后台运行和自启动等配置了。这里采用Debian系统作为演示。 二、后台运行  工程项目代码采用maven进行打包。1mvnwpackag... 查看详情

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

0.前言  结合前面两篇博客,前面博客实现了Gateway网关的路由功能。此时,如果每个微服务都需要一套帐号认证体系就没有必要了。可以在网关处进行权限认证。然后转发请求到后端服务。这样后面的微服务就可以直接调用,... 查看详情

物联网架构成长之路(27)-docker练习之zookeeper安装(代码片段)

0.前言  准备了解一下消息队列MQ,对比了一些开源的中间件,最后选择Kafka作为以后用到的消息队列,消息队列的应用场景及Kafka与其他消息队列的优缺点这里就不细说了,具体的可以参考其他博客说明。不过Kafka依赖Zookeeper... 查看详情

物联网架构成长之路(24)-docker练习之compose容器编排(代码片段)

0.前言  一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂。因此学习东西还是要循序渐进,慢慢来。先了解单机编排技术DockerCompose,了解一些技术细节及原理后,在入手K8s... 查看详情

物联网架构成长之路(23)-docker练习之elasticsearch服务搭建(代码片段)

0.前言  最近基本都是学一些环境配置,和一些中间件的安装与配置。没有实际编写代码。可能看起来有点水,我对自己的学习方式是,先要了解各个中间件的安装配置以及简单使用,理论应用场景,然后我在小项目中,逐步... 查看详情

物联网架构成长之路(50)-emq配置ssl证书,实现mqtts协议(代码片段)

0.前言  EMQ是带有SSL功能的,需要进行简单的配置,才能使用。下面就简单说一下如何实现自签证书。 1.利用OpenSSL签发证书1?catcreateCA.sh2#/bin/sh3#生成自签名的CAkey和证书4opensslgenrsa-outca.key20485opensslreq-x509-new-nodes-keyca.key-sha256... 查看详情

我的物联网成长记9物联网平台安全如何破?

【摘要】很多朋友都很关心华为物联网云平台的安全能力,今天它来了。安全的设备接入,稳定放心;安全的数据传输,可靠不怕泄露;安全的数据处理,保护用户隐私。今天我们就将围绕端、管、云和应用的安全能力为大家进... 查看详情

开启运维自动化架构师成长之路

...句话是我在网上看到认为最有道理的励志语录了,当然互联网IT行业的工作者相对理解的会更加深刻。以这句话开头引出我将要写的这篇文章。首先,请允许我做一个自我介绍:熟悉的朋友喜欢叫我一声岩哥,这么些年我也认可... 查看详情

云计算成长之路第一章01

一个小白学习互联网技术的成长之路,分享给大家,也算是记录自己的成长之路。大神直接忽略DAY011.0云计算简介云计算:提供云服务的机器。由一群计算机把数据备份,过滤,加密,存储。2.0服务器架构能够为其他计算机提供... 查看详情