物联网架构成长之路(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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.wunaozai.eureka</groupId>
<artifactId>global-service-eureka</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging> <name>global-service-eureka</name>
<description>服务注册中心</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>
application.yml 这里我配置两份,一份EUREKA1:8761 一份EUREKA:8762
server:
port: 8761 spring:
application:
name: EUREKA服务注册中心 management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: ALWAYS eureka:
client:
# register-with-eureka: false
# fetch-registry: false
service-url:
defaultZone: http://EUREKA1:8761/eureka/,http://EUREKA2:8762/eureka/
instance:
hostname: EUREKA1
Application.java
package com.wunaozai.eureka; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class GlobalServiceEurekaApplication { public static void main(String[] args) {
SpringApplication.run(GlobalServiceEurekaApplication.class, args);
}
}
2. Spring Boot Admin 服务
默认是不需要帐号密码登录的,我这里配置一下spring-boot-starter-security 增加帐号密码登录
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wunaozai.admin.demo</groupId>
<artifactId>spring-cloud-admin-demo</artifactId>
<version>0.0.1</version>
<name>spring-cloud-admin-demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.1</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
applicatoin.yml
server:
port: 8788 spring:
boot:
admin:
client:
url:
- "http://ADMIN:8788" #这里配置Spring Boot Admin 服务地址,配置这里表示把自己注册到Admin上,如果使用Eureka服务发现的,这部分可以省略
username: 'user'
password: 'password'
application:
name: WEB服务监控中心
security:
user:
name: 'user'
password: 'password' management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: ALWAYS info:
version: ${spring.application.name}:${server.port} eureka:
instance:
lease-renewal-interval-in-seconds: 10
health-check-url-path: /actuator/health
# 注册给eureka的时候告诉eureka自己的密码
metadata-map:
"user.name": ${spring.security.user.name}
"user.password": ${spring.security.user.password}
prefer-ip-address: true
client:
registry-fetch-interval-seconds: 5
# fetch-registry: false
# register-with-eureka: false
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://EUREKA1:8761}/eureka/,${EUREKA_SERVICE_URL:http://EUREKA2:8762}/eureka/
Application.java
package com.wunaozai.admin.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer; @Configuration
@EnableAdminServer
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudAdminDemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudAdminDemoApplication.class, args);
} @Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties prop) {
this.adminContextPath = prop.getContextPath();
} @Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setTargetUrlParameter("redirectTo"); http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.antMatchers(adminContextPath + "/actuator/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(handler)
.and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and().csrf().disable();
}
}
}
3. Spring Boot Client 服务(配置到Eureka服务)
这里就配置Eureka Client 即可
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wunaozai.client.demo</groupId>
<artifactId>spring-cloud-client-demo</artifactId>
<version>0.0.1</version>
<name>spring-cloud-client-demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.1</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
applicatoin.yml
server:
port: 18889
spring:
# boot:
# admin:
# client:
# url:
# - "http://127.0.0.1:8788"
# username: 'user'
# password: 'password'
application:
name: 子业务服务器 management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: ALWAYS #eureka:
# client:
# fetch-registry: false
# register-with-eureka: false
eureka:
instance:
lease-renewal-interval-in-seconds: 10
health-check-url-path: /actuator/health
prefer-ip-address: true
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://172.16.23.241:8761}/eureka/
Application.java
package com.wunaozai.client.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient
@SpringBootApplication
public class SpringCloudClientDemoApplication { public static void main(String[] args) {
System.out.println("ok");
SpringApplication.run(SpringCloudClientDemoApplication.class, args);
} }
4. Spring Boot Client 服务(直接配置Admin地址)
如果不想通过Eureka,只是用Spring Boot Admin,可以只配置Admin的地址来实现。通过在application.yml 配置
spring:
boot:
admin:
client:
url:
- "http://127.0.0.1:8788"
username: 'user'
password: 'password'
5. 举个例子(基于docker-compose)
使用mvn package 把以上3个微服务打包成Docker镜像
Dockerfile 文件
FROM java:8
VOLUME /tmp ADD spring-cloud-admin-demo-0.0.1.jar app.jar
RUN bash -c 'touch /app.jar' EXPOSE 8788 ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
Build 构建镜像
docker build -t wunaozai/eureka:0.0.1 -f Dockerfile .
docker-compose.yml 文件
version: '3' services:
EUREKA1:
image: eureka:1
ports:
- 8761:8761
EUREKA2:
image: eureka:2
ports:
- 8762:8762
ADMIN:
image: admin:1
ports:
- 8788:8788
client-1:
image: client:1
ports:
- 18881:18888
client-2:
image: client:1
ports:
- 18882:18888
client-3:
image: client:1
ports:
- 18883:18888
client-4:
image: client:1
ports:
- 18884:18888
client-5:
image: client:1
ports:
- 18885:18888
client-6:
image: client:1
ports:
- 18886:18888
client-7:
image: client:1
ports:
- 18887:18888
client-8:
image: client:1
ports:
- 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
物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控的更多相关文章
- 物联网架构成长之路(22)-Docker练习之Etcd服务搭建
0. 前言 时隔多日,前段时间忙完一个可有可无的项目后,又进入摸鱼时间,没有办法,非互联网公司,就是闲得蛋疼.又开始了自学之路.以前入门过Docker,然后又很久没有看了,最近重新看了一下,推荐一下这 ...
- 物联网架构成长之路(23)-Docker练习之Elasticsearch服务搭建
0. 前言 最近基本都是学一些环境配置,和一些中间件的安装与配置.没有实际编写代码.可能看起来有点水,我对自己的学习方式是,先要了解各个中间件的安装配置以及简单使用,理论应用场景,然后我在小项目中,逐 ...
- 物联网架构成长之路(18)-接阿里云OSS服务
1.申请/购买OSS服务 在阿里云上申请/购买OSS服务, 然后在会得AccessKeyID,AccessKeySecret,bucketName 这三个东西 2.增删改查 在pom.xml文件上增加 ...
- 物联网架构成长之路(25)-Docker构建项目用到的镜像1
0. 前言 现在项目处于初级阶段,按照规划,先构建几个以后可能会用到的Image,并上传到阿里云的Docker仓库.以后博客中用到的Image,大部分都会用到这几个基础的Image,构建一个简单的物联 ...
- 物联网架构成长之路(24)-Docker练习之Compose容器编排
0.前言 一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂.因此学习东西还是要循序渐进,慢慢来.先了解单机编排技术Docker Compo ...
- 物联网架构成长之路(31)-EMQ基于HTTP权限验证
看过之前的文章就知道,我之前是通过搞插件,或者通过里面的MongoDB来进行EMQ的鉴权登录和权限验证.但是前段时间发现,还是通过HTTP WebHook 方式来调用鉴权接口比较适合实际使用.还是实现 ...
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...
- Spring Boot、微服务架构和大数据
一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶 ...
- 一文读懂spring boot 和微服务的关系
欢迎访问网易云社区,了解更多网易技术产品运营经验. Spring Boot 和微服务没关系, Java 微服务治理框架普遍用的是 Spring Cloud. Spring Boot 产生的背景,是开发 ...
随机推荐
- 【可靠性】Mysql 5.7 降低了半同步复制-数据丢失的风险
如果你的生产线开启了半同步复制,那么对数据的一致性会要求较高,但在MySQL5.5/5.6里,会存在数据不一致的风险.有这么一个场景,客户端提交了一个事务,master把binlog发送给slave, ...
- XamarinEssentials教程获取首选项的值
XamarinEssentials教程获取首选项的值 如果开发者想要获取首选项中某一项的值时,可以使用Preferences类的Get()方法实现,该方法可以对指定键的值进行获取.该方法有12种形式, ...
- vue实例属性之methods和computed
我们可以把同一函数放在methods或者computed中,区别在于computed有缓存,计算属性只有在它的相关依赖发生改变时才会重新求值,即数据改变才会执行函数.而methods每当触发重新渲染时 ...
- UVA 277 Puzzle
题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...
- LOJ.6073.[2017山东一轮集训Day5]距离(可持久化线段树 树链剖分)
题目链接 就是恶心人的,简单写写了...(似乎就是[HNOI2015]开店?) 拆式子,记\(dis_i\)为\(i\)到根节点的路径权值和,\(Ans=\sum dis_{p_i}+\sum dis ...
- 洛谷 P1078 文化之旅(CODEVS 1316)
题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一 种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不 同的国家可能有相同的文化.不同 ...
- 移动端触屏滑动,JS事件
先了解下 移动端的触屏滑动 毕竟这玩意其实和PC端还是有一定的区别的 hh 整理了下网上的资料放一放 移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等 ...
- java第二周的学习知识3(==与equals)
==与equals()的之间的差别1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等:如果作用于引用类型的变量,则比较的是所指向的对象的地址 2)对于equals方法,注意 ...
- Ruby语法基础(二)
Ruby语法基础(二) 继续ruby的学习,这次主要把目光放到运算符,条件判断,循环,方法,以及其他ruby特有的基本概念上 运算符 算术运算符:+,-,/,%,**,值的注意的是,ruby中一切皆为 ...
- Maven实战(七)——常用Maven插件介绍(上)
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven-compiler-plugin完成的.进一步说,每个任务对应了 ...